はやし屋

競技プログラミングのメモ きたない

aoj 0114: Electro-Fly

lcmとってやる

#include<iostream>
#include<algorithm>
using namespace std;
#define lcm(a,b) ((a)/(__gcd(a,b))*(b))
 
long long calc(long long a , long long b){
  long long ret = 1;
  long long c = a%b;
  while(c != 1){c = (a*c)%b;ret++;}
  return ret ;
}
 
int main(){
  long long a1 ,m1 ,a2 ,m2 ,a3 ,m3;
  while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3 && 
    (a1|m1|a2|m2|a3|m3)){
    long long x , y , z;
    x = calc(a1,m1);
    y = calc(a2,m2);
    z = calc(a3,m3);
 
    cout << lcm(x,lcm(y,z)) << endl;
  }
}