Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
constexpr int MOD = 1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

ll N, P;

ll mypow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    if (res > P)
      return -1;
    x = x * x;
    n >>= 1;
  }
  if (res > P)
    return -1;
  return res;
}

void solve() {
  cin >> N >> P;

  ll ans = 0;
  for (ll i = 1; i * i <= P; i++) {
    if (P % i == 0) {
      ll j = P / i;
      if (N == 1) {
        ans = max(ans, i);
        if (P % j == 0)
          ans = max(ans, j);
      } else {
        ll P_ = P;
        bool ok1 = true, ok2 = true;
        for (ll k = 0; k < N; k++) {
          if (P_ % i == 0) {
            P_ /= i;
          } else {
            ok1 = false;
            break;
          }
          if (P_ == 0) {
            ok1 = false;
            break;
          }
        }
        P_ = P;
        for (ll k = 0; k < N; k++) {
          if (P_ % j == 0) {
            P_ /= j;
          } else {
            ok2 = false;
            break;
          }
          if (P_ == 0) {
            ok2 = false;
            break;
          }
        }

        if (ok1)
          ans = max(ans, i);
        if (ok2)
          ans = max(ans, j);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
constexpr int MOD = 1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

ll N, P;

ll mypow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    if (res > P)
      return -1;
    x = x * x;
    n >>= 1;
  }
  if (res > P)
    return -1;
  return res;
}

void solve() {
  cin >> N >> P;

  ll ans = 0;
  for (ll i = 1; i * i <= P; i++) {
    if (P % i == 0) {
      ll j = P / i;
      if (N == 1) {
        ans = max(ans, i);
        if (P % j == 0)
          ans = max(ans, j);
      } else {
        ll P_ = P;
        bool ok1 = true, ok2 = true;
        if (i != 1) {
          for (ll k = 0; k < N; k++) {
            if (P_ % i == 0) {
              P_ /= i;
            } else {
              ok1 = false;
              break;
            }
            if (P_ == 0) {
              ok1 = false;
              break;
            }
          }
        }
        P_ = P;
        for (ll k = 0; k < N; k++) {
          if (P_ % j == 0) {
            P_ /= j;
          } else {
            ok2 = false;
            break;
          }
          if (P_ == 0) {
            ok2 = false;
            break;
          }
        }

        if (ok1)
          ans = max(ans, i);
        if (ok2)
          ans = max(ans, j);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}