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

#include <bits/stdc++.h>
using namespace std;

long long gcd(long long a, long long b);
long long lcs(long long a, long long b);

int main() {
  while (1) {
    long long a1, m1, a2, m2, a3, m3;
    scanf("%lld %lld %lld %lld %lld %lld", &a1, &m1, &a2, &m2, &a3, &m3);
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0)
      break;
    long long x = 1, y = 1, z = 1, cntx = 0, cnty = 0, cntz = 0;
    do {
      x = (a1 * x) % m1;
      cntx++;
    } while (x != 1);
    do {
      y = (a2 * y) % m2;
      cnty++;
    } while (y != 1);
    do {
      z = (a3 * z) % m3;
      cntz++;
    } while (z != 1);
    printf("%lld\\n", lcs(lcs(cntx, cnty), cntz));
  }
  return 0;
}

long long gcd(long long a, long long b) {
  while (a % b != 0) {
    long long temp = b;
    b = a % b;
    a = temp;
  }
  return b;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  while (1) {
    int a1, m1, a2, m2, a3, m3;
    scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3);
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0)
      break;
    int x = 1, y = 1, z = 1, cnt = 0;
    do {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
      cnt++;
    } while (x != 1 || y != 1 || z != 1);
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

long long lcm(long long m, long long n) {
  if ((m == 0) || (n == 0))
    return 0;
  return ((m / gcd(m, n)) * n);
}

