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

#include <iostream>
#include <string>
using namespace std;
int main() {
  long p, q, i, j, k, d[80];
  string s;
  while (cin >> p >> q) {
    i = 0;
    d[i++] = p;
    s = "";
    for (p *= 10;; p *= 10) {
      j = p / q;
      p %= q;
      d[i] = p;
      s += j + 48;
      if (p == 0)
        break;
      for (k = 0; k < i; k++)
        if (d[k] == p)
          break;
      if (k == i)
        i++;
      else
        break;
    }
    cout << s << endl;
    if (p != 0) {
      for (j = 0; j < k; j++)
        cout << " ";
      for (; j < i; j++)
        cout << "^";
      cout << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
  long p, q, i, j, f, d[10], l, i2;
  string s;
  stringstream ss;
  while (cin >> p) {
    cin >> q;
    f = 0;
    s = "";
    for (i = 0; i < 10; i++)
      d[i] = 0;
    for (p *= 10;; p *= 10) {
      j = p / q;
      p %= q;
      if (p == 0)
        f = 1;
      if (d[j] == p)
        break;
      ss.str("");
      ss << j;
      s += ss.str();
      d[j] = p;
      if (f == 1)
        break;
    }
    if (f == 0) {
      l = s.size();
      if (s[l - 1] == j + 48) {
        cout << s << endl;
        for (; l > 1; l--)
          cout << ' ';
        cout << '^' << endl;
      } else {
        i = s.find_last_of(j + 48);
        i2 = s.find_last_not_of('0');
        cout << s.substr(0, i2 + 1) << endl;
        for (j = 0; j < i + i2 - l + 1; j++)
          cout << ' ';
        for (; j < l - i + 2; j++)
          cout << '^';
        cout << endl;
      }
    } else {
      cout << s << endl;
    }
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int h, i, j;
  int p, q;
  while (cin >> p >> q) {
    int a[90], b[90];
    set<pair<int, int>> c;
    h = -1;
    for (i = 0;; i++) {
      p *= 10;
      a[i] = p / q;
      b[i] = p = p % q;
      if (b[i]) {
        if (c.count(make_pair(a[i], b[i]))) {
          for (h = 0; a[h] != a[i] || b[h] != b[i]; h++)
            ;
          break;
        } else {
          c.insert(make_pair(a[i], b[i]));
        }
      } else {
        i++;
        break;
      }
    }
    for (j = 0; j < i; j++)
      cout << a[j];
    cout << endl;
    if (h != -1) {
      for (j = 0; j < h; j++)
        cout << " ";
      for (; j < i; j++)
        cout << "^";
      cout << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int i, j;
  int p, q, *a, b[10];
  for (;;) {
    p = 0;
    q = 0;
    cin >> p >> q;
    if (p == 0 && q == 0)
      break;
    a = (int *)malloc(q * sizeof(int));
    memset(a, -1, q * sizeof(int));
    memset(b, -1, sizeof(b));
    for (i = 0; p; i++) {
      if (*(a + p * 10 % q) >= 0 && b[p * 10 / q] >= 0)
        break;
      *(a + p * 10 % q) = i;
      b[p * 10 / q] = i;
      cout << p * 10 / q;
      p = p * 10 % q;
    }
    cout << endl;
    if (p) {
      for (j = 0; j < *(a + p * 10 % q); j++)
        cout << " ";
      for (; j < i; j++)
        cout << "^";
      cout << endl;
    }
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
map<int, bool> EX;
int p, q;
bool found;
int debug;

int dfs(int v, int &cnt) {
  if (v == 0)
    return v;
  if (EX[v])
    return v;
  EX[v] = true;

  int va = v / q;
  if (va >= 9)
    va = 9;
  cout << va;
  int res = dfs((v - q * va) * 10, cnt);
  debug++;
  if (found)
    return res;
  if (v == res)
    found = true;
  cnt++;
  return res;
}

int main() {
  while (cin >> p >> q) {
    debug = 0;
    found = false;
    EX.clear();
    int cnt = 0;
    int res = dfs(p * 10, cnt);
    cout << endl;
    if (!res)
      continue;
    rep(i, debug - cnt) cout << ' ';
    rep(i, cnt) cout << '^';
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
using namespace std;
typedef unsigned long long ll;
typedef pair<ll, int> P;
ll p, q;
bool ch;
bool flag;
string result;
string result2;
map<ll, bool> index;
int debug;

P dfs(ll value) {
  if (!value) {
    ch = true;
    flag = false;
    return P(-(1 << 29), 0);
  }
  // cout<< "value = " << value << " : " << result << endl;
  assert(value >= 0);
  if (index[value])
    return P(value, 0);
  index[value] = true;
  if (value < q) {
    result += "0";
    P res = dfs(value * 10);
    if (flag)
      res.second++;
    if (res.first == value)
      flag = false;
    return res;
  } else {
    ll v = value / q;
    if (v >= 9)
      v = 9;

    result += (char)('0' + v);

    P res = dfs(10 * (value - q * v));
    if (flag)
      res.second++;
    if (res.first == value)
      flag = false;
    return res;
  }
}

int main() {

  while (cin >> p >> q) {
    ch = false;
    debug = 0;
    result2.clear();
    result.clear();
    index.clear();
    flag = true;
    P pes = dfs(p);
    reverse(result2.begin(), result2.end());
    cout << result.substr(1) << endl;
    if (!ch) {
      assert((int)result.size() <= 83);
      // string res = result.substr(1);
      // cout << result2.substr(1) << endl;
      string resu2;
      rep(i, result.size() - pes.second - 1) resu2 += " ";
      rep(i, pes.second) resu2 += "^";
      resu2 += string(result.size() - pes.second - 1, ' ');

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

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

std::string func(std::string str, int x) {
  std::string ret;
  while (--x) {
    ret += str;
  }
  return ret;
}
int main() {
  std::vector<int>::iterator Result;
  std::vector<int> mod;
  int p, q;
  while (std::cin >> p >> q) {
    mod.clear();
    bool End = false;
    while (1) {
      if (p == 0)
        break;
      std::cout << (p * 10) / q;
      mod.push_back(p);
      if (mod.size() > 80)
        break;
      if ((Result = std::find(mod.begin(), mod.end(), (p * 10) % q)) !=
          mod.end()) {
        End = true;
        break;
      }
      p = (p * 10) % q;
    }
    std::cout << std::endl;
    if (End)
      std::cout << func(" ", Result - mod.begin() + 1)
                << func("^", mod.size() - (Result - mod.begin()) + 1)
                << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

std::string func(std::string str, int x) {
  std::string ret;
  while (--x) {
    ret += str;
  }
  return ret;
}
int main() {
  while (1) {
    int p, q;
    bool End = false;
    std::vector<int>::iterator Result;
    std::vector<int> mod;
    std::cin >> p >> q;
    while (1) {
      if (p == 0)
        break;
      std::cout << (p * 10) / q;
      mod.push_back(p);
      if (mod.size() > 80)
        break;
      if ((Result = std::find(mod.begin(), mod.end(), (p * 10) % q)) !=
          mod.end()) {
        End = true;
        break;
      }
      p = (p * 10) % q;
    }
    std::cout << std::endl;
    if (End)
      std::cout << func(" ", Result - mod.begin() + 1)
                << func("^", mod.size() - (Result - mod.begin()) + 1)
                << std::endl;
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Runtime Error

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

void drawdown(int a, int size) {
  int i = 0;
  for (; i < a; i++)
    std::cout << " ";
  for (; i < size; i++)
    std::cout << "^";
  std::cout << std::endl;
}

int main() {

  long long p, q;
  while (std::cin >> p >> q) {
    std::vector<long long> r;
    r.push_back(p);

    while (true) {

      if (p / q == 0)
        p *= 10;
      std::cout << p / q;
      p %= q;
      if (p == 0) {
        std::cout << std::endl;
        break;
      }
      std::vector<long long>::iterator it = std::find(r.begin(), r.end(), p);
      if (it != r.end()) {
        std::cout << std::endl;
        drawdown(it - r.begin(), r.size());
        break;
      }
      r.push_back(p);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void drawdown(int a, int size) {
  int i = 0;
  for (; i < a; i++)
    std::cout << " ";
  for (; i < size; i++)
    std::cout << "^";
  std::cout << std::endl;
}

int main() {

  long long p, q;
  // while (std::cin >> p >> q){
  std::vector<long long> r;
  r.push_back(0);

  while (true) {

    if (p / q == 0)
      p *= 10;
    std::cout << p / q;
    p %= q;
    std::vector<long long>::iterator it = std::find(r.begin(), r.end(), p);
    if (it != r.end()) {
      std::cout << std::endl;
      if (p == 0)
        break;
      else {
        drawdown(it - r.begin(), r.size());
        break;
      }
    }
    r.push_back(p);
  }
  //}
  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);
long long int lcm(long long int a, long long int b);

long long int gcd(long long int a, long long int b) {
  if (b == 0)
    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() {
  long long int a1, a2, a3, m1, m2, m3;
  long long int x, y, z;
  long long int count;

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    x = (1 * a1) % m1;
    y = (1 * a2) % m2;
    z = (1 * a3) % m3;
    count = 1;
    while (x != 1) {
      x = (x * a1) % m1;
      count++;
    }
    x = count;
    count = 1;
    while (y != 1) {
      y = (y * a2) % m2;
      count++;
    }
    y = count;
    count = 1;
    while (z != 1) {
      z = (z * a3) % m3;
      count++;
    }
    z = count;
    cout << lcm(x, lcm(y, z)) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

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

typedef long long int lli;

lli lcm(lli a, lli b) { return a * b / __gcd(a, b); }

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

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 | m1 | a2 | m2 | a3 | m3) {

    lli x, y, z;
    x = a1 % m1;
    y = a2 % m2;
    z = a3 % m3;

    lli X = 1, Y = 1, Z = 1;
    while (x != 1)
      x = x * a1 % m1, X++;
    while (y != 1)
      y = y * a2 % m2, Y++;
    while (z != 1)
      z = z * a3 % m3, Z++;

    cout << lcm(X, lcm(Y, Z)) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 | m1 | a2 | m2 | a3 | m3) {

    int x, y, z;
    x = a1 % m1;
    y = a2 % m2;
    z = a3 % m3;

    int ans = 1;

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

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

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

using namespace std;
typedef long long ll;
/** Problem0114 : Electro-Fly **/
ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  while (b) {
    ll tmp = a % b;
    a = b;
    b = tmp;
  }

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

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

    x = a1 % m1;
    y = a2 % m2;
    z = a3 % m3;

    long long nx, ny, nz;
    for (nx = 1; x != 1; nx++)
      x = (a1 * x) % m1;

    for (ny = 1; y != 1; ny++)
      y = (a2 * y) % m2;

    for (nz = 1; z != 1; nz++)
      z = (a3 * z) % m3;

    cout << lcm(lcm(nx, ny), nz) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

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

using namespace std;
/** Problem0114 : Electro-Fly **/
int main() {
  int a1, m1, a2, m2, a3, m3, x, y, z, i;
  while (1) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0)
      break;

    x = a1 % m1;
    y = a2 % m2;
    z = a3 % m3;
    i = 1;
    while (x != 1 || y != 1 || z != 1) {
      x = (a1 * x) % m1;
      y = (a2 * y) % m2;
      z = (a3 * z) % m3;
      i++;
    }
    cout << i << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long gcd(long long a, long long 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 g = a > b ? gcd(a, b) : gcd(b, a);
  return a / g * b;
}
int main() {
  int a1, m1, a2, m2, a3, m3, x, y, z;
  long long i, a, b, c;
  for (;;) {
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 == 0)
      break;
    x = y = z = 1;
    a = b = c = 0;
    i = 0;
    for (;;) {
      if (a == 0)
        x = a1 * x % m1;
      if (b == 0)
        y = a2 * y % m2;
      if (c == 0)
        z = a3 * z % m3;
      i++;
      if (a == 0 && x == 1)
        a = i;
      if (b == 0 && y == 1)
        b = i;
      if (c == 0 && z == 1)
        c = i;
      if (a != 0 && b != 0 && c != 0) {
        cout << lcm(a, lcm(b, c)) << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#include <iostream>

using namespace std;

typedef long long ll;

ll x, y, z;
ll m[3], a[3];

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

int main() {

  while (1) {
    int sum = 0;

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

    if (sum == 0)
      break;

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

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

    ll tmp = (t[0] * t[1]) / GCD(t[0], t[1]);
    ll ans = (tmp * t[2]) / GCD(tmp, t[2]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

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, y, z;
    x = y = z = 1;
    int rev = 0;
    while (1) {
      rev++;
      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 << rev << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

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

unsigned long long int GCD(unsigned long long int a, unsigned long long int b) {
  unsigned long long int less = min(a, b), gcd = 1, i;
  for (i = 1; i <= less; ++i) {
    if (a % i == 0 && b % i == 0)
      gcd = i;
  }
  return gcd;
}

unsigned long long int LCM(unsigned long long int a, unsigned long long int b) {
  return a * b / GCD(a, b);
}

unsigned long long int LCM(unsigned long long int a, unsigned long long int b,
                           unsigned long long int c) {
  return LCM(a, LCM(b, c));
}

int main() {
  unsigned long long int a1, a2, a3, m1, m2, m3;
  unsigned long long int x, y, z;
  unsigned long long int l, m, n;

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,
         a1 || a2 || a3 || m1 || m2 || m3) {
    x = y = z = 1;
    l = m = n = 0;

    do {
      x = (a1 * x) % m1;
      ++l;
    } while (x - 1);
    do {
      y = (a2 * y) % m2;
      ++m;
    } while (y - 1);
    do {
      z = (a3 * z) % m3;
      ++n;
    } while (z - 1);

    cout << LCM(l, m, n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 | a2 | a3 | m1 | m2 | m3) {
    x = y = 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);

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
typedef long long int Int;
const int N = 3;

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

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

int main() {
  Int A[N], M[N];
  while (true) {
    REP(i, N) cin >> A[i] >> M[i];
    if (A[0] == 0)
      break;
    Int v[N], num[N];
    REP(i, N) {
      Int cnt = 1;
      v[i] = A[i] % M[i];
      while (v[i] != 1) {
        v[i] = A[i] * v[i] % M[i];
        ++cnt;
      }
      num[i] = cnt;
    }
    cout << lcm(num[0], lcm(num[1], num[2])) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int N = 3;

int A[N], M[N];

int main() {
  while (true) {
    REP(i, N) cin >> A[i] >> M[i];
    if (A[0] == 0)
      break;
    int ans = 1, x = A[0] % M[0], y = A[1] % M[1], z = A[2] % M[2];
    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];
      ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00114 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdint.h>
#include <stdio.h>

unsigned int GCD(unsigned int a, unsigned int b) {
  while (1) {
    unsigned int r = a % b;
    if (r == 0)
      return b;
    else {
      a = b;
      b = r;
    }
  }
}

uint64_t LCM(uint64_t a, uint64_t b) {
  uint64_t gcd = GCD(a, b);
  return (a / gcd) * b;
}

int main(void) {
  while (1) {
    uint64_t moves = 0;
    int x = 1, y = 1, z = 1;
    unsigned int a1, m1, a2, m2, a3, m3;
    uint64_t lcm1 = 0, lcm2 = 0, lcm3 = 0;

    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;
    while (1) {
      x = (a1 * x) % m1;
      lcm1++;
      if (x == 1)
        break;
    }
    while (1) {
      y = (a2 * y) % m2;
      lcm2++;
      if (y == 1)
        break;
    }
    while (1) {
      z = (a3 * z) % m3;
      lcm3++;
      if (z == 1)
        break;
    }
    moves = LCM(lcm1, LCM(lcm2, lcm3));
    printf("%llu\\n", moves);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int GCD(int a, int b) {
  while (true) {
    int r = a % b;
    if (r == 0)
      return b;
    else {
      a = b;
      b = r;
    }
  }
}
int LCM(int a, int b) {
  int n = 1;
  int gcd = GCD(a, b);
  while (true) {
    if ((gcd * n) % a == 0 && (gcd * n) % b == 0)
      return gcd * n;
    else
      n++;
  }
}

int main(void) {
  while (true) {
    int moves = 0;
    int x = 1, y = 1, z = 1;
    int a1, m1, a2, m2, a3, m3;
    int lcm1 = 0, lcm2 = 0, lcm3 = 0;

    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0)
      break;
    while (true) {
      x = (a1 * x) % m1;
      lcm1++;
      if (x == 1)
        break;
    }
    while (true) {
      y = (a2 * y) % m2;
      lcm2++;
      if (y == 1)
        break;
    }
    while (true) {
      z = (a3 * z) % m3;
      lcm3++;
      if (z == 1)
        break;
    }
    moves = LCM(lcm1, LCM(lcm2, lcm3));
    cout << moves << '\\n';
  }
  return 0;
}