Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
// #include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

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

bool isprime(long long int x) { // 与えられた整数が素数ならtrueを返す
  if (x == 2) {
    return true;
  }
  if (x < 2 || x % 2 == 0) {
    return false;
  }
  long long int i = 3;
  for (i; i <= sqrt(x); i += 2) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {

  long long int n, p;
  long long int maxi = 1;
  long long int i = 2;

  cin >> n >> p;
  if (i == 1) {
    maxi = p;
  } else {
    while (p != 1) {
      while (isprime(i) == false) {
        i++;
      }
      if ((long long int)(pow(i, n)) < 0) {
        // cout << "a " << i << endl;
        break;
      }
      if (p < (long long int)(pow(i, n))) {
        // cout << "b " << i << endl;
        break;
      }
      while (1) {
        if (p % (long long int)(pow(i, n)) == 0) {
          // cout << "c " << i << endl;
          p /= (long long int)(pow(i, n));
          maxi *= i;
        } else {
          break;
        }
      }

      i++;
    }
  }

  cout << maxi << endl;

  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
// #include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

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

bool isprime(long long int x) { // 与えられた整数が素数ならtrueを返す
  if (x == 2) {
    return true;
  }
  if (x < 2 || x % 2 == 0) {
    return false;
  }
  long long int i = 3;
  for (i; i <= sqrt(x); i += 2) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {

  long long int n, p;
  long long int maxi = 1;
  long long int i = 2;

  cin >> n >> p;
  if (n == 1) {
    maxi = p;
  } else {
    while (p != 1) {
      while (isprime(i) == false) {
        i++;
      }
      if ((long long int)(pow(i, n)) < 0) {
        // cout << "a " << i << endl;
        break;
      }
      if (p < (long long int)(pow(i, n))) {
        // cout << "b " << i << endl;
        break;
      }
      while (1) {
        if (p % (long long int)(pow(i, n)) == 0) {
          // cout << "c " << i << endl;
          p /= (long long int)(pow(i, n));
          maxi *= i;
        } else {
          break;
        }
      }

      i++;
    }
  }

  cout << maxi << endl;

  return 0;
}