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

#include <bits/stdc++.h>
using namespace std;
//????°???¬?????°
long long koubaisu(long long a, long long b) {
  long long x = a * b;
  /* ?????¶??° a > b ????¢?????????\\??? */
  if (a < b) {
    long long tmp = a;
    a = b;
    b = tmp;
  }
  /* ?????????????????????????????? */
  long long r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  /* ????°???¬?????°????????? */
  return x / b;
}
int main() {
  long long a[3], m[3];
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2],
         a[0] + a[1] + a[2] + m[0] + m[1] + m[2]) {
    long long x = a[0] % m[0], y = a[1] % m[1], z = a[2] % m[2];
    long long cnt = 0;
    long long indx, indy, indz;
    if (x == 1)
      indx = 1;
    else
      for (int i = 0; i < m[0] - 1; i++) {
        if (x == 1) {
          indx = i + 1;
          break;
        }
        x = (a[0] * x) % m[0];
      }
    if (y == 1)
      indy = 1;
    else
      for (int i = 0; i < m[1] - 1; i++) {
        if (y == 1) {
          indy = i + 1;
          break;
        }
        y = (a[1] * y) % m[1];
      }
    if (z == 1)
      indz = 1;
    else
      for (int i = 0; i < m[2] - 1; i++) {
        if (z == 1) {
          indz = i + 1;
          break;
        }
        z = (a[2] * z) % m[2];
      }
    long long ind;
    ind = koubaisu(indx, indy);
    cnt = koubaisu(ind, indz);
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[3], m[3];
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2],
         a[0] + a[1] + a[2] + m[0] + m[1] + m[2]) {
    int x = a[0] % m[0], y = a[1] % m[1], z = a[2] % m[2];
    int cnt;
    if (x == 1 && y == 1 && z == 1)
      cnt = 0;
    else
      cnt = 1;
    while (!(x == 1 && y == 1 && z == 1)) {
      x = (a[0] * x) % m[0];
      y = (a[1] * y) % m[1];
      z = (a[2] * z) % m[2];
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define int long long

using namespace std;

int cou(int a, int m);
int ggd(int number1, int number2);
int lcm(int number1, int number2);

signed main() {
  int a1, a2, a3, m1, m2, m3;
  int x, y, z;
  int count;
  int countx, county, countz;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    x = 1;
    y = 1;
    z = 1;
    count = 0;

    countx = cou(a1, m1);
    county = cou(a2, m2);
    countz = cou(a3, m3);

    cout << lcm(lcm(countx, county), countz) << endl;
  }
  return 0;
}

int cou(int a, int m) {
  int x = 1;
  int count = 0;
  do {
    count++;
    x = (a * x) % m;
  } while (x != 1);
  return count;
}

int ggd(int number1, int number2) {
  int m = number1;
  int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }

  while (n != 0) {
    int temp = m % n;
    m = n;
    n = temp;
  }

  return m;
}

int lcm(int number1, int number2) {
  return number1 * number2 / ggd(number1, number2);
}

Error: TLE

Fixed submission:

#include <iostream>
#define int long long

using namespace std;

int cou(int a, int m);
int ggd(int number1, int number2);
int lcm(int number1, int number2);

signed main() {
  int a1, a2, a3, m1, m2, m3;
  int x, y, z;
  int count;
  int countx, county, countz;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    x = 1;
    y = 1;
    z = 1;
    count = 0;

    countx = cou(a1, m1);
    county = cou(a2, m2);
    countz = cou(a3, m3);

    cout << lcm(lcm(countx, county), countz) << endl;
  }
  return 0;
}

int cou(int a, int m) {
  int x = 1;
  int count = 0;
  do {
    count++;
    x = (a * x) % m;
  } while (x != 1);
  return count;
}

int ggd(int number1, int number2) {
  int m = number1;
  int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }

  while (m != n) {
    int temp = n;
    n = m - n;
    m = temp;
  }

  return m;
}

int lcm(int number1, int number2) {
  return number1 * number2 / ggd(number1, number2);
}
Problem p00114 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long lli;

int f(int a, int b) {
  int x = 1;
  int i = 0;
  do {
    x = (a * x) % b;
    i++;
  } while (x != 1);
  return i;
}

lli lcm(lli p, lli q) { return p / __gcd(p, q) * q; }

