alexjercan/bugnet huggingface dataset:
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  } else {
    return gcd((long long int)max(a % b, b), (long long int)min(a % b, b));
  }
}

long long int lcm(long long int a, long long int b) {
  long long int c = a * b / gcd(a, b);
  return c;
}

int main() {
  long long int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0) {
      break;
    }
    long long int x, y, z;
    x = y = z = 1;
    long long int counter = 1;
    long long int counter2 = 1;
    long long int counter3 = 1;
    x = (a1 * x) % m1;
    y = (a2 * y) % m2;
    z = (a3 * z) % m3;
    while (x != 1) {
      x = (a1 * x) % m1;
      counter++;
    }
    while (y != 1) {
      y = (a2 * y) % m2;
      counter2++;
    }
    while (z != 1) {
      z = (a3 * z) % m3;
      counter3++;
    }
    cout << lcm(counter, lcm(counter2, counter3)) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long long int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0) {
      break;
    }
    long long int x, y, z;
    x = y = z = 1;
    long long int counter = 1;
    x = (a1 * x) % m1;
    y = (a2 * y) % m2;
    z = (a3 * z) % m3;
    while (!(x == 1 && y == 1 && z == 1)) {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
      counter++;
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / (gcd(a, b)); }

int main() {
  int a[3], m[3];
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2] &&
         !(a[0] == 0 && a[1] == 0 && a[2] == 0 && m[0] == 0 && m[1] == 0 &&
           m[2] == 0)) {
    int x, y, z;
    x = y = z = 1;
    ll cnt = 1;
    int nx = (a[0] * x) % m[0];
    int ny = (a[1] * y) % m[1];
    int nz = (a[2] * z) % m[2];
    x = nx, y = ny, z = nz;

    while (1) {
      if (x == 1)
        break;
      nx = (a[0] * x) % m[0];
      x = nx;
      cnt++;
    }
    int xLoop = cnt;
    cnt = 1;
    while (1) {
      if (y == 1)
        break;
      ny = (a[1] * y) % m[1];
      y = ny;
      cnt++;
    }
    int yLoop = cnt;
    cnt = 1;
    while (1) {
      if (z == 1)
        break;
      nz = (a[2] * z) % m[2];
      z = nz;
      cnt++;
    }
    int zLoop = cnt;

    ll ret = lcm(zLoop, lcm(xLoop, yLoop));
    cout << ret << endl;

    // while(1){
    //	if(x==1&&y==1&&z==1){
    //		break;
    //	}
    //	nx=(a[0]*x)%m[0];
    //	ny=(a[1]*y)%m[1];
    //	nz=(a[2]*z)%m[2];
    //	x=nx,y=ny,z=nz;
    //	cnt++;
    // }
    // cout<<cnt<<endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int a[3], m[3];
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2] &&
         !(a[0] == 0 && a[1] == 0 && a[2] == 0 && m[0] == 0 && m[1] == 0 &&
           m[2] == 0)) {
    int x, y, z;
    x = y = z = 1;
    ll cnt = 1;
    int nx = (a[0] * x) % m[0];
    int ny = (a[1] * y) % m[1];
    int nz = (a[2] * z) % m[2];
    x = nx, y = ny, z = nz;
    while (1) {
      if (x == 1 && y == 1 && z == 1) {
        break;
      }
      nx = (a[0] * x) % m[0];
      ny = (a[1] * y) % m[1];
      nz = (a[2] * z) % m[2];
      x = nx, y = ny, z = nz;
      cnt++;
    }
    cout << cnt << endl;
    //	break;
  }

  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;
ll gcd2(ll a, ll b) {
  while (b != 0) {
    ll w = a % b;
    a = b;
    b = w;
  }
  return a;
}

