Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a + mod - b) % mod
#define MUL(a, b) a = (a * b) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << " ";
    os << *it;
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto x : mp)
    os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template <typename T, int SIZE> int array_length(const T (&)[SIZE]) {
  return SIZE;
}
template <typename T, int N>
void PRINTF(const T (&a)[N], int s = N, int t = -1, bool f = true) {
  if (t == -1) {
    rep(i, s) {
      if (i)
        cout << " ";
      cout << a[i];
    }
  } else
    repi(i, s, t) {
      if (i != s)
        cout << " ";
      cout << a[i];
    }
  if (f)
    cout << "\n";
}
template <typename T, int N1, int N2>
void PRINTF(const T (&a)[N1][N2], int h = N1, int w = N2) {
  rep(i, h) {
    rep(j, w) { cout << a[i][j] << " \n"[j == w - 1]; }
  }
}
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;

int a[2000000];
map<int, int> mp;

int Pow(int x, int y) {
  int res = 1;
  rep(i, inf) {
    if (y == 0)
      break;
    if (y & 1)
      res *= x;
    y >>= 1;
    x *= x;
  }
  return res;
}

signed main() {
  int n, p;
  scanf("%lld%lld", &n, &p);
  if (n == 1) {
    printf("%lld\n", p);
    return 0;
  }
  int pp = p;
  repi(i, 2, inf) {
    if (pp == 1)
      break;
    while (pp % i == 0) {
      // printf("*%lld %lld*\n", i, pp);
      mp[i]++;
      pp /= i;
    }
  }

  for (auto it = mp.begin(); it != mp.end(); it++) {
    // printf("%lld %lld\n", it->fi, it->se);
  }

  int ans = 1;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    ans *= Pow(it->fi, (it->se / n));
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a + mod - b) % mod
#define MUL(a, b) a = (a * b) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << " ";
    os << *it;
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto x : mp)
    os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template <typename T, int SIZE> int array_length(const T (&)[SIZE]) {
  return SIZE;
}
template <typename T, int N>
void PRINTF(const T (&a)[N], int s = N, int t = -1, bool f = true) {
  if (t == -1) {
    rep(i, s) {
      if (i)
        cout << " ";
      cout << a[i];
    }
  } else
    repi(i, s, t) {
      if (i != s)
        cout << " ";
      cout << a[i];
    }
  if (f)
    cout << "\n";
}
template <typename T, int N1, int N2>
void PRINTF(const T (&a)[N1][N2], int h = N1, int w = N2) {
  rep(i, h) {
    rep(j, w) { cout << a[i][j] << " \n"[j == w - 1]; }
  }
}
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;

int a[2000000];
map<int, int> mp;

int Pow(int x, int y) {
  int res = 1;
  rep(i, inf) {
    if (y == 0)
      break;
    if (y & 1)
      res *= x;
    y >>= 1;
    x *= x;
  }
  return res;
}

signed main() {
  int n, p;
  scanf("%lld%lld", &n, &p);
  if (n == 1) {
    printf("%lld\n", p);
    return 0;
  }
  int pp = p;
  repi(i, 2, 2000000) {
    if (pp == 1)
      break;
    while (pp % i == 0) {
      // printf("*%lld %lld*\n", i, pp);
      mp[i]++;
      pp /= i;
    }
  }

  for (auto it = mp.begin(); it != mp.end(); it++) {
    // printf("%lld %lld\n", it->fi, it->se);
  }

  int ans = 1;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    ans *= Pow(it->fi, (it->se / n));
  }
  printf("%lld\n", ans);
  return 0;
}