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

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

int main() {
  int p, q, t, ans = 1, num;
  set<int> s;
  cin >> p >> q;
  num = t = q / __gcd(p, q);
  for (int i = 2; i * i <= num; i++) {
    if (!(num % i)) {
      s.insert(i);
      while (!(num % i))
        num /= i;
    }
  }
  if (num != 1)
    s.insert(num);
  set<int>::iterator ite = s.begin();
  while (ite != s.end())
    ans *= *ite, ite++;
  if (ans == 1)
    ans = t;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

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

vector<int> div(int x) {
  vector<int> res;
  for (int i = 1; i * i <= x; i++) {
    if (!(x % i)) {
      res.push_back(i);
      if (i != x / i)
        res.push_back(x / i);
    }
  }
  return res;
}

int main() {
  int p, q, a, ans = 1;
  vector<int> prime;
  set<int> s;
  cin >> p >> q;
  q /= __gcd(p, q);

  int Q = q;
  for (int i = 2; i * i <= q; i++) {
    while (Q % i == 0) {
      Q /= i;
      prime.push_back(i);
    }
  }
  if (Q > 1)
    prime.push_back(Q);
  for (int i = 0; i < prime.size(); i++)
    s.insert(prime[i]);
  set<int>::iterator ite;
  for (ite = s.begin(); ite != s.end(); ite++)
    ans *= (*ite);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

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

int main() {
  int p, q, mi, ans, temp;

  cin >> p >> q;

  mi = (p, q);

  while (p % q == 0) {
    if (q == 1)
      break;
    p /= q;
    q /= q;
  }

  for (int i = 2; i * i <= mi; i++) {
    while (p % i == 0 && q % i == 0) {
      p /= i;
      q /= i;
    }
    while (q % (mi / i) == 0 && p % (mi / i) == 0) {
      p /= (mi / i);
      q /= (mi / i);
    }
  }

  ans = 1;

  for (int i = 2; i * i <= q; i++) {
    if (q % i != 0)
      continue;
    int k = 1;
    if (i != 2) {
      for (int j = 3; j * j <= i; j++) {
        if (i % j == 0) {
          k = 0;
          break;
        }
      }
    }
    if (k == 1)
      ans *= i;
    while (q % i == 0) {
      q /= i;
    }
  }
  ans *= q;

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int p, q, mi, ans;

  cin >> p >> q;

  mi = (p, q);

  while (p % q == 0) {
    if (q == 1)
      break;
    p /= q;
    q /= q;
  }

  for (int i = 2; i * i <= mi; i++) {
    while (p % i == 0 && q % i == 0) {
      p /= i;
      q /= i;
    }
    while (q % (mi / i) == 0 && p % (mi / i) == 0) {
      p /= (mi / i);
      q /= (mi / i);
    }
  }

  ans = q;

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

  cout << ans << endl;

  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
long long int p, q, count, ans, k;
int prime[100000];
char a[1000005];

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

void check() {
  count = 0;
  for (int i = 2; i < 1000005; i++) {
    if (a[i] == '1')
      continue;
    prime[count] = i;
    count++;
    for (int j = i; j < 1000005; j += i) {
      a[j] = '1';
    }
  }
}

int main() {
  for (int i = 2; i < 1000005; i++) {
    a[i] = '0';
  }
  check();
  cin >> p >> q;
  q = q / gcd(p, q);
  ans = 1;
  k = 0;

  while (q > 1 && count > k) {
    if (q % prime[k] == 0)
      ans *= prime[k];
    while (q % prime[k] == 0)
      q /= prime[k];
    k++;
  }
  cout << ans * q << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int p, q, count, ans, k;
int prime[100000];
char a[1000005];

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

void check() {
  count = 0;
  for (int i = 2; i < 1000005; i++) {
    if (a[i] == '1')
      continue;
    prime[count] = i;
    count++;
    for (int j = i; j < 1000005; j += i) {
      a[j] = '1';
    }
  }
}

int main() {
  for (int i = 2; i < 1000005; i++) {
    a[i] = '0';
  }
  check();
  cin >> p >> q;
  q = q / gcd(p, q);
  ans = 1;
  k = 0;

  while (q > 1) {
    if (q % prime[k] == 0)
      ans *= prime[k];
    while (q % prime[k] == 0)
      q /= prime[k];
    k++;
  }
  cout << ans << endl;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int yaku(int n, int m) {
  if (n % m == 0) {
    return m;
  }
  return yaku(m, n % m);
}

int main() {
  int n, m;
  cin >> n >> m;
  m /= yaku(n, m);
  int ans = 1;
  for (int i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      ans *= i;
      while (m % i == 0) {
        m /= i;
      }
    }
  }
  ans *= m;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int yaku(int n, int m) {
  if (n % m == 0) {
    return m;
  }
  return yaku(m, n % m);
}

int main() {
  int n, m;
  cin >> n >> m;
  m /= yaku(n, m);
  int ans = 1;
  for (int i = 2; i <= m; i++) {
    if (m % i == 0) {
      ans *= i;
      while (m % i == 0) {
        m /= i;
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
bool prime[20000000];

int main() {

  for (int i = 2; i * i < 20000000; i++)
    for (int j = 2; j < 20000000 / i && !prime[i]; j++)
      prime[i * j] = 1;

  vector<int> n;
  for (int i = 2; i < 20000000; i++)
    if (!prime[i])
      n.push_back(i);

  int p, q;
  cin >> p >> q;
  int a = __gcd(p, q);
  p /= a, q /= a;

  int ans = 1;
  for (int i = 0; i < n.size(); i++)
    if (q % n[i] == 0) {
      ans *= n[i];
      while (q % n[i] == 0)
        q /= n[i];
    }
  ans *= q;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int p, q;
  cin >> p >> q;
  int a = __gcd(p, q);
  p /= a, q /= a;

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

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

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

using namespace std;

typedef pair<int, int> pii;

signed main() {
  int p, q;
  int k;
  cin >> p >> q;
  k = __gcd(p, q);
  p /= k;
  q /= k;

  int q_ = q;
  vector<pii> ans;
  for (int i = 2; i <= sqrt(q); i++) {
    if (q_ % i == 0) {
      pii p = make_pair(i, 1);
      q_ /= i;
      while (q_ % i == 0) {
        p.second++;
        q_ /= i;
      }
      ans.push_back(p);
    }
  }

  if (q_ > 1)
    ans.push_back(pii(q_, 1));

  int a = 1;
  for (int i = 0; i < (int)ans.size(); i++) {
    a *= ans[i].first;
  }

  cout << a << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

long p, q;
vector<int> yakusuu;

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

  for (int i = 1; i <= p / 2; i++) {
    if (p % i == 0) {
      yakusuu.push_back(i);
    }
  }

  for (int j = 0; j < yakusuu.size(); j++) {
    if (q % yakusuu[j] == 0)
      q = q / yakusuu[j];
  }

  cout << q << endl;

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

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

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

  return gcd(b, a % b);
}

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

  long tmp = gcd(q, p);
  p /= tmp;
  q /= tmp;

  long ans = 1;
  long root = sqrt(q) + 1;
  vector<bool> hurui(root, true);
  for (int i = 2; i <= root; i++) {
    if (hurui[i] == true) {
      if (i <= root) {
        for (int j = 2 * i; j <= root; j += i) {
          hurui[j] = false;
        }
      }
      if (q % i == 0) {
        ans *= i;
        while (q % i == 0)
          q /= i;
        if (q == 1)
          break;
      }
    }
  }
  ans *= q;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  return gcd(b, a % b);
}

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

  long tmp = gcd(q, p);
  p /= tmp;
  q /= tmp;

  long ans = 1;
  long root = sqrt(q) + 1;
  vector<bool> hurui(q + 1, true);
  for (int i = 2; i <= q / 2; i++) {
    if (hurui[i] == true) {
      if (i <= root) {
        for (int j = 2 * i; j <= q / 2; j += i) {
          hurui[j] = false;
        }
      }
      if (q % i == 0)
        ans *= i;
    }
  }
  if (ans == 1)
    ans = q;

  cout << ans << endl;

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

#include <iostream>
#include <vector>

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 = q / gcd(p, q);
  vector<int> v;
  for (int i = 2; i * i <= q; i++) {
    if (q % i == 0) {
      v.push_back(i);
      while (q % i == 0)
        q /= i;
    }
  }
  int r = 1;
  for (int i = 0; i < v.size(); i++)
    r *= v[i];

  cout << r * q << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

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 = q / gcd(p, q);
  vector<int> v;
  for (int i = 2; i <= q; i++) {
    if (q % i == 0) {
      v.push_back(i);
      while (q % i == 0)
        q /= i;
    }
  }
  int r = 1;
  for (int i = 0; i < v.size(); i++)
    r *= v[i];

  cout << r << endl;

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

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD = 1e9 + 7;

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

//?´???°??????
vector<ll> divisor(ll n) {
  vector<ll> res;
  // res.push_back(1);res.push_back(n);
  ll tmp = n;
  for (int i = 2; i * i <= n; i++) {
    if (tmp % i != 0)
      continue;
    res.push_back(i);
    while (tmp % i == 0) {
      tmp /= i;
    }
  }
  if (tmp != n)
    res.push_back(tmp);
  sort(res.begin(), res.end());
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll p, q;
  cin >> p >> q;
  ll g = gcd(p, q);
  p /= g;
  q /= g;
  vector<ll> res = divisor(q);
  if (res.size() == 0)
    cout << q << endl;
  else {
    ll ret = 1;
    for (int i = 0; i < (int)res.size(); i++)
      ret *= res[i];
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
// a,b???GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

//?´???°??????
vector<ll> divisor(ll n) {
  vector<ll> res;
  // res.push_back(1);res.push_back(n);
  ll tmp = n;
  for (int i = 2; i <= tmp; i++) {
    if (tmp % i != 0)
      continue;
    if (i == n)
      break;
    res.push_back(i);
    while (tmp % i == 0) {
      tmp /= i;
    }
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll p, q;
  cin >> p >> q;
  ll g = gcd(p, q);
  p /= g;
  q /= g;
  vector<ll> res = divisor(q);
  if (res.size() == 0)
    cout << q << endl;
  else {
    ll ret = 1;
    for (int i = 0; i < (int)res.size(); i++)
      ret *= res[i];
    cout << ret << endl;
  }
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

long long int gcd(long long int p, long long int q) {
  if (!q)
    return p;
  if (q > p)
    return gcd(q, p);
  return gcd(q, p % q);
}

int main(void) {
  long long int p, q;
  scanf("%lld%lld", &p, &q);
  long long int g = gcd(p, q);
  p /= g;
  q /= g;

  long long int b = 1;
  for (long long int i = 2; q - 1 && i * i <= q; i++) {
    if (q % i)
      continue;
    b *= i;
    while (!(q % i))
      q /= i;
  }
  b *= q;
  printf("%lld\\n", b);
}

Error: TLE

Fixed submission:

#include <cstdio>

long long int gcd(long long int p, long long int q) {
  if (!q)
    return p;
  if (q > p)
    return gcd(q, p);
  return gcd(q, p % q);
}

int main(void) {
  long long int p, q;
  scanf("%lld%lld", &p, &q);
  long long int g = gcd(p, q);
  p /= g;
  q /= g;

  long long int b = 1;
  for (long long int i = 2; q - 1; i++)
    if (!(q % i)) {
      b *= i;
      while (!(q % i))
        q /= i;
    }
  printf("%lld\\n", b);
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
int p, q;
vector<int> primes;

const int MA = 30000000;
bool t[MA * 2];

void eratosu() {
  t[1] = 1;

  for (int i = 2; i <= MA; i++) {
    if (t[i] == 0) {
      primes.push_back(i);
      for (int j = i * 2; j <= MA; j += i)
        t[j] = 1;
    }
  }
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  eratosu();
  cin >> p >> q;

  for (int i = 0; i < primes.size(); i++) {
    int now = primes[i];
    while (q % now == 0 && p % now == 0)
      q /= now, p /= now;
  }

  int gc = gcd(p, q);
  int np = p, nq = q;
  // p=np/gc;
  // q=nq/gc;
  int ans = 1, qq = q;
  int cnt = 0;

  for (int i = 0; i < primes.size(); i++) {
    int now = primes[i];
    if (q % now == 0) {
      ans *= now, cnt++;
    }
    while (q % now == 0)
      q /= now;

    // if(now>sqrt(qq))break;
  }
  ans *= q;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
int p, q;
vector<int> primes;

const int MA = 10000000;
bool t[MA * 2];

void eratosu() {
  t[1] = 1;

  for (int i = 2; i <= MA; i++) {
    if (t[i] == 0) {
      primes.push_back(i);
      for (int j = i * 2; j <= MA; j += i)
        t[j] = 1;
    }
  }
}

bool check(int x) {
  int a = x;
  int aa = x;

  while (aa <= q) {
    if (aa == q)
      return 1;
    aa = aa * a;
  }
  return 0;
}

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

  for (int i = 0; i < primes.size(); i++) {
    int now = primes[i];
    // cout<<i<<endl;
    if (sqrt(now) > p || sqrt(now) > q)
      break;
    while (p % now == 0 && q % now == 0)
      p /= now, q /= now;
  }
  // cout<<p<<" "<<q<<endl;

  for (int i = 2; i <= 1000000000; i++) {
    if (check(i)) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int pow(int x, int n) {
  if (n == 0)
    return 1;
  int res = pow(x, n / 2);
  if (n % 2 == 1)
    return x * res * res;
  return res * res;
}

vector<int> Prime(int p) {
  vector<bool> is_prime(p + 10, true);
  vector<int> prime;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= p; i++) {
    if (is_prime[i])
      prime.push_back(i);
    for (int j = 2 * i; j <= p; j += i) {
      is_prime[j] = false;
    }
  }
  return prime;
}

map<int, int> fact(int n) {
  map<int, int> res;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        res[i]++;
      else
        res[i]++, res[n / i]++;
    }
  }
  return res;
}

int main() {
  int p, q;
  std::cin >> p >> q;
  int gcd = __gcd(p, q), ans = 1;
  p /= gcd;
  q /= gcd;
  set<int> s;
  for (int i = 2; i * i <= q; i++) {
    while (q % i == 0)
      s.insert(i), q /= i;
  }
  s.insert(q);
  for (auto i : s) {
    ans *= i;
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int pow(int x, int n) {
  if (n == 0)
    return 1;
  int res = pow(x, n / 2);
  if (n % 2 == 1)
    return x * res * res;
  return res * res;
}

vector<int> Prime(int p) {
  vector<bool> is_prime(p + 10, true);
  vector<int> prime;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= p; i++) {
    if (is_prime[i])
      prime.push_back(i);
    for (int j = 2 * i; j <= p; j += i) {
      is_prime[j] = false;
    }
  }
  return prime;
}

map<int, int> fact(int n) {
  map<int, int> res;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        res[i]++;
      else
        res[i]++, res[n / i]++;
    }
  }
  return res;
}

int main() {
  int p, q;
  std::cin >> p >> q;
  int gcd = __gcd(p, q), ans;
  p /= gcd;
  q /= gcd;
  for (int i = 2; i * i <= q; i++) {
    int tmp = i;
    for (int j = 2; tmp <= q; j++) {
      tmp *= i;
      if (tmp == q) {
        std::cout << i << std::endl;
        return 0;
      }
    }
  }
  std::cout << q << std::endl;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using lli = long long int;

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

lli solve(lli p, lli q) {
  const lli NMAX = 100008;
  vector<lli> factor(NMAX, 0);
  for (lli n = 2; n < NMAX; ++n) {
    if (factor[n]) {
      continue;
    }
    for (lli k = 2; k * n < NMAX; ++k) {
      factor[k * n] = n;
    }
  }
  lli ans = 1;
  for (lli n = 2; q > 1 && n < NMAX; ++n) {
    if (factor[n] || (q % n != 0)) {
      continue;
    }
    while (q % n == 0) {
      q /= n;
    }
    ans *= n;
  }
  ans *= q;
  return max<lli>(2, ans);
}

int main() {
  lli P, Q;
  cin >> P >> Q;
  const lli G = gcd(P, Q);
  P /= G, Q /= G;
  cout << solve(P, Q) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using lli = long long int;

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

lli solve(lli p, lli q) {
  const lli NMAX = 100008;
  vector<lli> factor(NMAX, 0);
  for (lli n = 2; n < NMAX; ++n) {
    if (factor[n]) {
      continue;
    }
    for (lli k = 2; k * n < NMAX; ++k) {
      factor[k * n] = n;
    }
  }
  lli ans = 1;
  for (lli n = 2; q > 1; ++n) {
    if (factor[n] || (q % n != 0)) {
      continue;
    }
    while (q % n == 0) {
      q /= n;
    }
    ans *= n;
  }
  return max<lli>(2, ans);
}

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

#include <iostream>
using namespace std;
int p, q;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  cin >> p >> q;
  int res = 1;
  int r = gcd(p, q);
  q /= r;
  for (int i = 2; i * i <= q; i++) {
    if (q % i == 0) {
      res *= i;
      while (q % i == 0)
        q /= i;
    }
  }
  if (q >= 2)
    res *= q;
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long p, q;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  cin >> p >> q;
  long long r = gcd(p, q);
  p /= r;
  q /= r;
  for (int i = 2; i * i <= q; i++) {
    int D = 1;
    if (q == i)
      continue;
    while (true) {
      D *= i;
      if (D > q)
        break;
      if (D == q) {
        cout << i << endl;
        goto E;
      }
    }
  }
  cout << q << endl;
E:;
  return 0;
}
Problem p01809 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int euclid(int x, int y) {
  int t;
  while (x % y) {
    x %= y;
    t = x;
    x = y;
    y = t;
  }
  return y;
}
int main() {
  int p, q, n, m, t = 2, ans = 1;
  cin >> p >> q;
  n = q / euclid(p, q);
  m = n;
  map<int, int> f;
  while (n > 1 && t < sqrt(m) + 1) {
    if (!(n % t)) {
      n /= t;
      f[t] = 1;
    }
    while (!(n % t)) {
      ++f[t];
      n /= t;
    }
    ++t;
  }
  if (n != 1)
    f[n] = 1;

  for (auto itr = f.begin(); itr != f.end(); ++itr) {
    ans *= itr->first;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int euclid(int x, int y) {
  int t;
  while (x % y) {
    x %= y;
    t = x;
    x = y;
    y = t;
  }
  return y;
}
int main() {
  int p, q, n, m, t = 2, ans = 1;
  cin >> p >> q;
  n = q / euclid(p, q);
  m = n;
  map<int, int> f;
  while (n > 1) {
    if (!(n % t)) {
      n /= t;
      f[t] = 1;
    }
    while (!(n % t)) {
      ++f[t];
      n /= t;
    }
    ++t;
  }
  m = q;
  for (auto itr = f.begin(); itr != f.end(); ++itr)
    m = min(m, itr->second);
  for (auto itr = f.begin(); itr != f.end(); ++itr) {
    m = euclid(m, itr->second);
  }
  for (auto itr = f.begin(); itr != f.end(); ++itr) {
    for (int i = 0; i < itr->second / m; ++i) {
      ans *= itr->first;
    }
  }
  cout << ans << endl;
  return 0;
}