Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define int long long
#define endl "\n"
#define fir first
#define sec second
#define fro front
#define m_p make_pair
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define vecin(v)                                                               \
  for (int i = 0; i < (int)v.size(); i++)                                      \
    cin >> v[i];
using namespace std;

const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
int jousu(int x00, int y00) {
  int z00 = 1;
  for (int i = 0; i < y00; i++) {
    z00 *= x00;
  }
  return z00;
}
int keta(int x00) {
  int z00 = x00;
  int w00 = 0;
  while (z00 != 0) {
    z00 /= 10;
    w00++;
  }
  return w00;
}

int modinv(int a, int m) {
  int b = m, u = 1, v = 0;
  while (b) {
    int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
int modpow(int a, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res *a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int gcd(int xx, int yy) {
  int p = xx;
  int q = yy;
  if (q > p)
    swap(p, q);
  while (p % q != 0) {
    p %= q;
    swap(p, q);
  }
  return q;
}
int lcm(int xx, int yy) { return xx * yy / gcd(xx, yy); }
bool prime(int xx) {
  int a = xx;
  for (int i = 2; i * i <= xx; i++) {
    if (xx % i == 0) {
      return 0;
    }
  }
  return 1;
}
int com(int xx, int yy) {
  int zz = 1;
  for (int i = xx; i > xx - yy; i--) {
    zz *= i;
    zz %= mod;
  }
  for (int i = 1; i <= yy; i++) {
    zz *= modinv(i, mod);
    zz %= mod;
  }
  return zz;
}

signed main() {
  int a = 2, n, p, h = 0, ans = 1;
  cin >> n >> p;
  while (p >= a) {
    if (prime(a) == 0) {
      goto skip;
    }
    h = 0;
    while (p % a == 0) {
      p /= a;
      h++;
    }
    while (h >= n) {
      ans *= a;
      h -= n;
    }
  skip:
    a++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define int long long
#define endl "\n"
#define fir first
#define sec second
#define fro front
#define m_p make_pair
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define vecin(v)                                                               \
  for (int i = 0; i < (int)v.size(); i++)                                      \
    cin >> v[i];
using namespace std;

const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
int jousu(int x00, int y00) {
  int z00 = 1;
  for (int i = 0; i < y00; i++) {
    z00 *= x00;
  }
  return z00;
}
int keta(int x00) {
  int z00 = x00;
  int w00 = 0;
  while (z00 != 0) {
    z00 /= 10;
    w00++;
  }
  return w00;
}

int modinv(int a, int m) {
  int b = m, u = 1, v = 0;
  while (b) {
    int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
int modpow(int a, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res *a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int gcd(int xx, int yy) {
  int p = xx;
  int q = yy;
  if (q > p)
    swap(p, q);
  while (p % q != 0) {
    p %= q;
    swap(p, q);
  }
  return q;
}
int lcm(int xx, int yy) { return xx * yy / gcd(xx, yy); }
bool prime(int xx) {
  int a = xx;
  for (int i = 2; i * i <= xx; i++) {
    if (xx % i == 0) {
      return 0;
    }
  }
  return 1;
}
int com(int xx, int yy) {
  int zz = 1;
  for (int i = xx; i > xx - yy; i--) {
    zz *= i;
    zz %= mod;
  }
  for (int i = 1; i <= yy; i++) {
    zz *= modinv(i, mod);
    zz %= mod;
  }
  return zz;
}

signed main() {
  int a = 2, n, p, h = 0, ans = 1;
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  while (p >= a * a) {
    if (prime(a) == 0) {
      goto skip;
    }
    h = 0;
    while (p % a == 0) {
      p /= a;
      h++;
    }
    while (h >= n) {
      ans *= a;
      h -= n;
    }
  skip:
    a++;
  }
  cout << ans << endl;
}