int main() {
  long long a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3 && a1) {
    long long x = 1, y = 1, z = 1;

    long long count_x = 1;
    while (true) {
      if ((a1 * x) % m1 == 1) {
        break;
      } else {
        count_x++;
        x = (a1 * x) % m1;
      }
    }

    long long count_y = 1;
    while (true) {
      if ((a2 * y) % m2 == 1) {
        break;
      } else {
        count_y++;
        y = (a2 * y) % m2;
      }
    }

    long long count_z = 1;
    while (true) {
      if ((a3 * z) % m3 == 1) {
        break;
      } else {
        count_z++;
        z = (a3 * z) % m3;
      }
    }

    long long ans = lcm(count_x, count_y);
    ans = lcm(ans, count_z);

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3 && a1) {
    int x = 1, y = 1, z = 1;
    int count = 1;

    while (true) {
      int flag = 0;
      if ((a1 * x) % m1 == 1) {
        flag++;
      }
      if ((a2 * y) % m2 == 1) {
        flag++;
      }
      if ((a3 * z) % m3 == 1) {
        flag++;
      }

      if (flag == 3) {
        cout << count << endl;
        break;
      } else {
        count++;

        x = (a1 * x) % m1;
        y = (a2 * y) % m2;
        z = (a3 * z) % m3;
      }
    }
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Runtime Error

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

// 4 % 6 6,4
// 6 % 4 4,2
// 4 % 2 = 0 2
//(4 / 2) * (6 / 2)  * 2;
//(4 + 6) / 2 * 2;
//(c[0] / gcd) * (c[0] / gcd)
//(4 / 2) * (6 / 2) * (12 / 2) * 2;
// 1 * 3 * 3 * 2 = 12

// a = a'm
// b = b'm
// c = c'm
// a',b',c'ªf
// a'b'c' / m ^ 2
// 2 3 6
long long int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// 4 6 2 * (4 / 2) * (6 / 2)
//=2 * 2 * 3 = 12
long long int lcm(long long int a, long long int b) {
  return a * b / gcd(a, b);
}

int main() {
  long long int a[3], m[3], p[3] = {1, 1, 1}, c[3] = {0};
  while (1) {
    memset(c, 0, sizeof(c));
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (!a[0])
      break;
    for (int i = 0; i < 3; i++) {
      do {
        p[i] = (a[i] * p[i]) % m[i];
        // cout << "Turn " << c[i] << ": " << p[i] << endl;
        c[i] += 1;
      } while (p[i] != 1);
    }
    // cout << c[0] << c[1] << c[2] << endl;
    // int gcd = rec(c[0],rec(c[1],c[2])),
    // lcm = rec(c[0],gcd) * rec(c[1],gcd) * rec(c[2],gcd) * gcd;
    // cout << gcd << " " << lcm << endl;
    cout << lcm(lcm(c[0], c[1]), c[2]) << endl;
  }
  // 2 * 1 % 5 = 2;
  // 2 * 2 % 5 = 4;
  // 2 * 4 % 5 = 3;3ñÚÅÍÈ¢©D
  // 2 * 3 % 5 = 1;
  // 3 * 1 % 7 = 3;
  // 3 * 3 % 7 = 2;
  // 3 * 2 % 7 = 6;
  // 3 * 6 % 7 = 4;
  // 3 * 4 % 7 = 5;
  // 3 * 5 % 7 = 1;6ñÚ
  return 0;
}

Error: -8

Fixed submission:

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

// 4 % 6 6,4
// 6 % 4 4,2
// 4 % 2 = 0 2
//(4 / 2) * (6 / 2)  * 2;
//(4 + 6) / 2 * 2;
//(c[0] / gcd) * (c[0] / gcd)
//(4 / 2) * (6 / 2) * (12 / 2) * 2;
// 1 * 3 * 3 * 2 = 12

// a = a'm
// b = b'm
// c = c'm
// a',b',c'ªf
// a'b'c' / m ^ 2
// 2 3 6
long long int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// 4 6 2 * (4 / 2) * (6 / 2)
//=2 * 2 * 3 = 12
long long int lcm(long long int a, long long int b) {
  if (!gcd(a, b))
    return 0;
  return a * b / gcd(a, b);
}

int main() {
  long long int a[3], m[3], p[3] = {1, 1, 1}, c[3] = {0};
  while (1) {
    memset(c, 0, sizeof(c));
    cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
    if (!a[0] && !a[1] && !a[2] && !a[3])
      break;
    for (int i = 0; i < 3; i++) {
      do {
        p[i] = (a[i] * p[i]) % m[i];
        // cout << "Turn " << c[i] << ": " << p[i] << endl;
        c[i] += 1;
      } while (p[i] != 1);
    }
    // cout << c[0] << c[1] << c[2] << endl;
    // int gcd = rec(c[0],rec(c[1],c[2])),
    // lcm = rec(c[0],gcd) * rec(c[1],gcd) * rec(c[2],gcd) * gcd;
    // cout << gcd << " " << lcm << endl;
    cout << lcm(lcm(c[0], c[1]), c[2]) << endl;
  }
  // 2 * 1 % 5 = 2;
  // 2 * 2 % 5 = 4;
  // 2 * 4 % 5 = 3;3ñÚÅÍÈ¢©D
  // 2 * 3 % 5 = 1;
  // 3 * 1 % 7 = 3;
  // 3 * 3 % 7 = 2;
  // 3 * 2 % 7 = 6;
  // 3 * 6 % 7 = 4;
  // 3 * 4 % 7 = 5;
  // 3 * 5 % 7 = 1;6ñÚ
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, a, b) for (int i = (a); i < (b); i++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const double EPS = 1e-10;
// int mod=1000000007

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

int main() {
  vi a(3, 0), m(3, 0), z(3, 0), co(3, 1), ans(3, 0);
  while (1) {
    rep(i, 3) cin >> a[i] >> m[i];
    if (a == z && m == z)
      break;
    rep(i, 3) {
      ans[i] = 0;
      do {
        co[i] = a[i] * co[i] % m[i];
        ans[i]++;
      } while (co[i] != 1);
    }
    cout << lcm(lcm(ans[0], ans[1]), ans[2]) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, a, b) for (int i = (a); i < (b); i++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const double EPS = 1e-10;
// int mod=1000000007

int main() {
  vi a(3, 0), m(3, 0), z(3, 0), co(3, 1), o(3, 1);
  int ans;
  while (1) {
    rep(i, 3) cin >> a[i] >> m[i];
    if (a == z && m == z)
      break;
    ans = 0;
    do {
      rep(i, 3) { co[i] = a[i] * co[i] % m[i]; }
      ans++;
    } while (co != o);
    cout << ans << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

long long calc(long long x, long long y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

int main() {
  long long int a1, a2, a3, m1, m2, m3, min_x, min_y, min_z, x, y, z, pre, ans;

  while (true) {
    scanf("%lld %lld %lld %lld %lld %lld", &a1, &m1, &a2, &m2, &a3, &m3);
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && m3 == 0 && m3 == 0)
      break;

    x = 1;
    for (min_x = 1; min_x <= m1; min_x++) {
      x = a1 * x % m1;
      if (x == 1)
        break;
    }

    y = 1;
    for (min_y = 1; min_y <= m2; min_y++) {
      y = a2 * y % m2;
      if (y == 1)
        break;
    }

    z = 1;
    for (min_z = 1; min_z <= m3; min_z++) {
      z = a3 * z % m3;
      if (z == 1)
        break;
    }

    if (min_x > min_y) {
      pre = ((min_x / (calc(min_x, min_y))) * min_y);
    } else {
      pre = ((min_y / (calc(min_y, min_x))) * min_x);
    }

    if (pre > min_z) {
      ans = ((pre / (calc(pre, min_z))) * min_z);
    } else {
      ans = ((min_z / (calc(min_z, pre))) * pre);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

struct Data {
  long long int x, y, z;
};

int main() {
  long long int a1, a2, a3, m1, m2, m3, count;
  Data data;

  while (true) {
    scanf("%lld %lld %lld %lld %lld %lld", &a1, &m1, &a2, &m2, &a3, &m3);
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && m3 == 0 && m3 == 0)
      break;

    count = 0;
    data.x = 1;
    data.y = 1;
    data.z = 1;

    while (true) {
      count++;
      data.x = a1 * data.x % m1;
      data.y = a2 * data.y % m2;
      data.z = a3 * data.z % m3;

      if (data.x == 1 && data.y == 1 && data.z == 1)
        break;
    }

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

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

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;

inline ll lcm(ll n, ll m) { return m * n / __gcd(n, m); }

int main() {
  ll a[3], m[3];

  while (true) {
    ll sum = 0;
    for (int i = 0; i < 3; i++) {
      cin >> a[i] >> m[i];
      sum += (a[i] + m[i]);
    }
    if (sum == 0)
      break;

    ll x[3] = {1, 1, 1}, cnt = 0;
    ll t[3] = {0};

    for (int i = 0; i < 3; i++) {
      while (true) {
        x[i] = (a[i] * x[i]) % m[i];

        t[i]++;
        if (x[i] == 1)
          break;
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  while (true) {
    int sum = 0;
    for (int i = 0; i < 3; i++) {
      cin >> a[i] >> m[i];
      sum += (a[i] + m[i]);
    }
    if (sum == 0)
      break;

    int x[3] = {1, 1, 1}, cnt = 0;

    while (true) {
      for (int i = 0; i < 3; i++)
        x[i] = (x[i] * a[i]) % m[i];

      cnt++;

      if (x[0] == 1 && x[1] == 1 && x[2] == 1)
        break;
    }
    cout << cnt << endl;
  }

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

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

long long solve(long long a, long long m) {
  long long count;
  long long x = 1;

  x = (a * x) % m;
  for (count = 1; x != 1; count++) {
    x = (a * x) % m;
  }

  return count;
}

long long gcd(long long x1, long long x2) {
  long long m = max(x1, x2);
  long long n = min(x1, x2);

  while (n != 0) {
    long long tmp = n;
    n = m % n;
    m = tmp;
  }
  return m;
}

long long lcm(long long x1, long long x2) {
  long long n_gcd = gcd(x1, x2);
  return (x1 / n_gcd) * x2;
}

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

  while (scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3),
         a1 || a2 || a3 || m1 || m2 || m3) {
    long long count1 = solve((long long)a1, (long long)m1);
    long long count2 = solve((long long)a2, (long long)m2);
    long long count3 = solve((long long)a3, (long long)m3);

    printf("%lld\\n", lcm(lcm(count1, count2), count3));
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

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

  while (scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3),
         a1 || a2 || a3 || m1 || m2 || m3) {
    int x, y, z;

    x = y = z = 1;
    x = (a1 * x) % m1;
    y = (a2 * y) % m2;
    z = (a3 * z) % m3;

    int count;
    for (count = 1; x != 1 || y != 1 || z != 1; count++) {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
    }

    printf("%d\\n", count);
  }
}
Problem p00114 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)

namespace std {
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }
} // namespace std

int main() {
  long long cyc[3], a[3], m[3];

#define in(i) , &a[i], &m[i]
#define o(i) | a[i] | m[i]

  while (scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)), 0 o(0) o(1) o(2)) {
    rep(i, 3) {
      cyc[i] = 0;
      int cnt = 1, k = 1;
      while (1) {
        k *= a[i];
        k %= m[i];
        if (k == 1) {
          cyc[i] = cnt;
          break;
        }
        cnt++;
      }
    }
    cout << lcm(lcm(cyc[0], cyc[1]), cyc[2]) << endl;
  }
}

Error: -8

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)

namespace std {
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }
} // namespace std

int main() {
  long long cyc[3], a[3], m[3];

#define in(i) , &a[i], &m[i]
#define o(i) | a[i] | m[i]

  while (scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)), 1 o(0) o(1) o(2)) {
    rep(i, 3) {
      cyc[i] = 0;
      int cnt = 1, k = 1;
      while (1) {
        k *= a[i];
        k %= m[i];
        if (k == 1) {
          cyc[i] = cnt;
          break;
        }
        cnt++;
      }
    }
    cout << lcm(lcm(cyc[0], cyc[1]), cyc[2]) << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long int lli;

lli gcd(lli x, lli y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

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

lli func(lli c[3]) {
  lli res = 1;
  for (int i = 0; i < 3; ++i)
    res = lcm(res, c[i]);

  return res;
}

int main() {
  lli s, t, u, a[3], m[3], c[3];

  while (1) {
    t = 0;
    for (int i = 0; i < 3; ++i) {
      cin >> a[i] >> m[i];
      t += (a[i] + m[i]);
      c[i] = 1;
    }
    if (t == 0)
      break;

    for (int i = 0; i < 3; ++i) {
      t = a[i] % m[i];
      while (t != 1) {
        c[i]++;
        t = (t * a[i]) % m[i];
      }
    }

    cout << func(c) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(int argc, char **argv) {
  int a1, a2, a3;
  int m1, m2, m3;
  int x, y, z;
  int c;

  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 == 0) {
      break;
    }
    x = a1 % m1;
    y = a2 % m2;
    z = a3 % m3;
    c = 1;

    while (!(x == 1 && y == 1 && z == 1)) {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
      c++;
    }

    cout << c << endl;
  }

  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) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

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

int a1, a2, a3, m1, m2, m3;
int main() {
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || m1 || a2 || m2 || a3 || m3) {
    int t = 1, c = 0, cx, cy, cz;
    do {
      t = t * a1 % m1;
      c++;
    } while (t != 1);
    cx = c;
    c = 0;
    do {
      t = t * a2 % m2;
      c++;
    } while (t != 1);
    cy = c;
    c = 0;
    do {
      t = t * a3 % m3;
      c++;
    } while (t != 1);
    cz = c;

    cout << lcm(lcm(cx, cy), cz) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int a1, a2, a3, m1, m2, m3;
int main() {
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || m1 || a2 || m2 || a3 || m3) {
    int x = 1, y = 1, z = 1;
    int cnt = 0;
    do {
      x = a1 * x % m1;
      y = a2 * y % m2;
      z = a3 * z % m3;
      cnt++;
    } while (!(x == 1 && y == 1 && z == 1));

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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

long long gcd(long long m, long long n) {
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      swap(m, n);
    n = n - m;
  }
  return m;
}

long long lcm(long long m, long long n) {
  if ((0 == m) || (0 == n))
    return 0;

  return m / gcd(m, n) * n;
}

int a1, m1, a2, m2, a3, m3;

int main() {

  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, c1, c2, c3;
    x = 1;
    y = 1;
    z = 1;
    c1 = 0;
    c2 = 0;
    c3 = 0;

    while (!(c1 != 0 && x == 1)) {
      c1++;
      x = a1 * x % m1;
    }
    while (!(c2 != 0 && y == 1)) {
      c2++;
      y = a2 * y % m2;
    }
    while (!(c3 != 0 && z == 1)) {
      c3++;
      z = a3 * z % m3;
    }

    // cout<<c1<<c2<<c3<<endl;
    cout << lcm(lcm(c1, c2), c3) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

int a1, m1, a2, m2, a3, m3;

int main() {
  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, count;
    x = 1;
    y = 1;
    z = 1;
    count = 0;

    x = (a1 * x) % m1;
    y = (a2 * y) % m2;
    z = (a3 * z) % m3;
    count++;

    while (!(x == 1 && y == 1 && z == 1)) {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
      count++;
    }

    cout << count << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

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

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  long long a[3] = {0}, m[3];
  while (cin >> a[0] >> m[0], a[0]) {
    rep(i, 2) cin >> a[1 + i] >> m[1 + i];

    int d[3] = {0};

    rep(i, 3) {
      int pos = 1;
      do {
        pos = (a[i] * pos) % m[i];
        d[i]++;
      } while (pos != 1);
    }
    cout << (long long)lcm(lcm(d[0], d[1]), d[2]) << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b, c, d, e, f, r, x, y, z;
  while (cin >> a >> b >> c >> d >> e >> f, a) {
    x = y = z = 1;
    r = 0;
    do {
      x = a * x % b;
      y = c * y % d;
      z = e * z % f;
      r++;
    } while (x != 1 || y != 1 || z != 1);
    cout << r << endl;
  }
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

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

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

int main() {
  int a[3], m[3], p[3] = {1, 1, 1}, c[3] = {0};
  while (scanf("%d%d%d%d%d%d", a, m, a + 1, m + 1, a + 2, m + 2), a[0]) {
    memset(c, 0, sizeof(c));
    for (int i = 0; i < 3; i++) {
      do {
        p[i] = a[i] * p[i] % m[i];
        c[i]++;
      } while (p[i] != 1);
    }
    printf("%lld\\n", lcm(lcm(c[0], c[1]), c[2]));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

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

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

int main() {
  int a[3], m[3], p[3] = {1, 1, 1}, c[3] = {0};
  while (scanf("%d%d%d%d%d%d", a, m, a + 1, m + 1, a + 2, m + 2), a[0]) {
    memset(c, 0, sizeof(c));
    for (int i = 3; i; i--) {
      do {
        p[i] = a[i] * p[i] % m[i];
        c[i]++;
      } while (p[i] != 1);
    }
    printf("%lld\\n", lcm(lcm(c[0], c[1]), c[2]));
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

long long int Lcm(long long int a, long long int b) {
  return a / Gcd(a, b) * b;
}

int main() {
  int a[3];
  int m[3];
  while (true) {
    for (int i = 0; i < 3; i++)
      cin >> a[i] >> m[i];
    if (a[0] == 0 && a[1] == 0 && a[2] == 0 && m[0] == 0 && m[1] == 0 &&
        m[2] == 0)
      return 0;

    int x = 1;
    int y = 1;
    int z = 1;
    long long int countX = 0;
    long long int countY = 0;
    long long int countZ = 0;

    while (true) {
      countX++;
      x = (a[0] * x) % m[0];
      if (x == 1)
        break;
    }
    while (true) {
      countY++;
      y = (a[1] * y) % m[1];
      if (y == 1)
        break;
    }
    while (true) {
      countZ++;
      z = (a[2] * z) % m[2];
      if (z == 1)
        break;
    }

    long long int lcm = Lcm(countX, countY);
    lcm = Lcm(lcm, countZ);
    cout << lcm << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a[3];
  int m[3];
  while (true) {
    for (int i = 0; i < 3; i++)
      cin >> a[i] >> m[i];
    if (a[0] == 0 && a[1] == 0 && a[2] == 0 && m[0] == 0 && m[1] == 0 &&
        m[2] == 0)
      return 0;

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

  return 0;
}