prithivMLmods/Coder-Stat huggingface dataset:
Problem p03196 (C++)

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

using ll = long long;
#define ALL(c) (c).begin(), (c).end()
const int MOD = 1000000007;

int main() {
  ll n, p;
  cin >> n >> p;

  if (n == 1) {
    cout << p << endl;
    return 0;
  }

  map<ll, ll> m;
  ll d = 2;
  ll l = p / 2;
  while (p > 1 && l >= d) {
    if (p % d == 0) {
      m[d]++;
      p /= d;
    } else {
      if (d == 2) {
        d++;
      } else {
        d += 2;
      }
    }
  }

  ll ans = 1;
  for (const auto q : m) {
    if (q.second >= n) {
      ans *= (ll)pow(q.first, q.second / n);
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define ALL(c) (c).begin(), (c).end()
const int MOD = 1000000007;

int main() {
  ll n, p;
  cin >> n >> p;

  if (n == 1) {
    cout << p << endl;
    return 0;
  }

  map<ll, ll> m;
  ll d = 2;
  ll l = p;
  while (p > 1 && l >= d * d) {
    if (p % d == 0) {
      m[d]++;
      p /= d;
    } else {
      if (d == 2) {
        d++;
      } else {
        d += 2;
      }
    }
  }

  ll ans = 1;
  for (const auto q : m) {
    if (q.second >= n) {
      ans *= (ll)pow(q.first, q.second / n);
    }
  }

  cout << ans << endl;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long

ll N, P;

int solve() {
  ll ans = 1;
  ll store;
  for (int i = 1; pow(i, N) <= P; i++) {
    store = static_cast<ll>(pow(i, N));
    if (P % store == 0) {
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}

int main() {
  cin >> N >> P;
  solve();
  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long

ll N, P;

int solve() {
  ll ans = 1;
  ll store;
  for (int i = 1; pow(i, N) <= P; i++) {
    store = static_cast<ll>(pow(i, N));
    if (P % store == 0) {
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}

int main() {
  cin >> N >> P;
  if (N == 1) {
    cout << P << endl;
    return 0;
  }
  solve();
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(0);
  ll n, p;
  cin >> n >> p;
  vector<ll> D;
  for (ll d = 1; d * d <= p; ++d) {
    if (p % d == 0) {
      D.push_back(d);
      D.push_back(p / d);
    }
  }
  ll odp = 1;
  for (int i = 0; i < D.size(); ++i) {
    ll t = p;
    ll j;
    for (j = 0; j < n; ++j) {
      if (t % D[i] != 0)
        break;
      t /= D[i];
    }
    if (t >= 1 && j == n)
      odp = max(odp, D[i]);
  }
  cout << odp;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(0);
  ll n, p;
  cin >> n >> p;
  vector<ll> D;
  for (ll d = 1; d * d <= p; ++d) {
    if (p % d == 0) {
      D.push_back(d);
      D.push_back(p / d);
    }
  }
  ll odp = 1;
  for (int i = 0; i < D.size(); ++i) {
    if (D[i] == 1)
      continue;
    ll t = p;
    ll j;
    for (j = 0; j < n; ++j) {
      if (t % D[i] != 0)
        break;
      t /= D[i];
    }
    if (t >= 1 && j == n)
      odp = max(odp, D[i]);
  }
  cout << odp;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
typedef long long ll;

ll N, P;
ll pw(ll n, int m) {
  ll ret = 1;
  while (m) {
    if (m & 1) {
      ret *= n;
    }
    n *= n;
    m >>= 1;
  }
  return ret;
}
int main() {
  cin >> N >> P;
  ll ans = 1;
  for (int i = 2; i * i <= P; i++) {
    int cnt = 0;
    while (P % i == 0) {
      P /= i;
      cnt++;
    }
    if (cnt >= N) {
      ans *= pw(i, (cnt / N));
    }
  }
  if (P != 1 && N == 1) {
    ans *= P;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
typedef long long ll;

ll N, P;
ll pw(ll n, int m) {
  ll ret = 1;
  while (m) {
    if (m & 1) {
      ret *= n;
    }
    n *= n;
    m >>= 1;
  }
  return ret;
}
int main() {
  cin >> N >> P;
  ll ans = 1;
  for (ll i = 2; i * i <= P; i++) {
    int cnt = 0;
    while (P % i == 0) {
      P /= i;
      cnt++;
    }
    if (cnt >= N) {
      ans *= pw(i, (cnt / N));
    }
  }
  if (P != 1 && N == 1) {
    ans *= P;
  }
  cout << ans << endl;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  long n, p;
  cin >> n >> p;
  int c = 2;
  long t = static_cast<long>(pow(c, n));
  long r = 1;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  while (t <= p) {
    if (p % t == 0) {
      r = c;
    }
    ++c;
    t = static_cast<long>(pow(c, n));
  }
  cout << r << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  long n, p;
  cin >> n >> p;
  int c = 2;
  long t = static_cast<long>(pow(c, n));
  long r = 1;
  if (n == 1) {
    cout << p << endl;
    return 0;
  } else if (n >= 40) {
    cout << 1 << endl;
    return 0;
  }
  while (t <= p) {
    if (p % t == 0) {
      r = c;
    }
    ++c;
    t = static_cast<long>(pow(c, n));
  }
  cout << r << endl;
}
Problem p03196 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

int main() {
  int64 N, P;
  cin >> N >> P;
  vector<int64> divisor;
  for (int64 i = 1; i * i <= P; i++) {
    if (P % i == 0) {
      divisor.emplace_back(i);
      if (i * i != P)
        divisor.emplace_back(P / i);
    }
  }
  int64 ret = 1;
  for (auto &p : divisor) {
    if (p == 1)
      continue;
    int64 mul = 1;
    for (int64 i = 0; i < N; i++) {
      mul *= p;
      if (mul > P)
        break;
    }
    if (mul <= P && P % mul == 0) {
      ret = max(ret, p);
    }
  }

  cout << ret << endl;
}


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

using namespace std;

using int64 = long long;

int main() {
  int64 N, P;
  cin >> N >> P;
  vector<int64> divisor;
  for (int64 i = 1; i * i <= P; i++) {
    if (P % i == 0) {
      divisor.emplace_back(i);
      if (i * i != P)
        divisor.emplace_back(P / i);
    }
  }
  int64 ret = 1;
  for (auto &p : divisor) {
    if (p == 1)
      continue;
    __int128_t mul = 1;
    for (int64 i = 0; i < N; i++) {
      mul *= p;
      if (mul > P)
        break;
    }
    if (mul <= P && P % mul == 0) {
      ret = max(ret, p);
    }
  }

  cout << ret << endl;
}
Problem p03196 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, p;
  vector<int> prime;
  cin >> n >> p;
  if (n == 1)
    cout << p << endl;
  else if (n == 2) {
    int ans = 1;
    for (int i = 2; i <= sqrt(p); i++) {
      if (p % (i * i) == 0)
        ans = i;
    }
    cout << ans << endl;
  } else {
    int ans = 1;
    for (int i = 2; i <= p; i++) {
      long tmp = 1;
      bool chk = true;
      for (int j = 0; j < n; j++) {
        tmp *= i;
        if (tmp > p) {
          chk = false;
          break;
        }
      }
      if (chk) {
        if (p % tmp == 0)
          ans = i;
      } else
        break;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, p;
  vector<int> prime;
  cin >> n >> p;
  if (n == 1)
    cout << p << endl;
  else if (n == 2) {
    int ans = 1;
    for (long i = 2; i <= sqrt(p); i++) {
      if (p % (i * i) == 0)
        ans = i;
    }
    cout << ans << endl;
  } else {
    int ans = 1;
    for (int i = 2; i <= p; i++) {
      long tmp = 1;
      bool chk = true;
      for (int j = 0; j < n; j++) {
        tmp *= i;
        if (tmp > p) {
          chk = false;
          break;
        }
      }
      if (chk) {
        if (p % tmp == 0)
          ans = i;
      } else
        break;
    }
    cout << ans << endl;
  }
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int N, P;
  cin >> N >> P;

  if (N == 1) {
    cout << P << endl;
    return 0;
  }

  int A = P;
  int ans = 1;
  int cnt = 2;
  int waru = 0;
  while (P > 1 || cnt <= sqrt(A)) {
    if (P % cnt == 0 && P / cnt > 0) {
      waru++;
      P /= cnt;
    } else if (P % cnt != 0) {
      cnt++;
      waru = 0;
    }
    if (waru == N) {
      ans *= cnt;
      waru = 0;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int N, P;
  cin >> N >> P;

  if (N == 1) {
    cout << P << endl;
    return 0;
  }

  int A = P;
  int ans = 1;
  int cnt = 2;
  int waru = 0;
  while (P > 1 && cnt <= sqrt(A)) {
    if (P % cnt == 0 && P / cnt > 0) {
      waru++;
      P /= cnt;
    } else if (P % cnt != 0) {
      cnt++;
      waru = 0;
    }
    if (waru == N) {
      ans *= cnt;
      waru = 0;
    }
  }
  cout << ans << endl;
}
Problem p03196 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;

lli modpow(lli a, lli p, lli mod) {
  lli ret = 1;
  for (int i = 63; i >= 0; i--) {
    ret *= ret;
    if (ret > mod)
      return -1;
    if ((1LL << i & p) != 0) {
      ret *= a;
      if (ret > mod)
        return -1;
    }
  }
  return ret;
}

int main() {
  lli n, p;
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  lli ans = 1;
  for (lli i = 2;; i++) {
    lli ret = modpow(i, n, p);
    if (ret == -1)
      break;
    while (p % ret == 0) {
      p /= ret;
      ans *= i;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;

lli modpow(lli a, lli p, lli mod) {
  lli ret = 1;
  for (int i = 63; i >= 0; i--) {
    if (ret > (lli)1e7)
      return -1;
    ret *= ret;
    if (ret > mod)
      return -1;
    if ((1LL << i & p) != 0) {
      ret *= a;
      if (ret > mod)
        return -1;
    }
  }
  return ret;
}

int main() {
  lli n, p;
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  lli ans = 1;
  for (lli i = 2;; i++) {
    lli ret = modpow(i, n, p);
    if (ret == -1)
      break;
    while (p % ret == 0) {
      p /= ret;
      ans *= i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define int long long

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n, p;
  cin >> n >> p;

  vector<int> a(100000001);

  if (p == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (n == 1) {
    cout << p << endl;
    return 0;
  }

  int i = 2;

  while (p > 1) {
    if (p % i == 0) {
      a.at(i)++;
      p /= i;
    } else {
      i++;
    }
  }

  int ans = 1;
  for (int i = 2; i < 100000001; i++) {
    if (a.at(i) >= n) {
      int j = a.at(i) / n;
      ans *= pow(i, j);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define int long long

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n, p;
  cin >> n >> p;

  vector<int> a(100000001);

  if (p == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (n == 1) {
    cout << p << endl;
    return 0;
  }

  int i = 2;

  while (p > 1 && i < 10000000) {
    if (p % i == 0) {
      a.at(i)++;
      p /= i;
    } else {
      i++;
    }
  }

  int ans = 1;
  for (int i = 2; i < 100000001; i++) {
    if (a.at(i) >= n) {
      int j = a.at(i) / n;
      ans *= pow(i, j);
    }
  }

  cout << ans << endl;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;

int main() {

  ll N, P, tmp, ans;
  cin >> N >> P;
  ans = 1;

  if (N == 1) {
    cout << P;
    return 0;
  }

  for (ll i = 1; i <= 1e6; i++) {
    bool flag = true;
    tmp = P;
    for (int j = 0; j < N; j++) {
      if (tmp % i == 0) {
        tmp /= i;
      } else {
        tmp /= i;
        flag = false;
        break;
      }
    }
    if (flag)
      ans = i;
    if (tmp < 1)
      break;
  }

  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;

int main() {

  ll N, P, tmp, ans;
  cin >> N >> P;
  ans = 1;

  if (N == 1) {
    cout << P;
    return 0;
  }

  for (ll i = 2; i <= 1e6; i++) {
    bool flag = true;
    tmp = P;
    for (int j = 0; j < N; j++) {
      if (tmp % i == 0) {
        tmp /= i;
      } else {
        tmp /= i;
        flag = false;
        break;
      }
    }
    if (flag)
      ans = i;
    if (tmp < 1)
      break;
  }

  cout << ans;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 9;
const int INF = 1e9;
const double EPS = 1e-9;

map<long long, long long> mp;
long long n, p;

long long quickPow(long long a, long long b) {
  if (b == 0)
    return 1;

  long long tmp = quickPow(a, b / 2);
  if (b & 1)
    return tmp * tmp * a;
  return tmp * tmp;
}

void read() { cin >> n >> p; }

void solv() {
  for (int i = 2; i * i <= p; i++) {
    if (p % i == 0) {
      while (p % i == 0) {
        mp[i]++;
        p /= i;
      }
    }
  }

  if (p > 1)
    mp[p]++;

  long long res = 1;
  for (map<long long, long long>::iterator it = mp.begin(); it != mp.end();
       it++) {
    res *= quickPow(it->first, it->second / n);
  }

  cout << res << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifdef DEBUG
  freopen("in.txt", "r", stdin);
#endif

  read();
  solv();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 9;
const int INF = 1e9;
const double EPS = 1e-9;

map<long long, long long> mp;
long long n, p;

long long quickPow(long long a, long long b) {
  if (b == 0)
    return 1;

  long long tmp = quickPow(a, b / 2);
  if (b & 1)
    return tmp * tmp * a;
  return tmp * tmp;
}

void read() { cin >> n >> p; }

void solv() {
  for (long long i = 2; i * i <= p; i++) {
    if (p % i == 0) {
      while (p % i == 0) {
        mp[i]++;
        p /= i;
      }
    }
  }

  if (p > 1)
    mp[p]++;

  long long res = 1;
  for (map<long long, long long>::iterator it = mp.begin(); it != mp.end();
       it++) {
    res *= quickPow(it->first, it->second / n);
  }

  cout << res << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifdef DEBUG
  freopen("in.txt", "r", stdin);
#endif

  read();
  solv();

  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
ll n, p, ans;

ll pow(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y % 2) {
    return x * pow(x, y - 1);
  } else {
    ll yy = pow(x, y / 2);
    return yy * yy;
  }
}

int main() {
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  if (n > 100) {
    cout << 1 << endl;
  }
  for (ll i = 1; pow(i, n) <= p; i++) {
    if (p % pow(i, n) == 0)
      ans = i;
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
ll n, p, ans;

ll pow(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y % 2) {
    return x * pow(x, y - 1);
  } else {
    ll yy = pow(x, y / 2);
    return yy * yy;
  }
}

int main() {
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  if (n > 100) {
    cout << 1 << endl;
    return 0;
  }
  for (ll i = 1; pow(i, n) <= p; i++) {
    if (p % pow(i, n) == 0)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

int main() {
  ll n, p;
  cin >> n >> p;
  ll m = 0;
  for (ll i = 1; pow(i, n) <= p; ++i) {
    if (p % (ll)pow(i, n) == 0)
      m = max(m, i);
  }
  cout << m << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

int main() {
  ll n, p;
  cin >> n >> p;
  ll m = 0;
  if (n == 1) {
    m = p;
  } else {
    for (ll i = 1; pow(i, n) <= p; ++i) {
      if (p % (ll)pow(i, n) == 0)
        m = max(m, i);
    }
  }
  cout << m << endl;
  return 0;
}
Problem p03196 (C++)

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

int main() {
  long long A;
  cin >> A;
  long long N;
  cin >> N;
  if (A == 1) {
    cout << N << endl;
    return 0;
  }
  long long temp = N;
  map<long long, long long> mp;
  int i = 2;
  while (temp > 1) {
    if (temp % i == 0) {
      mp[i]++;
      temp = temp / i;
      // cout<<i<<endl;
    } else
      i++;
    if (i > 500000000) {
      cout << 1 << endl;
      return 0;
    }
  }
  long long ans = 1;
  long long mult = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    if ((itr->second) >= A) {
      mult = (itr->second) / A;
      ans = ans * pow((itr->first), mult);
      // cout<<itr->first<<endl;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long A;
  cin >> A;
  long long N;
  cin >> N;
  if (A == 1) {
    cout << N << endl;
    return 0;
  }
  long long temp = N;
  map<long long, long long> mp;
  int i = 2;
  while (temp > 1) {
    if (temp % i == 0) {
      mp[i]++;
      temp = temp / i;
      // cout<<i<<endl;
    } else
      i++;
    if (i > 100000000) {
      cout << 1 << endl;
      return 0;
    }
  }
  long long ans = 1;
  long long mult = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    if ((itr->second) >= A) {
      mult = (itr->second) / A;
      ans = ans * pow((itr->first), mult);
      // cout<<itr->first<<endl;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
typedef long long int LL;
int main() {
  LL n, m, a, b;
  int i, j;
  int s = 1;
  cin >> n >> m;
  if (n == 1) {
    cout << m << endl;
    return 0;
  }
  for (a = 1;; a++) {
    for (b = 1, i = 0; i < n; i++) {
      b *= a;
      if (b > m) {
        cout << s << endl;
        return 0;
      }
    }
    if (b > m)
      break;
    if (m % b == 0)
      s = a;
  }
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long int LL;
int main() {
  LL n, m, a, b;
  int i, j;
  int s = 1;
  cin >> n >> m;
  if (n == 1) {
    cout << m << endl;
    return 0;
  }
  if (n > 100) {
    cout << 1 << endl;
    return 0;
  }
  for (a = 1;; a++) {
    for (b = 1, i = 0; i < n; i++) {
      b *= a;
      if (b > m) {
        cout << s << endl;
        return 0;
      }
    }
    if (b > m)
      break;
    if (m % b == 0)
      s = a;
  }
  cout << s << endl;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long long pw(long long e, long long x) {
  if (x == 0)
    return 1;
  if (x == 1)
    return e;
  if (x % 2 == 0)
    return pw(e * e, x / 2);
  return e * pw(e, x - 1);
}

int main() {
  long long n, p;
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  long long ans = 1;
  for (long long i = 2; pw(i, n) <= p; i++) {
    if (p % pw(i, n) == 0)
      ans = i;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long long pw(long long e, long long x) {
  if (x == 0)
    return 1;
  if (x == 1)
    return e;
  if (x % 2 == 0)
    return pw(e * e, x / 2);
  return e * pw(e, x - 1);
}

int main() {
  long long n, p;
  cin >> n >> p;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  long long ans = 1;
  if (n > 1000000) {
    cout << 1 << endl;
    return 0;
  }
  for (long long i = 2; pw(i, n) <= p; i++) {
    if (p % pw(i, n) == 0)
      ans = i;
  }
  cout << ans << endl;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int A, B, C = 1;
  cin >> A >> B;
  if (A == 1) {
    cout << B << endl;
    return 0;
  }
  for (int i = 2; i * i <= B; i++)
    if (B % i == 0) {
      int cnt = 0;
      while (B % i == 0)
        B /= i, cnt++;
      cnt = (int)(cnt / A);
      while (cnt--)
        C *= i;
    }
  cout << C << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int A, B, C = 1;
  cin >> A >> B;
  if (A == 1) {
    cout << B << endl;
    return 0;
  }
  for (long long int i = 2; i * i <= B; i++)
    if (B % i == 0) {
      int cnt = 0;
      while (B % i == 0)
        B /= i, cnt++;
      cnt = (int)(cnt / A);
      while (cnt--)
        C *= i;
    }
  cout << C << endl;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int main() {
  ll n, P;
  cin >> n >> P;

  ll p = P;
  // map<ll,ll> mp;
  ll ans = 1;
  vector<ll> last;
  for (int i = 2; i * i <= p; i++) {
    ll tmp = 0;
    if (p % i == 0) {

      while (p % i == 0) {
        tmp++;
        p /= i;
      }
      // mp[i]=tmp;
    }
    if (tmp >= n) {
      ans *= pow((ll)i, tmp / n);
      // last.push_back(i);
    }
    if (p < pow((ll)i, n) || pow((ll)i, n) < 0)
      break;
  }

  if (p != 1) {
    ll tmp = 0;
    while (P % p == 0) {
      P /= p;
      tmp++;
    }
    if (tmp % n == 0)
      ans *= p;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int main() {
  ll n, P;
  cin >> n >> P;

  ll p = P;
  // map<ll,ll> mp;
  ll ans = 1;
  vector<ll> last;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  for (int i = 2; i * i <= p; i++) {
    ll tmp = 0;
    if (p % i == 0) {

      while (p % i == 0) {
        tmp++;
        p /= i;
      }
      // mp[i]=tmp;
    }
    if (tmp >= n) {
      ans *= pow((ll)i, tmp / n);
      // last.push_back(i);
    }
    if (p < pow((ll)i, n) || pow((ll)i, n) < 0)
      break;
  }

  if (p != 1) {
    ll tmp = 0;
    while (P % p == 0) {
      P /= p;
      tmp++;
    }
    if (tmp % n == 0)
      ans *= p;
  }

  cout << ans << endl;

  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, p;
  cin >> n >> p;
  ll tmp_num = p;
  vector<pair<ll, ll>> v_cnt;
  ll cnt_max = 0;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  for (ll i = 2; i <= p; ++i) {
    if (tmp_num <= 1) {
      break;
    }
    ll cnt = 0;
    while (1) {
      if (tmp_num % i != 0) {
        break;
      }
      tmp_num /= i;
      ++cnt;
    }
    if (cnt < n) {
      continue;
    }
    v_cnt.emplace_back(i, cnt / n);
  }
  //    for ( auto tmp_num : v_cnt ) {
  //        cout << tmp_num.first << " " << tmp_num.second << " " << endl;;
  //    }
  //    cout << endl;
  if (v_cnt.empty()) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = 1;
  for (auto tmp_num : v_cnt) {
    ll tmp_ans = 1;
    rep(ri, tmp_num.second) { tmp_ans *= tmp_num.first; }
    //        cout << tmp_ans << " " << loop_num << endl;
    ans *= tmp_ans;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, p;
  cin >> n >> p;
  ll tmp_num = p;
  vector<pair<ll, ll>> v_cnt;
  ll cnt_max = 0;
  if (n == 1) {
    cout << p << endl;
    return 0;
  }
  for (ll i = 2; i * i <= p; ++i) {
    if (tmp_num <= 1) {
      break;
    }
    ll cnt = 0;
    while (1) {
      if (tmp_num % i != 0) {
        break;
      }
      tmp_num /= i;
      ++cnt;
    }
    if (cnt < n) {
      continue;
    }
    v_cnt.emplace_back(i, cnt / n);
  }
  //    for ( auto tmp_num : v_cnt ) {
  //        cout << tmp_num.first << " " << tmp_num.second << " " << endl;;
  //    }
  //    cout << endl;
  if (v_cnt.empty()) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = 1;
  for (auto tmp_num : v_cnt) {
    ll tmp_ans = 1;
    rep(ri, tmp_num.second) { tmp_ans *= tmp_num.first; }
    //        cout << tmp_ans << " " << loop_num << endl;
    ans *= tmp_ans;
  }

  cout << ans << endl;

  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll power(ll x, ll y) {
  if (x == 1)
    return 1;
  if (y == 1)
    return x;
  if (x > 1000000)
    return 1000000000001;
  if (y > 40)
    return 1000000000001;
  ll res = 1;
  for (ll i = 0; i < y; i++) {
    if (res > 1000000000000) {
      res = 1000000000001;
      break;
    }
    res *= x;
  }
  return res;
}

int main() {
  ll N, P;
  cin >> N >> P;
  ll ans = 1;
  for (ll i = 1; power(i, N) <= P; i++) {
    if (P % power(i, N))
      continue;
    ans = i;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll power(ll x, ll y) {
  if (x == 1)
    return 1;
  if (y == 1)
    return x;
  if (x > 1000000)
    return 1000000000001;
  if (y > 40)
    return 1000000000001;
  ll res = 1;
  for (ll i = 0; i < y; i++) {
    if (res > 1000000000000) {
      res = 1000000000001;
      break;
    }
    res *= x;
  }
  return res;
}

int main() {
  ll N, P;
  cin >> N >> P;
  ll ans = 1;
  if (N == 1)
    ans = P;
  else {
    for (ll i = 1; power(i, N) <= P; i++) {
      if (P % power(i, N))
        continue;
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

long long sqr(long long n) { return static_cast<long long>(n) * n; }

long long bipow(long long c, long long n) {
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return c;
  } else if (n % 2 == 0) {
    return sqr(bipow(c, n / 2));
  } else {
    return (static_cast<long long>(c) * sqr(bipow(c, (n - 1) / 2)));
  }
}

void solve(long long N, long long P) {
  if (N >= 40) {
    std::cout << 1 << std::endl;
    return;
  }
  long long ans = 1;
  long long x = 1;
  while (true) {
    long long y = bipow(x, N);

    if (P < y) {
      break;
    }

    if (P % y == 0) {
      ans = x;
    }
    x++;
  }
  std::cout << ans << std::endl;
  return;
}

int main() {
  long long N;
  long long P;
  scanf("%lld", &N);
  scanf("%lld", &P);
  solve(N, P);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

long long sqr(long long n) { return static_cast<long long>(n) * n; }

long long bipow(long long c, long long n) {
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return c;
  } else if (n % 2 == 0) {
    return sqr(bipow(c, n / 2));
  } else {
    return (static_cast<long long>(c) * sqr(bipow(c, (n - 1) / 2)));
  }
}

void solve(long long N, long long P) {
  if (N >= 40) {
    std::cout << 1 << std::endl;
    return;
  }
  if (N == 1) {
    std::cout << P << std::endl;
    return;
  }
  long long ans = 1;
  long long x = 1;
  while (true) {
    long long y = bipow(x, N);

    if (P < y) {
      break;
    }

    if (P % y == 0) {
      ans = x;
    }
    x++;
  }
  std::cout << ans << std::endl;
  return;
}

int main() {
  long long N;
  long long P;
  scanf("%lld", &N);
  scanf("%lld", &P);
  solve(N, P);
  return 0;
}
Problem p03196 (C++)

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

using namespace std;

typedef long long ll;

ll n, p, g = 1;

int main() {
  cin >> n >> p;
  if (n == 1)
    return cout << p, 0;
  for (int i = 2; i * i <= p; i++)
    if (p % i == 0) {
      int c = 0;
      while (p % i == 0)
        p /= i, c++;
      for (int j = 0; j < (c / n); j++)
        g *= i;
    }
  cout << g;
  return 0;
}

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

using namespace std;

typedef long long ll;

ll n, p, g = 1;

int main() {
  cin >> n >> p;
  if (n == 1)
    return cout << p, 0;
  for (ll i = 2; i * i <= p; i++)
    if (p % i == 0) {
      int c = 0;
      while (p % i == 0)
        p /= i, c++;
      for (int j = 0; j < (c / n); j++)
        g *= i;
    }
  cout << g;
  return 0;
}
Problem p03196 (C++)

Buggy submission (status: Runtime Error):
// submitted by Himj
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i, s, e) for (i = s; i < e; i++)
#define ms(arr, val) memset(arr, val, sizeof(arr))
using namespace std;

const int mxn = 2e5 + 1;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, p, i, j, ans = 1;
    cin >> n >> p;
    j = 2;
    while (j * j <= p) {
      ll cnt = 0;
      while (p % j == 0) {
        p /= j;
        cnt++;
      }
      ans *= pow(j, cnt / n);
      j++;
    }
    if (n == 1)
      ans *= p;
    cout << ans;
  }
  return 0;
}


Fixed submission:
// submitted by Himj
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i, s, e) for (i = s; i < e; i++)
#define ms(arr, val) memset(arr, val, sizeof(arr))
using namespace std;

const int mxn = 2e5 + 1;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, p, i, j, ans = 1;
    cin >> n >> p;
    j = 2;
    while (j * j <= p) {
      ll cnt = 0;
      while (p % j == 0) {
        p /= j;
        cnt++;
      }
      ans *= pow(j, cnt / n);
      j++;
    }
    if (n == 1)
      ans *= p;
    cout << ans;
  }
  return 0;
}
Problem p03197 (Python)

Buggy submission (status: Runtime Error):
print("second" if all([int(input()) % 2 == 0 for i in range(input())]) else "first")


Fixed submission:
print(
    "second" if all([int(input()) % 2 == 0 for i in range(int(input()))]) else "first"
)
Problem p03197 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
A = list(map(int, input().split()))

print("second" if all(A[i] % 2 == 0 for i in range(n)) else "first")


Fixed submission:
n = int(input())
A = [int(input()) for i in range(n)]

print("second" if all(A[i] % 2 == 0 for i in range(n)) else "first")
Problem p03197 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[100005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (a[i] % 2)
      return puts("first");
  }
  puts("second");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[100005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (a[i] % 2) {
      puts("first");
      return 0;
    }
  }
  puts("second");
  return 0;
}
Problem p03198 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
vector<long long> solve(vector<int> A) {
  int N = A.size();
  vector<long long> dp(N);
  vector<double> diff(N - 1);
  set<int> four;
  for (int i = N - 2; i >= 0; --i) {
    dp[i] = dp[i + 1];
    diff[i] = double(A[i + 1]) / A[i];
    for (int j = i; j < N - 1; ++j) {
      while (diff[j] < 0.9999999999) {
        diff[j] *= 4;
        dp[i] += (N - j - 1) * 2;
        if (!four.empty()) {
          int ptr = *four.begin();
          dp[i] -= (N - ptr - 1) * 2;
          diff[ptr] /= 4;
          if (diff[ptr] < 3.9999999999)
            four.erase(ptr);
        }
      }
    }
    if (diff[i] >= 3.9999999999)
      four.insert(i);
  }
  return dp;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<long long> ldp = solve(A);
  reverse(A.begin(), A.end());
  vector<long long> rdp = solve(A);
  reverse(rdp.begin(), rdp.end());
  long long ans = min(ldp[0], rdp[N - 1] + N);
  for (int i = 0; i < N - 1; ++i) {
    ans = min(ans, ldp[i + 1] + rdp[i] + i + 1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
vector<long long> solve(vector<int> A) {
  int N = A.size();
  vector<long long> dp(N);
  vector<double> diff(N - 1);
  set<int> four;
  for (int i = N - 2; i >= 0; --i) {
    dp[i] = dp[i + 1];
    diff[i] = double(A[i + 1]) / A[i];
    while (diff[i] < 0.9999999999) {
      diff[i] *= 4;
      dp[i] += (N - i - 1) * 2;
      if (!four.empty()) {
        int ptr = *four.begin();
        dp[i] -= (N - ptr - 1) * 2;
        diff[ptr] /= 4;
        if (diff[ptr] < 3.9999999999)
          four.erase(ptr);
      }
    }
    if (diff[i] >= 3.9999999999)
      four.insert(i);
  }
  return dp;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<long long> ldp = solve(A);
  reverse(A.begin(), A.end());
  vector<long long> rdp = solve(A);
  reverse(rdp.begin(), rdp.end());
  long long ans = min(ldp[0], rdp[N - 1] + N);
  for (int i = 0; i < N - 1; ++i) {
    ans = min(ans, ldp[i + 1] + rdp[i] + i + 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p03198 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf (long long)(1e18)
using namespace std;

long long n;
vector<long long> a, pre, suf;

long long solve();

int main() {
  cin >> n;
  a.resize(n);
  for (auto &p : a)
    cin >> p;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  auto calc = [](vector<long long> &v) {
    int len = 16;
    vector<long long> res(n + 1, inf);
    vector<vector<long long>> dp(n + 1, vector<long long>(len + 1, inf));
    res[n] = res[n - 1] = 0;
    for (int i = 0; i <= len; ++i)
      dp[n - 1][i] = i;
    for (int i = n - 2; i >= 0; --i) {
      long long now = v[i];
      for (int j = 0; j <= len; ++j) {
        long long nxt = v[i + 1];
        for (int k = 0; k <= 60; ++k) {
          if (now <= nxt) {
            if (k <= len)
              dp[i][j] = dp[i + 1][k] + j;
            else
              dp[i][j] = dp[i + 1][len] + (k - len) * (n - 1 - i) + j;
            break;
          }
          nxt <<= 2;
        }
        assert(dp[i][j] != inf);
        now <<= 2;
      }
      res[i] = dp[i][0] << 1;
    }
    return res;
  };
  suf = calc(a);
  reverse(a.begin(), a.end());
  for (auto &p : a)
    p *= 2;
  pre = calc(a);
  reverse(pre.begin(), pre.end());
  for (int i = 0; i <= n; ++i)
    pre[i] += i;
  long long res = inf;
  for (int i = 0; i <= n; ++i)
    res = min(res, pre[i] + suf[i]);
  return res;
}

Fixed submission:
#include <bits/stdc++.h>
#define inf (long long)(1e18)
using namespace std;

long long n;
vector<long long> a, pre, suf;

long long solve();

int main() {
  cin >> n;
  a.resize(n);
  for (auto &p : a)
    cin >> p;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  auto calc = [](vector<long long> &v) {
    int len = 15;
    vector<long long> res(n + 1, inf);
    vector<vector<long long>> dp(n + 1, vector<long long>(len + 1, inf));
    res[n] = res[n - 1] = 0;
    for (int i = 0; i <= len; ++i)
      dp[n - 1][i] = i;
    for (int i = n - 2; i >= 0; --i) {
      long long now = v[i];
      for (int j = 0; j <= len; ++j) {
        long long nxt = v[i + 1];
        for (int k = 0; k <= 60; ++k) {
          if (now <= nxt) {
            if (k <= len)
              dp[i][j] = dp[i + 1][k] + j;
            else
              dp[i][j] = dp[i + 1][len] + (k - len) * (n - 1 - i) + j;
            break;
          }
          nxt <<= 2;
        }
        assert(dp[i][j] != inf);
        now <<= 2;
      }
      res[i] = dp[i][0] << 1;
    }
    return res;
  };
  suf = calc(a);
  reverse(a.begin(), a.end());
  for (auto &p : a)
    p *= 2;
  pre = calc(a);
  reverse(pre.begin(), pre.end());
  for (int i = 0; i <= n; ++i)
    pre[i] += i;
  long long res = inf;
  for (int i = 0; i <= n; ++i)
    res = min(res, pre[i] + suf[i]);
  return res;
}
Problem p03198 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll cnt[2][101010];

int main() {
  int n;
  cin >> n;
  ll a[n + 1];
  rep(i, n) cin >> a[i];
  rep(j, 2) {
    vector<pint> st;
    st.push_back({inf, 0});
    rep(i, n - 1) {
      int ret = 0;
      ll res = a[i];
      while (a[i + 1] > res) {
        res *= 4;
        ++ret;
      }
      cnt[j][i + 2] = cnt[j][i + 1];
      while (ret) {
        cnt[j][i + 2] += 2 * (i + 1 - st.back().second);
        if (--st.back().first == 0) {
          st.pop_back();
        }
        --ret;
      }
      res = a[i + 1];
      while (4 * res < a[i]) {
        res *= 4;
        ++ret;
      }
      if (ret)
        st.push_back({ret, i + 1});
    }
    reverse(a, a + n);
  }
  ll ans = longinf;
  rep(i, n + 1) { ans = min(ans, cnt[0][i] + cnt[1][n - i] + i); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll cnt[2][202020];

int main() {
  int n;
  cin >> n;
  ll a[n + 1];
  rep(i, n) cin >> a[i];
  rep(j, 2) {
    vector<pint> st;
    st.push_back({inf, 0});
    rep(i, n - 1) {
      int ret = 0;
      ll res = a[i];
      while (a[i + 1] > res) {
        res *= 4;
        ++ret;
      }
      cnt[j][i + 2] = cnt[j][i + 1];
      while (ret) {
        cnt[j][i + 2] += 2 * (i + 1 - st.back().second);
        if (--st.back().first == 0) {
          st.pop_back();
        }
        --ret;
      }
      res = a[i + 1];
      while (4 * res < a[i]) {
        res *= 4;
        ++ret;
      }
      if (ret)
        st.push_back({ret, i + 1});
    }
    reverse(a, a + n);
  }
  ll ans = longinf;
  rep(i, n + 1) { ans = min(ans, cnt[0][i] + cnt[1][n - i] + i); }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
//

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / __gcd(a, b) * b
#define range(a) (a).begin(), (a).end()
const int inf = 2147483647;

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'B' && s[i + 1] == 'W') {
      swap(s[i], s[i + 1]);
      i = -1;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
//

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / __gcd(a, b) * b
#define range(a) (a).begin(), (a).end()
const int inf = 2147483647;

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  /*    for(int i = 0;i<s.size();i++){
          if(s[i]=='B'&&s[i+1]=='W'){
              swap(s[i],s[i+1]);
              i=-1;
              ans++;
          }
      }
  */
  int o = 0;
  rep(i, 0, s.size()) {
    if (s.at(i) == 'W') {
      ans += i - o;
      o++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl

const ll mod = 1e9 + 7;
const ll inf = 1e18;

void pV(vector<ll> A) {
  cout << "[vector]";
  for (int i = 0; i < A.size(); i++) {
    cout << A[i] << " ";
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // input
  string S;
  cin >> S;

  // 後端のBを切り取る（終端はWとする）
  int L = S.size();
  for (int i = S.size() - 1; i >= 0; i--) {
    if (S[i] == 'B') {
      L--;
    } else {
      break;
    }
  }

  if (L == -1) {
    // Bしかないやん
    p(0);
    return 0;
  }

  int whiteIndex = L - 1;
  int blackIndex = L - 1;

  ll answer = 0;

  // search white
  while (true) {
    while (S[whiteIndex] != 'W') {
      whiteIndex--;
      if (whiteIndex == -1) {
        p(answer);
        return 0;
      }
    }
    while (S[blackIndex] != 'B') {
      blackIndex--;
      if (blackIndex == -1) {
        p(answer);
        return 0;
      }
    }
    swap(S[whiteIndex], S[blackIndex]);
    answer += (whiteIndex - blackIndex);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl

const ll mod = 1e9 + 7;
const ll inf = 1e18;

void pV(vector<ll> A) {
  cout << "[vector]";
  for (int i = 0; i < A.size(); i++) {
    cout << A[i] << " ";
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // input
  string S;
  cin >> S;

  // 後端のBを切り取る（終端はWとする）
  int L = S.size();
  for (int i = S.size() - 1; i >= 0; i--) {
    if (S[i] == 'B') {
      L--;
    } else {
      break;
    }
  }

  if (L <= 0) {
    // Bしかないやん
    p(0);
    return 0;
  }

  int whiteIndex = L - 1;
  int blackIndex = L - 1;

  ll answer = 0;

  // search white
  while (true) {
    while (S[whiteIndex] != 'W') {
      whiteIndex--;
      if (whiteIndex == -1) {
        p(answer);
        return 0;
      }
    }
    while (S[blackIndex] != 'B') {
      blackIndex--;
      if (blackIndex == -1) {
        p(answer);
        return 0;
      }
    }
    swap(S[whiteIndex], S[blackIndex]);
    answer += (whiteIndex - blackIndex);
  }

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  string S;
  cin >> S;
  //    string S = "BWBWBW";
  int ret = 0;
  bool isMoreFound = true;
  while (isMoreFound) {
    string newS = S;
    bool isAtLeastFound = false;
    for (int idx = 0; idx < S.size() - 1; idx++) {
      if (S[idx] == 'B' && S[idx + 1] == 'W') {
        newS[idx] = 'W';     // flip
        newS[idx + 1] = 'B'; // flip
        ret++;
        idx++; // skip idx + 1
        isAtLeastFound = true;
      }
    }
    isMoreFound = isAtLeastFound;
    S = newS;
  }
  cout << ret;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  string S;
  cin >> S;
  //    string S = "BWBWBW";
  long long ret = 0;
  long long leftB = 0;
  for (int idx = 0; idx < S.size(); idx++) {
    if (S[idx] == 'B')
      leftB++;
    else if (S[idx] == 'W')
      ret += leftB;
  }
  cout << ret;
  return 0;
}