Problem p03196 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define chmin(a, b)                                                            \
  if ((a) > (b))                                                               \
    (a) = (b);
#define chmax(a, b)                                                            \
  if ((a) < (b))                                                               \
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 10;

/*--------------------------------------------------------------------*/
bool isprime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

signed main() {
  int n, p;
  cin >> n >> p;

  vi prime;
  for (int i = 2; i <= min((int)(1e6 + 3e5), p); i++) {
    if (isprime(i)) {
      prime.pb(i);
    }
  }

  vector<pii> sosuu;

  rep(i, prime.size()) {

    int cnt = 0;
    while (p % prime[i] == 0) {
      cnt++;
      p /= prime[i];
    }
    if (cnt > 0)
      sosuu.pb(make_pair(prime[i], cnt));

    if (p == 1) {
      break;
    }
  }

  assert(p == 1);

  int ans = 1;
  for (auto p : sosuu) {
    ans *= pow(p.first, p.second / n);
  }
  cout << ans << endl;
  // for(pii p:sosuu){
  // 	cout<<p.first<<' '<<p.second<<endl;
  // }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define chmin(a, b)                                                            \
  if ((a) > (b))                                                               \
    (a) = (b);
#define chmax(a, b)                                                            \
  if ((a) < (b))                                                               \
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 10;

/*--------------------------------------------------------------------*/
bool isprime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

signed main() {
  int n, p;
  cin >> n >> p;

  vi prime;
  for (int i = 2; i <= min((int)(1e6 + 3e5), p); i++) {
    if (isprime(i)) {
      prime.pb(i);
    }
  }

  vector<pii> sosuu;

  rep(i, prime.size()) {

    int cnt = 0;
    while (p % prime[i] == 0) {
      cnt++;
      p /= prime[i];
    }
    if (cnt > 0)
      sosuu.pb(make_pair(prime[i], cnt));

    if (p == 1) {
      break;
    }
  }
  if (isprime(p)) {
    sosuu.pb(make_pair(p, 1));
    p /= p;
  }

  assert(p == 1);

  int ans = 1;
  for (auto p : sosuu) {
    ans *= pow(p.first, p.second / n);
  }
  cout << ans << endl;
  // for(pii p:sosuu){
  // 	cout<<p.first<<' '<<p.second<<endl;
  // }
  return 0;
}