int main() {
  for (lli a1, m1, a2, m2, a3, m3; cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;) {
    if (a1 + m1 + a2 + m2 + a3 + m3 == 0)
      break;
    int r1 = f(a1, m1);
    int r2 = f(a2, m2);
    int r3 = f(a3, m3);
    lli ans = lcm(lcm(r1, r2), r3);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long lli;

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

#include <iostream>
using namespace std;

long long gcd(long long u, long long v) {
  long long t;
  if (u > v) {
    t = u;
    u = v;
    v = t;
  }
  while (v > 0) {
    u = u % v;
    t = u;
    u = v;
    v = t;
  }
  return u;
}

long long low(long long i, long long j) { return i * j / gcd(i, j); }

long long three_low(long long i, long long j, long long k) {
  return low(i, low(j, k));
}

int main() {
  int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (!a1 && !m1 && !a2 && !m2 && !a3 && !m3)
      break;
    int x = 1, y = 1, z = 1;
    long long count1 = 0, count2 = 0, count3 = 0;
    do {
      x = a1 * x % m1;
      count1++;
    } while (x != 1);
    do {
      y = a2 * y % m2;
      count2++;
    } while (y != 1);
    do {
      z = a3 * z % m3;
      count3++;
    } while (z != 1);

    cout << three_low(count1, count2, count3) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <iostream>
using namespace std;
int main() {
  int a[3], m[3];
  while (true) {
    for (int i = 0; i < 3; i++)
      cin >> a[i] >> m[i];
    if (a[0] == 0)
      break;
    int cnt[3];
    for (int i = 0; i < 3; i++) {
      cnt[i] = 1;
      int x = 1;
      while (true) {
        x = x * a[i] % m[i];
        if (x == 1)
          break;
        cnt[i]++;
      }
    }
    long long s = min(cnt[0], cnt[1]);
    while (true) {
      bool ok = true;
      for (int i = 0; i < 2; i++)
        if (cnt[i] % s != 0)
          ok = false;
      if (ok)
        break;
      s--;
    }
    s = cnt[0] * cnt[1] / s;
    long long k;
    if (s < cnt[2])
      k = s;
    else
      k = cnt[2];
    while (true) {
      bool ok = true;
      if (s % k != 0 || cnt[2] % k != 0)
        ok = false;
      if (ok)
        break;
      k--;
    }
    cout << s * cnt[2] / k << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int a[3], m[3], s[3];
  while (true) {
    for (int i = 0; i < 3; i++) {
      cin >> a[i] >> m[i];
      s[i] = 1;
    }
    if (a[0] == 0)
      break;
    int ans = 0;
    while (true) {
      for (int i = 0; i < 3; i++) {
        s[i] = a[i] * s[i] % m[i];
      }
      ans++;
      if (s[0] == 1 && s[1] == 1 && s[2] == 1)
        break;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(abs(a - b), min(a, b));
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    ll cnt1 = 0, cnt2 = 0, cnt3 = 0;
    int x = 1, y = 1, z = 1;
    do {
      x = (a1 * x) % m1;
      cnt1++;
    } while (x != 1);
    do {
      y = (a2 * y) % m2;
      cnt2++;
    } while (y != 1);
    do {
      z = (a3 * z) % m3;
      cnt3++;
    } while (z != 1);
    cout << lcm(lcm(cnt1, cnt2), cnt3) << endl;
  }
}

Error: TLE

Fixed submission:

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

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

long long GCD(long long i, long long j) {
  long long alt;
  while (j > 0) {
    alt = j;
    j = i % j;
    i = alt;
  }
  return i;
}

int main() {
  int a[3], m[3];
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2],
         a[0] || a[1] || a[2] || m[0] || m[1] || m[2]) {
    int dt[3] = {1, 1, 1};
    long long cnt[3] = {0, 0, 0};
    rep(i, 3) {
      do {
        dt[i] = a[i] * dt[i] % m[i];
        cnt[i]++;
      } while (dt[i] != 1);
    }
    long long ans, gcd, lcm, lcm2;
    gcd = GCD(cnt[0], cnt[1]);
    lcm = gcd * (cnt[0] / gcd) * (cnt[1] / gcd);
    gcd = GCD(lcm, cnt[2]);
    lcm2 = gcd * (lcm / gcd) * (cnt[2] / gcd);
    cout << lcm2 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int a[3], m[3];
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2],
         a[0] || a[1] || a[2] || m[0] || m[1] || m[2]) {
    int ans = 0, x = 1, y = 1, z = 1;
    while (1) {
      x = a[0] * x % m[0];
      y = a[1] * y % m[1];
      z = a[2] * z % m[2];
      ans++;
      if (x == 1 && y == 1 && z == 1)
        break;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, a, n) for (int i = a; i < n; i++)
using namespace std;
int main() {
  long int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f, a) {
    a %= b;
    c %= d;
    e %= f;
    long int s = 1, p = max(a, max(c, e));
    r(i, 2, 100) r(j, 2, p + 1) {
      if (a % j == 0 && c % j == 0 && e % j == 0) {
        s *= j;
        a /= j;
        c /= j;
        e /= j;
      }
      if (a % j == 0 && c % j == 0) {
        s *= j;
        a /= j;
        c /= j;
      }
      if (a % j == 0 && e % j == 0) {
        s *= j;
        a /= j;
        e /= j;
      }
      if (c % j == 0 && e % j == 0) {
        s *= j;
        c /= j;
        e /= j;
      }
    }
    cout << s << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

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

int main() {
  int a[3], m[3];
  while (1) {
    for (int i = 0; i < 3; i++) {
      cin >> a[i] >> m[i];
    }
    if (!a[0] && !m[0] && !a[1] && !m[1] && !a[2] && !m[2])
      break;
    long long ans = 0;
    int r[3];
    r[0] = r[1] = r[2] = 1;
    while (1) {
      ans++;
      for (int i = 0; i < 2; i++) {
        r[i] = (a[i] * r[i]) % m[i];
      }
      if (r[0] == 1 && r[1] == 1 && r[2] == 1)
        break;
    }
    cout << ans << endl;
  }

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

#include <iostream>
using namespace std;

long long int gcd(long long int a, long long int b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}

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

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

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (!a1 && !m1 && !a2 && !m2 && !a3 && !m3)
      break;

    int x = 1, y = 1, z = 1;
    long long int ans1 = 0, ans2 = 0, ans3 = 0;
    do {
      ans1++;
      x = (a1 * x) % m1;
    } while (x != 1);
    do {
      ans2++;
      y = (a2 * y) % m2;
    } while (y != 1);
    do {
      ans3++;
      z = (a3 * z) % m3;
    } while (z != 1);

    cout << lcm(ans1, lcm(ans2, ans3)) << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (!a1 && !m1 && !a2 && !m2 && !a3 && !m3)
      break;

    int x = 1, y = 1, z = 1;
    long long int ans = 0;
    do {
      ans++;
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
    } while (x != 1 || y != 1 || z != 1);

    cout << ans << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
long long a[3], m[3], k[3];
long long gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  while (true) {
    cin >> 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;
    }
    for (int i = 0; i < 3; i++) {
      long long cnt = 0, x = 1;
      while (true) {
        if (x == 1 && cnt >= 1) {
          break;
        }
        x *= a[i];
        x %= m[i];
        cnt++;
      }
      k[i] = cnt;
    }
    int LCM = k[0] * k[1] / gcd(k[0], k[1]);
    cout << LCM * k[2] / gcd(LCM, k[2]) << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;
long long a[3], m[3], k[3];
long long gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]) {
    for (int i = 0; i < 3; i++) {
      long long cnt = 0, x = 1;
      while (true) {
        if (x == 1 && cnt >= 1) {
          break;
        }
        x *= a[i];
        x %= m[i];
        cnt++;
      }
      k[i] = cnt;
    }
    int LCM = k[0] * k[1] / gcd(k[0], k[1]);
    cout << LCM * k[2] / gcd(LCM, k[2]) << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Runtime Error

// x^t = 1 mod m のとき、
// x^(kt) = 1 mod m.
#include <iostream>
using namespace std;

typedef long long ll;

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

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

int main() {
  ll a1, m1, a2, m2, a3, m3;

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 + m1 + a2 + m2 + a3 + m3 == 0)
      break;

    ll x = 1, y = 1, z = 1;

    ll tmp = a1;
    while (tmp != 1) {
      x++;
      tmp = (tmp * a1) % m1;
    }

    tmp = a2;
    while (tmp != 1) {
      y++;
      tmp = (tmp * a2) % m2;
    }

    tmp = a3;
    while (tmp != 1) {
      z++;
      tmp = (tmp * a3) % m3;
    }

    ll ans = lcm(lcm(x, y), z);
    cout << ans << endl;
  }

  return 0;
}


Error: -8

Fixed submission:

// x^t = 1 mod m のとき、
// x^(kt) = 1 mod m.
#include <iostream>
using namespace std;

typedef long long ll;

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

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

int main() {
  ll a1, m1, a2, m2, a3, m3;

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    ll x = 1, y = 1, z = 1;

    ll tmp = a1;
    while (tmp != 1) {
      x++;
      tmp = (tmp * a1) % m1;
    }

    tmp = a2;
    while (tmp != 1) {
      y++;
      tmp = (tmp * a2) % m2;
    }

    tmp = a3;
    while (tmp != 1) {
      z++;
      tmp = (tmp * a3) % m3;
    }

    ll ans = lcm(lcm(x, y), z);
    cout << ans << endl;
  }

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

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

long long int cnt(int kk, int a, int m) {
  long long int step = 0;
  do {
    kk = (kk * a) % m;
    step++;
  } while (kk != 1);
  return step;
}

long long int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

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

int main() {
  int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    int step;
    long long int step_final;
    if (a1 == 0 && a2 == 0 && a3 == 0 && m1 == 0 && m2 == 0 && m3 == 0)
      break;
    else {
      int x = 1;
      int y = 1;
      int z = 1;
      long long int step_x = cnt(x, a1, m1);
      long long int step_y = cnt(y, a2, m2);
      long long int step_z = cnt(z, a3, m3);
      step_final = lcm(step_x, lcm(step_y, step_z));
    }
    cout << step_final << endl;
  }
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

long long ggd(long long x, long long y) {
  while (x != y) {
    if (x < y) {
      long long temp = x;
      x = y;
      y = temp;
    }
    long long num = y;
    y = x - y;
    x = num;
  }
  return x;
}

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

int main() {
  long long a1, a2, a3, m1, m2, m3;
  long long x, y, z;
  x = y = z = 1;
  long long i, j, k;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    long long count = 0;
    bool xc, yc, zc;
    xc = yc = zc = true;
    while (true) {
      count++;
      if (xc) {
        x = a1 * x % m1;
        if (x == 1) {
          i = count;
          xc = false;
        }
      }
      if (yc) {
        y = a2 * y % m2;
        if (y == 1) {
          j = count;
          yc = false;
        }
      }
      if (zc) {
        z = a3 * z % m3;
        if (z == 1) {
          k = count;
          zc = false;
        }
      }
      if (!(xc || yc || zc))
        break;
    }
    cout << lcm(lcm(i, j), k) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

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

#include <iostream>
#include <vector>

using namespace std;

long long gcd(long long a, long long b) {
  if (b > a)
    swap(a, b);
  long long r = a % b;
  if (r == 0)
    return b;
  return gcd(b, r);
}

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

long long count(int a, int m) {
  long long n = (a * 1) % m;
  long long cnt = 1;
  while (1) {
    if (n == 1)
      break;
    cnt++;
    n = (a * n) % m;
  }
  return cnt;
}

int main() {
  long long a, b, c;
  long long l, m, n;

  while (cin >> a >> l >> b >> m >> c >> n, a || b || c || l || m || n) {
    long long nx = count(a, l);
    long long ny = count(b, m);
    long long nz = count(c, n);
    long long ans = 0;
    long long tmp = lcm(nx, ny);
    ans = lcm(tmp, nz);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int a, b, c;
  int l, m, n;
  int x = 1;
  int y = 1;
  int z = 1;

  while (cin >> a >> l >> b >> m >> c >> n, a || b || c || l || m || n) {
    int nx = (a * x) % l;
    int ny = (b * y) % m;
    int nz = (c * z) % n;
    int cnt = 1;
    while (1) {
      if (nx == 1 && ny == 1 && nz == 1)
        break;

      nx = (a * nx) % l;
      ny = (b * ny) % m;
      nz = (c * nz) % n;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}