Problem p03194 (C++)

Buggy submission (status: Runtime Error):
/*
    ##############################
    #  Author: Pratyush Gaurav   #
    #  College: NIT ROURKELA     #
    ##############################
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef vector<lli> vlli;
typedef vector<plli> vplli;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define sqr(x) (x) * (x)
#define all(a) a.begin(), a.end()
#define UN(v) sort(all(v)), v.resize(unique(all(v)) - v.begin())

#define endl '\n'

const long long INF = 100000000000000000;
const long long MOD = 1000000007;
const long long MAXN = 100005;

lli dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
lli dy[] = {1, -1, 0, 0, 1, -1, -1, 1};

lli ans = 1;

lli n, p;

lli power(lli a, lli b) {
  if (b == 0) {
    return 1;
  }
  lli temp = power(a, b / 2);
  if (temp * temp > p) {
    cout << ans << endl;
    exit(0);
  }
  if (temp * temp > 1e6 and a > 1e6) {
    cout << ans << endl;
    exit(0);
  }
  lli res = (b & 1 ? temp * a * temp : temp * temp);
  return res;
}

void solve() {
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return;
  } else {
    lli x = 1;
    while (1) {
      lli val = power(x, n);
      if (val > p)
        break;
      if (p % val == 0) {
        ans = max(ans, x);
      }
      x++;
    }
  }
  // cout << (1e18) << endl;
  cout << ans << endl;
}

int main() {
  IOS;
  lli t = 1;
  // cin >> t;

  while (t--)
    solve();
  return 0;
}


Fixed submission:
/*
    ##############################
    #  Author: Pratyush Gaurav   #
    #  College: NIT ROURKELA     #
    ##############################
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef vector<lli> vlli;
typedef vector<plli> vplli;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define sqr(x) (x) * (x)
#define all(a) a.begin(), a.end()
#define UN(v) sort(all(v)), v.resize(unique(all(v)) - v.begin())

#define endl '\n'

const long long INF = 100000000000000000;
const long long MOD = 1000000007;
const long long MAXN = 100005;

lli dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
lli dy[] = {1, -1, 0, 0, 1, -1, -1, 1};

lli ans = 1;

lli n, p;

lli power(lli a, lli b) {
  if (b == 0) {
    return 1;
  }
  lli temp = power(a, b / 2);
  if (temp > 1e6) {
    cout << ans << endl;
    exit(0);
  }
  if (temp * temp > p) {
    cout << ans << endl;
    exit(0);
  }
  if (temp * temp > 1e6 and a > 1e6) {
    cout << ans << endl;
    exit(0);
  }
  lli res = (b & 1 ? temp * a * temp : temp * temp);
  return res;
}

void solve() {
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return;
  } else {
    lli x = 1;
    while (1) {
      lli val = power(x, n);
      if (val > p)
        break;
      if (p % val == 0) {
        ans = max(ans, x);
      }
      x++;
    }
  }
  // cout << (1e18) << endl;
  cout << ans << endl;
}

int main() {
  IOS;
  lli t = 1;
  // cin >> t;

  while (t--)
    solve();
  return 0;
}