ll lcm(ll a, ll b) { return a * b / gcd2(a, b); }
ll count(ll a, ll m) {
  ll x = a % m, i;
  for (i = 1; x != 1; ++i) {
    x = a * x % m;
  }
  return i;
}
int main() {
  int a1, a2, a3, m1, m2, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (a1 == 0 && a2 == 0 && a3 == 0 && m1 == 0 && m2 == 0 && m3 == 0)
      break;
    int xc, yc, zc;
    xc = count(a1, m1);
    yc = count(a2, m2);
    zc = count(a3, m3);
    cout << lcm(lcm(xc, yc), zc) << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int a1, a2, a3, m1, m2, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (a1 == 0 && a2 == 0 && a3 == 0 && m1 == 0 && m2 == 0 && m3 == 0)
      break;
    int x = 1, y = 1, z = 1, i;
    for (i = 0;; ++i) {
      if (x == 1 && y == 1 && z == 1 && i != 0)
        break;
      x = a1 * x % m1;
      y = a2 * y % m2;
      z = a3 * z % m3;
    }
    cout << i << "\\n";
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
bool flag;
unsigned long long a1, a2, a3, m1, m2, m3;
unsigned long long x, y, z, ans, calc, cnt1, cnt2, cnt3;

unsigned long long gcd(unsigned long long a, unsigned long long b) {
  unsigned long long result = a;
  unsigned long long k = 0;
  unsigned long long n = b;
  do {
    k = result % n;
    result = n;
    n = k;
  } while (k != 0);
  return result;
}
int main() {
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (!(a1 || m1 || a2 || m2 || a3 || m3))
      break;
    x = y = z = 1;
    cnt1 = cnt2 = cnt3 = 0;
    while (1) {
      cnt1++;
      x = (a1 % m1) * (x % m1) % m1;
      if (x == 1)
        break;
    }
    while (1) {
      cnt2++;
      y = (a2 % m2) * (y % m2) % m2;
      if (y == 1)
        break;
    }
    while (1) {
      cnt3++;
      z = (a3 % m3) * (z % m3) % m3;
      if (z == 1)
        break;
    }
    calc = cnt1 / gcd(cnt1, cnt2) * cnt2;
    ans = cnt3 / gcd(calc, cnt3) * calc;
    cout << ans << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;
bool flag;
unsigned long long a1, a2, a3, m1, m2, m3;
unsigned long long x, y, z, ans, calc, cnt1, cnt2, cnt3;

unsigned long long gcd(unsigned long long a, unsigned long long b) {
  unsigned long long result = a;
  unsigned long long k = 0;
  unsigned long long n = b;
  do {
    k = result % n;
    result = n;
    n = k;
  } while (k != 0);
  return result;
}
int main() {
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    x = y = z = 1;
    cnt1 = cnt2 = cnt3 = 0;
    while (1) {
      cnt1++;
      x = a1 * x % m1;
      if (x == 1)
        break;
    }
    while (1) {
      cnt2++;
      y = a2 * y % m2;
      if (y == 1)
        break;
    }
    while (1) {
      cnt3++;
      z = a3 * z % m3;
      if (z == 1)
        break;
    }
    calc = cnt1 / gcd(cnt1, cnt2) * cnt2;
    ans = cnt3 / gcd(calc, cnt3) * calc;
    cout << ans << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

int gcd(int x, int y) {
  int r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }

  return y;
}

long lcm(int x, int y) { return (long)x * y / gcd(x, y); }

int main() {
  while (1) {
    int a[3], m[3], p[3], memo[3], flag[3] = {0};

    for (int i = 0; i < 3; ++i)
      scanf(" %d %d", &a[i], &m[i]);

    if (a[0] == 0 && m[0] == 0)
      break;

    for (int i = 0; i < 3; ++i)
      p[i] = a[i] % m[i];
    long count = 1;
    int mcount = 0;

    while (1) {
      for (int i = 0; i < 3; ++i) {
        if (p[i] == 1 && flag[i] == 0) {
          memo[i] = count;
          flag[i] = 1;
          ++mcount;
          // printf("mmm\\n");
        }
      }

      if (mcount == 3)
        break;

      for (int i = 0; i < 3; ++i)
        p[i] = (a[i] * p[i]) % m[i];

      ++count;
    }

    long tmp = lcm(memo[0], memo[1]);
    long ans = lcm(memo[2], tmp);

    printf("%ld\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

int main() {
  while (1) {
    int a[3], m[3], p[3];

    for (int i = 0; i < 3; ++i)
      scanf(" %d %d", &a[i], &m[i]);

    if (a[0] == 0 && m[0] == 0)
      break;

    for (int i = 0; i < 3; ++i)
      p[i] = a[i] % m[i];
    long count = 1;

    while (1) {
      if (p[0] == 1 && p[1] == 1 && p[2] == 1)
        break;

      for (int i = 0; i < 3; ++i)
        p[i] = (a[i] * p[i]) % m[i];

      ++count;
    }

    printf("%ld\\n", count);
  }

  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(a % b, b);
}
int main() {
  ll in[6] = {0};
  while (cin >> in[0] >> in[1] >> in[2] >> in[3] >> in[4] >> in[5],
         in[0] + in[1] + in[2] + in[3] + in[4] + in[5]) {
    rep(i, 3) {
      ll a = in[i * 2], b = in[i * 2 + 1];
      ll co = 0;
      ll d = 1;
      while (1) {
        co++;
        d = (a * d) % b;
        if (d == 1)
          break;
      }
      in[i] = co;
    }
    ll d = in[1] * in[2] / gcd(in[1], in[2]);
    cout << d * in[0] / gcd(in[0], d) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define pb(in, tmp) in.push_back(tmp)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) loop(i, 0, b)
#define all(in) in.begin(), in.end()
using namespace std;
int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f, a || b || c || d || e || f) {
    long long int co = 0;
    int x = 1, y = 1, z = 1;
    do {
      x = a * x % b;
      y = c * y % d;
      z = e * z % f;
      co++;
    } while (x - 1 || y - 1 || z - 1);
    cout << co << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  if ((a == 0) || (b == 0))
    return 0;
  while (a != b) {
    if (a > b)
      a = a - b;
    else
      b = b - a;
  }
  return a;
}

ll lcm(ll a, ll b) {
  if ((a == 0) || (b == 0))
    return 0;
  return ((a / gcd(a, b)) * b);
}

int main() {
  ll x, y, z, flag, a[3], m[3];
  while (1) {
    ll x = 1, y = 1, z = 1, flag = 1;
    for (int i = 0; i < 3; i++) {
      scanf("%lld%lld", &a[i], &m[i]);
      if (a[i] != 0 || m[i] != 0)
        flag = 0;
    }
    if (flag)
      break;
    int countx = 0, county = 0, countz = 0;
    while (1) {
      x = (a[0] * x) % m[0];
      countx++;
      if (x == 1)
        break;
    }
    while (1) {
      y = (a[1] * y) % m[1];
      county++;
      if (y == 1)
        break;
    }
    while (1) {
      z = (a[2] * z) % m[2];
      countz++;
      if (z == 1)
        break;
    }
    ll re = lcm(countx, county);
    re = lcm(re, countz);
    printf("%lld\\n", re);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int x, y, z, flag, a[3], m[3];
  while (1) {
    int x = 1, y = 1, z = 1, flag = 1;
    for (int i = 0; i < 3; i++) {
      scanf("%d%d", &a[i], &m[i]);
      if (a[i] != 0 || m[i] != 0)
        flag = 0;
    }
    if (flag)
      break;
    int count = 0;
    while (1) {
      x = (a[0] * x) % m[0];
      y = (a[1] * y) % m[1];
      z = (a[2] * z) % m[2];
      count++;
      if (x == 1 && y == 1 && z == 1)
        break;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
typedef long long ll;
int a[3], m[3];
ll cnt[3];

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {
  while (1) {
    for (int i = 0; i < 3; i++) {
      scanf("%d %d", &a[i], &m[i]);
    }
    if (a[0] == 0)
      break;
    for (int i = 0; i < 3; i++) {
      int x = a[i] % m[i];
      cnt[i] = 1;
      while (x != 1) {
        x = x * a[i] % m[i];
        cnt[i]++;
      }
    }
    ll k = cnt[0] * cnt[1] / gcd(cnt[0], cnt[1]);
    printf("%lld\\n", k * cnt[2] / gcd(k, cnt[2]));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int a[3], m[3];

int main(void) {
  while (1) {
    int x[3] = {1, 1, 1};
    long long int ans = 0;
    scanf("%d%d%d%d%d%d", &a[0], &m[0], &a[1], &m[1], &a[2], &m[2]);
    if (a[0] == 0 && m[0] == 0 && a[1] == 0 && m[1] == 0 && a[2] == 0 &&
        m[2] == 0)
      break;
    while (1) {
      ans++;
      for (int i = 0; i < 3; i++) {
        x[i] = (x[i] * a[i]) % m[i];
      }
      if (x[0] == 1 && x[1] == 1 && x[2] == 1)
        break;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
ll m[3], a[3];
ll gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(int a, int b) {
  if (a < b)
    swap(a, b);
  return (a / gcd(a, b)) * b;
}
int main() {
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2], a[0]) {
    ll xyz[3], res[3];
    xyz[0] = xyz[1] = xyz[2] = 1;
    for (int i = 0; i < 3; i++) {
      ll cnt = 0;
      do {
        cnt++;
        xyz[i] = a[i] * xyz[i] % m[i];
      } while (xyz[i] != 1);
      res[i] = cnt;
    }
    cout << lcm(res[0], lcm(res[1], res[2])) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int x, y, z;
  int m1, m2, m3, a1, a2, a3;

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, m1) {
    int cnt = 0;
    x = y = z = 1;
    while (1) {
      x = a1 * x % m1;
      y = a2 * y % m2;
      z = a3 * z % m3;
      cnt++;
      if (x == 1 && y == 1 && z == 1)
        break;
    }
    cout << cnt << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long lcm(long long, long long);
long long gcd(long long, long long);
int main() {
  int a1, m1, a2, m2, a3, m3;
  long long x, y, z;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 && m1 && a2 && m2 && a3 && m3) {
    x = y = z = 1;
    for (int i = a1 % m1; i != 1; i = i * a1 % m1)
      x++;
    for (int i = a2 % m2; i != 1; i = i * a2 % m2)
      y++;
    for (int i = a3 % m3; i != 1; i = i * a3 % m3)
      z++;
    cout << lcm(lcm(x, y), z) << endl;
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

long long gcd(long long a, long long b) {
  if (a < b) {
    gcd(b, a);
  }
  if (b <= 0) {
    return a;
  }
  return gcd(b, a % b);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long lcm(long long, long long);
long long gcd(long long, long long);
int main() {
  int a1, m1, a2, m2, a3, m3;
  long long x, y, z;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 && m1 && a2 && m2 && a3 && m3) {
    x = y = z = 1;
    for (int i = a1; i != 1; i = i * a1 % m1)
      x++;
    for (int i = a2; i != 1; i = i * a2 % m3)
      y++;
    for (int i = a3; i != 1; i = i * a3 % m2)
      z++;
    cout << lcm(lcm(x, y), z) << endl;
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

long long gcd(long long a, long long b) {
  if (a < b) {
    gcd(b, a);
  }
  if (b <= 0) {
    return a;
  }
  return gcd(b, a % b);
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); }
long lcm(long a, long b) { return a / gcd(a, b) * b; }
int main() {
  long a1, m1, a2, m2, a3, m3, x, y, z, n1, n2, n3;
  while (cin >> a1) {
    cin >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 == 0)
      break;
    for (n1 = 1, x = 1;;) {
      x = (a1 * x) % m1;
      if (x == 1)
        break;
      n1++;
    }
    for (n2 = 1, y = 1;;) {
      y = (a2 * y) % m2;
      if (y == 1)
        break;
      n2++;
    }
    for (n3 = 1, z = 1;;) {
      z = (a3 * z) % m3;
      if (z == 1)
        break;
      n3++;
    }
    cout << lcm(lcm(n1, n2), n3) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long a1, m1, a2, m2, a3, m3, x, y, z, n;
  while (cin >> a1) {
    cin >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 == 0)
      break;
    x = y = z = 1;
    n = 1;
    for (;;) {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
      if (x == 1 && y == 1 && z == 1)
        break;
      n++;
    }
    cout << n << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

typedef long long ll;

using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll lcm_n(vector<ll> &num) {
  int l = num[0];
  for (int i = 0; i < num.size(); i++)
    l = lcm(l, num[i]);
  return l;
}

ll func(ll a, ll m) {
  ll cnt = 1, x = a % m;
  while (x != 1)
    x = (a * x) % m, cnt++;
  return cnt;
}

int main(void) {

  ll m[4], a[4];

  while (true) {

    int b = 0;
    for (int i = 1; i < 4; i++) {
      cin >> a[i] >> m[i];
      b = a[i] + m[i];
    }
    if (b == 0)
      break;

    ll cnt = 0, x = 1, y = 1, z = 1;
    vector<ll> num;

    for (int i = 1; i < 4; i++)
      num.push_back(func(a[i], m[i]));

    cout << lcm(lcm(num[0], num[1]), num[2]) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {

  int m[4], a[4];

  while (true) {

    int b = 0;
    for (int i = 1; i < 4; i++) {
      cin >> a[i] >> m[i];
      b |= a[i] | m[i];
    }
    if (!b)
      break;

    int cnt = 0, x = 1, y = 1, z = 1, nx, ny, nz;

    while (true) {
      if (cnt != 0 && x == 1 && y == 1 && z == 1)
        break;
      x = (a[1] % m[1] * x % m[1]) % m[1];
      y = (a[2] % m[2] * y % m[2]) % m[2];
      z = (a[3] % m[3] * z % m[3]) % m[3];
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
ll gcd(ll x, ll y) {
  if (y > 0) {
    gcd(y, x % y);
  } else {
    return x;
  }
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  ll a1, a2, a3, m1, m2, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    ll x, y, z, a, b, c;
    a = b = c = x = y = z = 1;
    x = (a1 * x) % m1;
    y = (a2 * y) % m2;
    z = (a3 * z) % m3;
    for (; x != 1; a++) {
      x = (x * a1) % m1;
    }
    for (; y != 1; b++) {
      y = (y * a2) % m2;
    }
    for (; z != 1; c++) {
      z = (z * a3) % m3;
    }
    ll cnt = lcm(lcm(a, b), c);
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int main() {
  int a[3];
  int m[3];
  bool flag = true;
  while (flag) {
    int x, y, z;
    x = y = z = 1;
    int cnt = 0;
    int sum = 0;
    for (int i = 0; i < 3; i++) {
      cin >> a[i];
      cin >> m[i];
      if (a[i] != 0 || m[i] != 0) {
        sum = 1;
      }
    }
    while (true) {
      if (sum == 0) {
        flag = false;
        break;
      }
      x = (a[0] * x) % m[0];
      y = (a[1] * y) % m[1];
      z = (a[2] * z) % m[2];
      cnt++;
      // cout <<"test"<<endl;
      if (x == 1 && x == y && x == z) {
        cout << cnt << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;
ll GCD(ll a, ll b) { return (a == 0) ? b : GCD(b % a, a); }
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
int main() {
  int a[3], m[3];
  while (1) {
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (!a[0]) {
      break;
    }
    int val[3] = {1, 1, 1}, move[3] = {0};
    int cnt = 0;
    for (int i = 1;; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (move[j])
          continue;
        val[j] = a[j] * val[j] % m[j];
        if (val[j] == 1) {
          move[j] = i;
          cnt++;
        }
      }
      if (cnt >= 3)
        break;
    }
    ll ans = LCM(LCM(move[0], move[1]), move[2]);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;
ll GCD(ll a, ll b) { return (a == 0) ? b : GCD(b % a, a); }
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
int main() {
  int a[3], m[3];
  while (1) {
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (!a[0]) {
      break;
    }
    int val[3] = {1, 1, 1}, move[3];
    int cnt = 0;
    for (int i = 1;; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (move[j])
          continue;
        val[j] = a[j] * val[j] % m[j];
        if (val[j] == 1) {
          move[j] = i;
          cnt++;
        }
      }
      if (cnt >= 3)
        break;
    }
    ll ans = LCM(LCM(move[0], move[1]), move[2]);
    cout << ans << endl;
  }
}