alexjercan/bugnet huggingface dataset:
Problem p01803 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

char code[50][55];
int max;

char ntoc(char name[], int nb) {
  int i = 1, j = 1;
  code[nb][0] = name[0];
  while (name[i] != '\\n') {
    if (name[i - 1] == 'a' || name[i - 1] == 'i' || name[i - 1] == 'u' ||
        name[i - 1] == 'e' || name[i - 1] == 'o') {
      code[nb][j] = name[i];
      j++;
    }
    i++;
  }
  if (j > max)
    max = j;
  code[nb][j] = '\\0';
}

int main() {
  int n, i, k, l, m;
  char name[55];
  scanf("%d", &n);
  getchar();
  while (n) {
    max = 0;
    for (i = 0; i < n; i++) {
      fgets(name, 55, stdin);
      ntoc(name, i);
    }
    for (k = 1; k <= max; k++) {
      for (l = 0; l < n; l++)
        for (m = l + 1; m < n; m++)
          if (!strncmp(code[l], code[m], k))
            goto NEXT;
      printf("%d\\n", k);
      break;
    NEXT:
      printf("");
    }
    if (k == max + 1)
      printf("-1\\n");
    scanf("%d", &n);
    getchar();
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

char code[50][51];
int max;

char ntoc(char name[], int nb) {
  int i = 1, j = 1;
  code[nb][0] = name[0];
  while (name[i] != '\\n') {
    if (name[i - 1] == 'a' || name[i - 1] == 'i' || name[i - 1] == 'u' ||
        name[i - 1] == 'e' || name[i - 1] == 'o') {
      code[nb][j] = name[i];
      j++;
    }
    i++;
  }
  if (j > max)
    max = j;
  code[nb][j] = '\\0';
}

int main() {
  int n, i, k, l, m;
  char name[51];
  scanf("%d", &n);
  getchar();
  while (n) {
    max = 0;
    for (i = 0; i < n; i++) {
      fgets(name, 51, stdin);
      ntoc(name, i);
    }
    for (k = 1; k <= max; k++) {
      for (l = 0; l < n; l++)
        for (m = l + 1; m < n; m++)
          if (!strncmp(code[l], code[m], k))
            goto NEXT;
      printf("%d\\n", k);
      break;
    NEXT:
      printf("");
    }
    if (k == max + 1)
      printf("-1\\n");
    scanf("%d", &n);
    getchar();
  }
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1180
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

int const MOD = 1e9 + 7;

int GCD(int x, int y) {
  if (y == 0)
    return x;
  return GCD(y, x % y);
}
int main(void) {
  ll p, q;
  cin >> p >> q;

  int d = GCD(p, q);
  p /= d;
  q /= d;

  int ans = 1;
  for (int i = 2; i <= sqrt(q); ++i) {
    if (q % i == 0) {
      ans *= i;
      while (1) {
        if (q % i != 0)
          break;
        q /= i;
      }
    }
  }
  ans *= q;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1180
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

int const MOD = 1e9 + 7;

int GCD(int x, int y) {
  if (y == 0)
    return x;
  return GCD(y, x % y);
}
int main(void) {
  ll p, q;
  cin >> p >> q;

  int d = GCD(p, q);
  p /= d;
  q /= d;

  bool prime_check = true;
  for (ll i = 2; i * i <= q; ++i) {
    if (q % i == 0)
      prime_check = false;
  }
  if (prime_check) {
    cout << q << endl;
    return 0;
  }

  for (ll i = 2;; ++i) {
    ll t = q;
    for (;;) {
      if (t == 1) {
        cout << i << endl;
        return 0;
      }
      if (t % i != 0)
        break;
      t /= i;
    }
  }
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;

int solve();

ll gcd(ll p, ll q) {
  if (q == 0) {
    return p;
  }
  return gcd(q, p % q);
}

int main(void) {
  while (solve()) {
  }
  return 0;
}

int solve() {

  ll p, q, g;

  cin >> p >> q;

  g = gcd(max(p, q), min(p, q));

  //	約分
  p = p / g;
  q = q / g;

  //	答えはqの素因数をすべて掛け合わせたもの
  //	sqrt{q}までの素数を調べる
  const ll M = (1e+9) + 1;
  const ll N = sqrt(M) + 1;
  ll ans;

  // cout << M << " " << N << endl;
  //	エラトステネスの篩
  vector<ll> primes;
  primes.reserve(5000);
  vector<bool> isPrime(M, true);
  isPrime[0] = false;
  isPrime[1] = false;

  for (ll i = 2; i < N; i++) {
    if (isPrime[i] == false) {
      continue;
    } //	合成数なら
    primes.push_back(i);
    for (ll j = i; i * j < N; j++) {
      isPrime[i * j] = false;
    }
  }

  ll qc = q;
  set<ll> facts;

  for (auto p : primes) {
    if (qc % p == 0) {
      while (qc % p == 0)
        qc /= p;
      if (qc % p > 0) {
        facts.insert(p);
      }
    }
  }
  ans = qc;
  for (auto itr = facts.begin(); itr != facts.end(); ++itr) {
    ans *= (*itr);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:


#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;

int solve();

ll gcd(ll p, ll q) {
  if (q == 0) {
    return p;
  }
  return gcd(q, p % q);
}

int main(void) {
  while (solve()) {
  }
  return 0;
}

int solve() {

  ll p, q, g;

  cin >> p >> q;

  g = gcd(max(p, q), min(p, q));

  //	約分
  p = p / g;
  q = q / g;

  //	エラトステネスの篩

  const ll M = sqrt(10e16);
  std::vector<bool> isPrime(M, true);
  isPrime[1] = false;

  for (ll i = 2; i < M; i++) {
    if (isPrime[i] == false) {
      continue;
    }
    for (ll j = 2; i * j < M; j++) {
      isPrime[i * j] = false;
    }
  }

  ll ans = 1;

  for (ll i = 2; i < M; i++) {
    if (isPrime[i] == true && q % i == 0) {
      ans *= i;
    }
  }

  if (ans == 1 && q > 1) {
    ans = q;
  }

  cout << ans << endl;

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

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

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

int main() {
  int p, q;
  cin >> p >> q;
  p = gcd(p, q);
  q /= p;
  set<int> s;
  for (int i = 2; i <= q;) {
    if (i * i > q) {
      i = q;
    }
    if (q % i == 0) {
      q /= i;
      s.insert(i);
    } else {
      i++;
    }
  }
  int r = 1;
  for (auto it = begin(s); it != end(s); it++) {
    r *= *it;
  }
  cout << r << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int p, q;
  cin >> p >> q;
  p = gcd(p, q);
  q /= p;
  set<int> s;
  for (int i = 2; i <= q;) {
    if (q % i == 0) {
      q /= i;
      s.insert(i);
    } else {
      i++;
    }
  }
  int r = 1;
  for (auto it = begin(s); it != end(s); it++) {
    r *= *it;
  }
  cout << r << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;
typedef long long ll;

int P, Q;
bool p[1000000];
vector<int> v;

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

bool prime(int n) {
  REP(i, 0, v.size()) if (n % v[i] == 0) return false;
  return true;
}

ll solve() {
  ll ans = 1;
  REP(i, 0, v.size()) if (Q % v[i] == 0) {
    ans *= v[i];
    while (Q % v[i] == 0)
      Q /= v[i];
  }
  if (prime(Q))
    ans *= Q;
  return ans;
}

int main(void) {
  REP(i, 0, 1000000) p[i] = true;
  p[0] = p[1] = false;
  REP(i, 0, 1000000)
  if (p[i]) for (int j = i * 2; j < 1000000; j += i) p[j] = false;
  REP(i, 0, 1000000) if (p[i]) v.push_back(i);

  cin >> P >> Q;
  Q = Q / gcd(P, Q);
  cout << solve() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;
typedef long long ll;

int P, Q;
bool p[100000000];
vector<int> v;

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

ll solve() {
  ll ans = 1;
  REP(i, 0, v.size()) if (Q % v[i] == 0) ans *= v[i];
  if (ans == 1)
    ans *= Q;
  return ans;
}

int main(void) {
  REP(i, 0, 100000000) p[i] = true;
  p[0] = p[1] = false;
  REP(i, 0, 100000000)
  if (p[i]) for (int j = i * 2; j < 100000000; j += i) p[j] = false;
  REP(i, 0, 100000000) if (p[i]) v.push_back(i);

  cin >> P >> Q;
  Q = Q / gcd(P, Q);
  cout << solve() << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;

signed main(void) {
  int i, j;
  int p, q;
  cin >> p >> q;
  q = q / __gcd(p, q);
  ll ans = 1;
  for (i = 2; i * i <= q; i++)
    if (q % i == 0) {
      ans *= i;
      while (q % i == 0)
        q /= i;
    }
  cout << ans * q << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef pair<int, int> pii;

int main(void) {
  int i, j;
  int p, q;
  cin >> p >> q;
  q = q / __gcd(p, q);
  ll ans = 1;
  for (i = 2; i <= q; i++)
    if (q % i == 0) {
      ans *= i;
      while (q % i == 0)
        q /= i;
    }
  cout << ans << endl;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long p, q;
  cin >> p >> q;
  q = q / __gcd(p, q);
  int q_q = q;
  int ans = 1;
  for (int i = 2; i <= q / 2; i++) {
    if (q_q % i == 0) {
      q_q /= i;
      ans *= i;
      while (q_q % i == 0) {
        q_q /= i;
      }
    }
  }
  if (ans == 1) {
    ans = q;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long p, q;
  cin >> p >> q;
  q = q / __gcd(p, q);

  int ans = 1;
  for (int i = 2; i <= q; i++) {
    if (q % i == 0) {
      q /= i;
      ans *= i;
      while (q % i == 0) {
        q /= i;
      }
    }
  }
  cout << ans << endl;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {

  int p, q, use;
  int so[40000] = {0};

  cin >> p >> q;
  use = q;
  int a;
  while (q % p) {
    a = q % p;
    q = p;
    p = a;
  }
  use /= p;
  vector<int> sonaka;
  for (int i = 2; i < 40000; i++)
    if (!so[i]) {
      sonaka.push_back(i);
      for (int j = i * 2; j < 40000; j += i)
        so[j] = 1;
    }
  int result = 1;
  for (int i = 0; sonaka[i] <= use && i < sonaka.size(); i++)
    if (use % sonaka[i] == 0) {
      result *= sonaka[i];
      while (use % sonaka[i] == 0)
        use /= sonaka[i];
    }
  cout << result * use << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int p, q;
  cin >> p >> q;
  int x = 1, a = p, b = q;
  for (int i = 2; i <= p; ++i) {
    if (a % i == 0 && b % i == 0)
      while (a % i == 0 && b % i == 0) {
        x *= i;
        a /= i, b /= i;
      }
  }
  cout << q / x << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  int p, q;
  cin >> p >> q;
  q /= GCD(p, q);
  map<int, int> mpa;
  for (int i = 2; i * i <= q; i++) {
    while (q % i == 0) {
      mpa[i]++;
      q /= i;
    }
  }
  if (q != 1)
    mpa[q]++;
  map<int, int>::iterator it;
  int ans = 1;
  for (it = mpa.begin(); it != mpa.end(); it++) {
    ans *= it->first;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int p, q;
  cin >> p >> q;
  q /= GCD(p, q);
  int ans = q;
  set<int> sta;
  for (int i = 2; i * i <= q; i++) {
    if (q % i == 0)
      sta.insert(i);
  }
  set<int>::iterator it;
  for (it = sta.begin(); it != sta.end(); it++) {
    int num = 1;
    while (num < q)
      num *= (*it);
    if (num == q && (*it) < ans)
      ans = (*it);
  }
  cout << ans << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;
int p, q, ans = 1, j;

int so(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 1;
  return 0;
}

int main() {
  cin >> p >> q;
  if (so(q)) {
    for (int i = 2; q >= i; i++) {
      for (j = 0; q % i == 0; j++) {
        q /= i;
        if (p % i == 0)
          p /= i, j--;
      }
      if (j == 0)
        continue;
      /*if(p%(i*j)==0)p/=(i*j);
        else*/
      ans *= i;
    }
    cout << ans << endl;
  } else
    cout << q << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 1e9
int main() {
  int p, q;
  cin >> p >> q;
  for (int i = 2; i < MAX; i++) {
    if (q % i == 0 && p % (q / i) == 0) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define Get(t, i) get<(i)>((t))
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for (auto &a : b)

const int inf = 1 << 25;
const ll INF = 1LL << 55;

int main() {
  ll p, q;
  cin >> p >> q;
  vector<int> factor;
  q = q / __gcd(p, q);
  for (int x = 2; x * x <= q; x++) {
    if (q % x == 0) {
      factor.push_back(x);
      while (q % x == 0)
        q /= x;
    }
  }
  if (q != 1)
    factor.push_back(q);
  ll ans = 1;
  rep(i, factor.size()) ans *= factor[i];
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define Get(t, i) get<(i)>((t))
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for (auto &a : b)

const int inf = 1 << 25;
const ll INF = 1LL << 55;

int main() {
  ll p, q;
  cin >> p >> q;
  vector<int> factor;
  q = q / __gcd(p, q);
  for (int x = 2; q != 1; x++) {
    if (q % x == 0) {
      factor.push_back(x);
      while (q % x == 0)
        q /= x;
    }
  }
  ll ans = 1;
  rep(i, factor.size()) ans *= factor[i];
  cout << ans << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define N 1000000000
typedef long long int ll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
queue<ll> q;
vector<bool> prime(N + 1, true);
void make(ll M) {
  prime[0] = prime[1] = false;
  ll i;
  for (i = 2; i * i <= M + 1; i++) {
    if (prime[i]) {
      ll j = 2;
      q.push(i);
      while (i * j < M + 1) {
        prime[i * j] = false;
        j++;
      }
    }
  }
  /*	for(;i<=M;i++){
                  if(prime[i])q.push(i);
          }*/
  return;
}
int main() {
  ll a, b;
  // cout<<"ok"<<endl;

  cin >> a >> b;
  // make(max(a,b));
  // cout<<"ok"<<endl;
  // cout<<b/gcd(a,b)<<endl;
  ll c = b / gcd(a, b);
  // cout<<c<<endl;
  ll ans = 1;
  ll ma = c;
  for (ll i = 2; i * i <= ma; i++) {
    if (c % i == 0) {
      ans *= i;
      while (c % i == 0)
        c /= i;
    }
  }
  /*while(!q.empty()){
          ll d = q.front();
  //	cout<<d<<endl;
  if(c%d==0){
          while(c%d==0)c/=d;
          ans*=d;
  }
  q.pop();
  }*/

  // cout<<ans<<' '<<c<<endl;
  ans *= c;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N 1000000000
typedef long long int ll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
queue<ll> q;
vector<bool> prime(N + 1, true);
void make(ll M) {
  prime[0] = prime[1] = false;
  ll i;
  for (i = 2; i * i <= M + 1; i++) {
    if (prime[i]) {
      ll j = 2;
      q.push(i);
      while (i * j < M + 1) {
        prime[i * j] = false;
        j++;
      }
    }
  }
  /*	for(;i<=M;i++){
                  if(prime[i])q.push(i);
          }*/
  return;
}
int main() {
  ll a, b;
  // cout<<"ok"<<endl;

  cin >> a >> b;
  make(max(a, b));
  // cout<<"ok"<<endl;
  // cout<<b/gcd(a,b)<<endl;
  ll c = b / gcd(a, b);
  // cout<<c<<endl;
  ll ans = 1;
  ll ma = c;
  while (!q.empty()) {
    ll d = q.front();
    //	cout<<d<<endl;
    if (c % d == 0) {
      while (c % d == 0)
        c /= d;
      ans *= d;
    }
    q.pop();
  }
  /*for(ll i=2;i<=c;i++){
  if(c%i==0){

          ans*=i;
          //cout<<i<<endl;
          while(c%i==0)c/=i;
          //cout<<i<<' '<<c<<' '<<ma<<endl;
  }

  }*/
  // cout<<c<<endl;
  ans *= c;
  cout << ans << endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> pi;

vector<pi> f(int n) {
  int t = n;
  vector<pi> ret;
  for (int i = 2; i * i <= n; ++i) {
    if (t % i == 0) {
      pi add(i, 0);
      while (t % i == 0) {
        ++add.se;
        t /= i;
      }
      ret.pb(add);
    }
  }
  if (t > 1)
    ret.pb(pi(t, 1));
  return ret;
}

int main() {
  int p, q;
  cin >> p >> q;

  int g = __gcd(p, q);
  p /= g;
  q /= g;

  vector<pi> d = f(q);
  // rep(i,d.size()) printf("%d: %d\\n", d[i].fi,d[i].se);

  ll ans = 1;
  rep(i, d.size()) ans *= d[i].fi;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

int main() {
  int p, q;
  cin >> p >> q;

  int g = __gcd(p, q);
  p /= g;
  q /= g;

  int ans = q;

  for (int i = 2; i * i <= q; ++i) {
    int t = i;
    while (t * i <= q)
      t *= i;
    if (t == q) {
      ans = i;
      break;
    }
  }

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int p, q;
  cin >> p >> q;
  int r = gcd(q, p);
  p /= r;
  q /= r;
  int ans = 1;
  for (int i = 2; i * i <= q; i++) {
    if (q % i != 0)
      continue;
    ans *= i;
    while (q % i == 0)
      q /= i;
  }
  ans *= q;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int p, q;
  cin >> p >> q;
  int r = gcd(q, p);
  p /= r;
  q /= r;
  int ans = 1;
  for (int i = 2; i <= q; i++) {
    if (q % i != 0)
      continue;
    ans *= i;
    while (q % i == 0)
      q /= i;
  }
  cout << ans << endl;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>

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

bool judgeprime(int n) {
  if (n == 1 || n % 2 == 0)
    return false;
  int i, j;
  j = (int)sqrt(n) + 1;
  bool k = true;
  for (i = 3; i < j; i += 2)
    if (n % i == 0) {
      k = false;
      break;
    }
  return k;
}

int nextprime(int prime) {
  prime++;
  while (judgeprime(prime++) == false)
    ;
  return prime - 1;
}

int main(void) {
  int p, q;
  scanf("%d%d", &p, &q);

  q /= gcd(p, q);

  int prime = 2, ans = 1;

  while (q > 1) {
    if (q % prime == 0) {
      ans *= prime;
      while (q % prime == 0)
        q /= prime;
    }
    if (judgeprime(q)) {
      ans *= q;
      break;
    }
    prime = nextprime(prime);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>

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

bool judgeprime(int n) {
  if (n == 1)
    return false;
  int i, j;
  j = (int)sqrt(n);
  for (i = 2; i < j; i++)
    if (n % i == 0)
      break;
  return i == j;
}

int nextprime(int prime) {
  prime++;
  while (judgeprime(prime++) == false)
    ;
  return prime;
}

int main(void) {
  int p, q;
  scanf("%d%d", &p, &q);

  q /= gcd(p, q);

  int prime = 2, ans = 1;

  while (q > 1) {
    if (q % prime == 0) {
      ans *= prime;
      while (q % prime == 0)
        q /= prime;
    }
    prime = nextprime(prime);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
int main() {
  int p, q;
  scanf("%d%d", &p, &q);
  q /= std::__gcd(p, q);
  long long ans = 1;
  for (int i = 2; i * i <= q; i++) {
    if (!(q % i)) {
      ans *= i;
      while (!(q % i))
        q /= i;
    }
  }
  printf("%lld\\n", ans * q);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  int p, q;
  scanf("%d%d", &p, &q);
  int x = gcd(q, p);
  p /= x;
  q /= x;
  for (int i = 2; i <= q; i++) {
    int m = q;
    while (!(m % i))
      m /= i;
    if (m == 1) {
      printf("%d\\n", i);
      break;
    }
  }
}