Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char> CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define llower(v, hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v, hoge) *upper_bound(v.begin(), v.end(), hoge)

vector<lint> prime_factorization(lint N) {
  // 素因数分解して配列で返す
  vector<lint> R;
  if (N < 2)
    return R;
  for (int i = 2; i * i <= N; i++) {
    while (N % i == 0) {
      R.push_back(i);
      N /= i;
    }
  }
  if (N != 1) {
    R.push_back(N);
  }
  return R;
}

int main() {
  lint N, P;
  cin >> N >> P;
  vector<lint> V = prime_factorization(P);
  sort(V);
  lint VS = V.size();
  if (VS == 0) {
    cout << P << endl;
    return 0;
  }
  lint cnt = 1;
  lint ans = 1;
  for (int i = 1; i < VS; i++) {
    if (V[i - 1] == V[i]) {
      cnt++;
    } else {
      if (cnt / N * V[i - 1] != 0) {
        ans *= pow(V[i - 1], cnt / N);
      }
      cnt = 1;
    }
  }
  if (cnt / N * V[VS - 1] != 0) {
    ans *= pow(V[VS - 1], cnt / N);
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char> CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define llower(v, hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v, hoge) *upper_bound(v.begin(), v.end(), hoge)

vector<lint> prime_factorization(lint N) {
  // 素因数分解して配列で返す
  vector<lint> R;
  if (N < 2)
    return R;
  for (lint i = 2; i * i <= N; i++) {
    while (N % i == 0) {
      R.push_back(i);
      N /= i;
    }
  }
  if (N != 1) {
    R.push_back(N);
  }
  return R;
}

int main() {
  lint N, P;
  cin >> N >> P;
  vector<lint> V = prime_factorization(P);
  sort(V);
  lint VS = V.size();
  if (VS == 0) {
    cout << P << endl;
    return 0;
  }
  lint cnt = 1;
  lint ans = 1;
  for (int i = 1; i < VS; i++) {
    if (V[i - 1] == V[i]) {
      cnt++;
    } else {
      if (cnt / N * V[i - 1] != 0) {
        ans *= pow(V[i - 1], cnt / N);
      }
      cnt = 1;
    }
  }
  if (cnt / N * V[VS - 1] != 0) {
    ans *= pow(V[VS - 1], cnt / N);
  }

  cout << ans << endl;
}