alexjercan/bugnet huggingface dataset:
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
long long fast(long long a, long long b, long long p) {
  long long ans = 1;
  long long cur = a;
  while (b) {
    if (b & 1) {
      ans *= cur;
      ans %= p;
    }
    cur *= cur;
    cur %= p;
    b >>= 1;
  }
  return ans;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    cout << fast(n, m, 1e9 + 7) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int INF = 1e9;

int main() {
  int m, n;
  cin >> m >> n;
  long ans = 1;
  rep(i, n) { ans = ans * m % 1000000007; }
  cout << ans << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define ll long long int
using namespace std;

const ll md = 1000000007;

ll power(ll b, ll p, ll m) {
  ll d;
  if (p == 0)
    return 1;
  d = power(b, p / 2, m);
  if (p % 2 == 0)
    return (d % m * d % m) % m;
  else
    return (b % m * (d % m * d % m) % m) % m;
}

int main() {
  ll a, b, d;
  scanf("%lld %lld", &a, &b);
  d = power(a, b, md);
  printf("%lld\\n", d);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r 1000000007
int main() {
  long long m, n, i, j, k;
  while (cin >> m >> n) {
    long long ans = 1;
    for (i = 1; i <= n; i++) {
      ans = (ans * m) % r;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
using namespace std;

int main() {
  long long a1, p, b, a;
  while (cin >> a >> b) {
    a1 = a % mod;
    p = 1;
    while (b > 0) {
      if (b % 2 == 1) {
        p *= a1;
        p = p % mod;
      }
      b /= 2;
      a1 = (a1 * a1) % mod;
    }
    cout << p << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r 1000000007
int main() {
  long long m, n, i, j, k;
  while (cin >> m >> n) {
    long long ans = 1;
    for (i = 1; i <= n; i++) {
      ans = (ans * m) % r;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
typedef unsigned long long l;
l p(l x, l n) {
  if (!n)
    return 1;
  l r = p(x * x % M, n / 2);
  if (n % 2)
    r = r * x % M;
  return r;
}
int main() {
  l a, b;
  cin >> a >> b;
  cout << p(a, b) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long m, n, ans = 1;
  cin >> m >> n;
  for (int i = 0; i < n; i++) {
    ans *= m;
    if (ans > 1000000000)
      ans %= 1000000007;
  }
  cout << ans << endl;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

#define L 1000000007

int main() {
  int m, n, dig;
  long dp[32], p = 1;
  scanf("%d%d", &m, &n);
  dp[0] = m;
  if (n % 2 != 0) {
    p = m;
  }
  for (int i = 1; i < 32; i++) {
    if (n < (1 << i)) {
      dig = i;
      break;
    }
    dp[i] = (dp[i - 1] * dp[i - 1]) % L;
    if (n & (1 << i)) {
      p = (p * dp[i]) % L;
    }
  }
  printf("%ld\\n", p);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

unsigned int div(unsigned long long int a, unsigned long long int b,
                 unsigned int p) {
  return ((a % p) * (b % p)) % p;
}

int main() {
  const unsigned int p = 1000000007;
  int m, n;
  cin >> m >> n;
  int size = (int)log2(n);
  unsigned long long int rem = m;
  for (int i = 0; i < size; i++) {
    rem = div(rem, rem, p);
  }
  for (int i = 0; i < n - pow(2, size); i++) {
    rem = div(rem, m, p);
  }
  cout << rem << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

int main() {
  long long m, n;
  long long mod = 1;
  scanf("%lld %lld", &m, &n);

  while (n > 0) {
    if (n % 2) {
      mod = mod * m % 1000000007;
      n--;
    } else {
      m = m * m % 1000000007;
      n /= 2;
    }
  }
  // mod = mod * m %1000000007;
  printf("%lld\\n", mod);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  long long m, n;
  long long mod = 0;
  scanf("%lld %lld", &m, &n);

  while (n > 0) {
    if (n % 2) {
      mod = mod * n % 1000000007;
    } else {
      m = m * m % 1000000007;
      n /= 2;
    }
  }
  mod = mod * m % 1000000007;
  printf("%lld\\n", mod);

  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
typedef unsigned long long int lint;
const int num = 1000000007;
inline int prime(lint m, lint n) {
  int ans = 1;
  while (n != 0) {
    if (n % 2 == 1)
      ans = ans * m % num;
    m = m * m % num;
    n = n / 2;
  }
  return ans;
}
int main() {
  lint m, n;
  cin >> m >> n;
  cout << prime(m, n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int cntne = 2, cntpre = 1;
inline unsigned long long int power(unsigned long long int l,
                                    unsigned long long int m,
                                    unsigned long long int n) {
  cntne *= 2;
  cntpre *= 2;
  m *= m;
  if (m > 1000000007)
    m %= 1000000007;
  if (cntne > n) {
    for (int i = cntpre; i < n; i++) {
      m *= l;
      if (m > 1000000007)
        m %= 1000000007;
    }
    return m % 1000000007;
  } else
    power(l, m, n);
}
int main() {
  unsigned long long int m, n;
  cin >> m >> n;
  cout << power(m, m, n) << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool judge(int s) {
  for (int i = 2; i * i <= s; i++) {
    if (s % i != 0 && (i + 1) * (i + 1) > s) {
      return true;
    }
    if (s % i == 0) {
      return false;
      break;
    }
  }
}

int main() {
  unsigned long long a;
  int m, n, b;
  cin >> m >> n;
  a = m;
  b = 1000000007;

  int l = n;
  int k[1000] = {};

  if (judge(l)) {
    k[0] = l;
  }

  else {
    int count = 0;
    for (int j = 2;;) {
      if (l == 1) {
        break;
      }
      if (l % j == 0) {
        l = l / j;
        k[count] = j;
        count++;
        continue;
      }
      j++;
    }
  }

  for (int j = 0; k[j] != 0; j++) {
    for (int i = 1; i < k[j]; i++) {
      a = a * m;
      if (a >= b) {
        a = a % b;
        if (a == m && k[j] - i > k[j] % i) {
          k[j] = k[j] % i;
          i = 0;
        }
        if (a == 1) {
          break;
        }
      }
    }
    m = a;
  }

  cout << a << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

bool judge(int s) {
  for (int i = 2; i * i <= s; i++) {
    if (s % i != 0 && (i + 1) * (i + 1) > s) {
      return true;
    }
    if (s % i == 0) {
      return false;
      break;
    }
  }
}

int main() {
  unsigned long long a;
  int m, n, b;
  cin >> m >> n;
  a = m;
  b = 1000000007;

  int l = n;
  int k[1000] = {};

  if (judge(l)) {
    k[0] = l;
  }

  else {
    int count = 0;
    for (int j = 2;;) {
      if (l == 1) {
        break;
      }
      if (l % j == 0 && judge(j)) {
        l = l / j;
        k[count] = j;
        count++;
        continue;
      }
      j++;
    }
  }

  for (int j = 0; k[j] != 0; j++) {
    for (int i = 1; i < k[j]; i++) {
      a = a * m;
      if (a >= b) {
        a = a % b;
        if (a == m && k[j] - i > k[j] % i) {
          k[j] = k[j] % i;
          i = 0;
        }
        if (a == 1) {
          break;
        }
      }
    }
    m = a;
  }

  cout << a << endl;

  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int m = 0, k = 0;
  unsigned long long n = 0, l = 1, l1 = 0;

  cin >> m >> n;

  while (n > 10000) {
    k = (int)log2(n);
    l1 = m;

    for (int i = 0; i < k; i++)
      l1 = (l1 * l1) % 1000000007;

    n = n - (int)pow(2, k);
    l = (l * l1) % 1000000007;
  }

  for (int j = 0; j < n; j++) {
    l *= m;
    l = l % 1000000007;
  }

  cout << l << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int m = 0, k = 0;
  unsigned long long n = 0, l = 0;

  cin >> m >> n;

  k = (int)log2(n);
  l = m;

  for (int i = 0; i < k; i++) {
    l *= l;
    l = l % 1000000007;
  }

  for (unsigned long long i = 0; i < n - pow(2, k); i++) {
    l *= m;
    l = l % 1000000007;
  }

  cout << l << endl;

  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 114514810
#define FOR(i, r, n) for (int i = (r); i < (n); i++)
#define REP(i, n) FOR(i, (0), n)

using namespace std;

int m, n;
long long int cx = 1, cy = 1;

int main() {
  cin >> m >> n;
  cy = m;
  for (int i = 0; i < 5000; i++) {
    if (!n) {
      cout << cx << endl;
      return 0;
    }
    if (n & 1)
      cx = cx * cy % 1000000007;
    cy = cy * cy % 1000000007;
    n >>= 1;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 114514810
#define FOR(i, r, n) for (int i = (r); i < (n); i++)
#define REP(i, n) FOR(i, (0), n)

using namespace std;

int m, n;
long long int x = 1;

int main() {
  cin >> m >> n;
  for (int i = 0; i < n; i++) {
    x = x * m % 1000000007;
  }
  cout << x << endl;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define mod 1000000007
#define ll long long
using namespace std;

int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  ll c = a, d = 1;
  while (b != 1) {
    if (b & 1)
      d = c * d % mod;
    c = c * c % mod;
    b /= 2;
  }
  printf("%lld\\n", c * d % mod);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define mod 1000000007
#define ll long long
using namespace std;

int main() {
  int a, b;
  while (scanf("%d%d", &a, &b)) {
    ll c = a, d = 1;
    while (b != 1) {
      if (b & 1)
        d = c * d % mod;
      c = c * c % mod;
      b /= 2;
    }
    printf("%lld\\n", c * d % mod);
  }
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
long long int dp[100000] = {0}, m;
long long int pow(long long int n) {
  if (n == 1)
    return m;
  long long int mid = n / 2;
  if (n >= 100000)
    return pow(n - mid) * pow(mid) % 1000000007;
  if (dp[n] != 0)
    return dp[n];
  return dp[n] = (pow(n - mid) * pow(mid)) % 1000000007;
}
int main() {
  int n;
  scanf("%lld%d", &m, &n);
  printf("%lld\\n", pow(n));
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  unsigned long long int n, m, ans = 1;
  scanf("%llu%llu", &m, &n);
  for (int i = 0; i < n; i++) {
    ans *= m;
    ans %= 1000000007;
  }
  printf("%llu\\n", ans);
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
typedef long long ll;
/*
  AOJ power
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_B&lang=jp
  繰り返し2乗法
*/

ll power(int m, int n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return m * power(m, n - 1) % ll(1e9 + 7);
  ll k = power(m, n / 2);
  return k * k % ll(1e9 + 7);
}
int main() {
  int n, m;
  cin >> m >> n;
  cout << power(m, n) << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
typedef long long ll;
/*
  AOJ power
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_B&lang=jp
  繰り返し2乗法
*/

ll power(int m, int n) {
  ll ans = 1;
  while (n > 0) {
    if (n == 1)
      return (ans * m) % ll(1e9 + 7);
    ans *= m * m;
    ans %= ll(1e9 + 7);
    n -= 2;
  }
  return ans;
}
int main() {
  int n, m;
  cin >> m >> n;
  cout << power(m, n) << "\\n";
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < b; i++)
#define INF 1000000000
#define MAX_N 100000000
using namespace std;
bool debug = false;

unsigned long long solve(unsigned long long a, long long b, long long n) {
  unsigned long long tmp = 1;
  if (b > 0) {
    tmp = solve(a, b / 2, n);
    if (b % 2 == 0)
      tmp = (tmp * tmp) % n;
    else
      tmp = (((tmp * tmp) % n) * a) % n;
  }
  return tmp;
}

int main() {
  long long a, b;
  cin >> a >> b;
  cout << solve(a, b, 1000000007) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < b; i++)
#define INF 1000000000
#define MAX_N 100000000
using namespace std;
bool debug = false;

int main() {
  int a, b;
  long long ans = 1;
  cin >> a >> b;
  for (int i = 1; i <= b; i++) {
    ans = ans * a % 1000000007;
    if (i != 1 && b % i == 0 && i * 2 <= b) {
      ans = ans * ans % 1000000007;
      i *= 2;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
const long long INF = INT_MAX / 2;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define ll long long int
using namespace std;
ll ans = 0, sum = 0, n, k, cnt = 0, w, h;
string s;
vector<ll> v;
vector<pair<ll, ll>> vp;
vector<vector<ll>> vv(50, vector<ll>(50, INF));
vector<string> vs;
vector<char> vc;
set<ll> st;
map<char, ll> mp;

/*--------------------template--------------------*/

ll power(ll x, ll n, ll M) {
  ll tmp = 1;
  if (n > 0) {
    tmp = power(x, n / 2, M);
    if (n % 2 == 0)
      tmp = (tmp * tmp) % M;
    else
      tmp = (((tmp * tmp) % M) * x) % M;
  }
  return tmp;
}

int main() {
  cin >> n >> k;
  cout << power(n, k, MOD) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const long long INF = INT_MAX / 2;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define ll long long int
using namespace std;
ll ans = 0, sum = 0, n, k, cnt = 0, w, h;
string s;
vector<ll> v;
vector<pair<ll, ll>> vp;
vector<vector<ll>> vv(50, vector<ll>(50, INF));
vector<string> vs;
vector<char> vc;
set<ll> st;
map<char, ll> mp;

/*--------------------template--------------------*/

int main() {
  cin >> n >> k;
  ans = 1;
  FOR(i, 1, k + 1) {
    ans *= n;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n, m) for (int(i) = (n); (i) < (m); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y)                                                           \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << endl;
#define debugB(x, y) cerr << #x << ": " << bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

const ll INF = 1e9 + 7;

ll f(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = ((ans % INF) * (a % INF)) % INF;
    }
    a = ((a % INF) * (a % INF)) % INF;
    b /= 2;
  }
  return ans;
}

int main() {
  ll n, m;
  cin >> n >> m;
  cout << f(n, m) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n, m) for (int(i) = (n); (i) < (m); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y)                                                           \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << endl;
#define debugB(x, y) cerr << #x << ": " << bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

const ll INF = 1e9 + 7;

// int pow(int x, int n) { // x^n
//    int ans = 1;
//    while (n > 0) {
//       if (n&1) ans *= x;
//       x *= x;
//       n >>= 1;
//    }
//    return ans;
// }

ll pow2(ll x, ll n) {
  ll ans = 1;
  rep(i, 0, n) { ans = ((ans % INF) * (x % INF)) % INF; }
  return ans;
}

int main() {
  ll n, m;
  cin >> n >> m;
  cout << pow2(n, m) << endl;

  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

long long int mod_pow(long long int x, int n, int mod) {
  long long int ret = 1;
  while (n > 0) {
    if (!(n % 2 == 0))
      ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  const int mod = 1000000007;
  int m, n;
  cin >> m >> n;
  cout << mod_pow(m, n, mod) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int mo, n, i;
  long long int m, ans;
  cin >> m >> n;
  mo = m;
  for (i = 1; i <= n; i++) {
    if (m >= 1000000007)
      break;
    m *= mo;
  }
  if (m <= 1000000007 && i != n + 1) {
    ans = mo;
  } else if (m <= 1000000007 && i == n + 1) {
    ans = m / mo;
  } else {
    ans = m % 1000000007;
    for (; i < n; i++) {
      ans %= 1000000007;
      ans *= mo;
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>

constexpr unsigned long Q = 1000000007;

unsigned long rpow(unsigned long x, int n) {
  if (n == 0) {
    return 1;
  }
  unsigned long result = rpow(x * x % Q, n / 2);
  if (n % 2 == 0) {
    result %= Q;
  } else {
    result = result * x % Q;
  }
  return result;
}

int main() {
  int m, n;
  scanf("%d %d", &m, &n);
  printf("%ld\\n", rpow(m, n));
}

Error: TLE

Fixed submission:

#include <cstdio>

constexpr long Q = 1000000007;

int main() {
  long l = 1;
  int m, n;
  scanf("%d %d", &m, &n);
  for (int i = 0; i < n; i++) {
    l *= m;
    if (l > Q) {
      l %= Q;
    }
  }
  printf("%ld\\n", l);
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

typedef long long ll;
#define DIV 1000000007

ll power(ll m, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1) {
    return power(m * m % DIV, n / 2) % DIV * m % DIV;
  } else {
    return power(m * m % DIV, n / 2);
  }
}

int main() {
  int m, n;
  cin >> m >> n;
  ll surplus = power(m, n) % DIV;

  cout << surplus << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define DIV 1000000007

int main() {
  int m, n;
  cin >> m >> n;
  long product = m;

  for (int i = 1; i < n; ++i) {
    product *= m;
    product %= DIV;
  }
  cout << product << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#define X 1000000007
typedef long long ll;
using namespace std;

ll powdiv(ll m, ll n) {
  if (n == 1) {
    return m % X;
  }
  if (n % 2 == 0) {
    ll a = powdiv(m, n / 2) % X;
    return a * a % X;
  } else {
    ll a = powdiv(m, n / 2) % X;
    return a * a % X * m % X;
  }
}

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

  cout << powdiv(m, n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define X 1000000007
typedef long long ll;
using namespace std;

int main() {
  ll m, n;
  cin >> m >> n;
  ll rem = 1;

  for (ll i = 0; i < n; i++) {
    rem = (rem * m) % X;
  }

  cout << rem << endl;

  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

long long power(int x, int y) {
  int retn = 1;
  for (int i = 0; i < y; ++i) {
    retn *= x;
  }
  return retn;
}

int main() {
  int m, n;
  cin >> m >> n;

  const long long mod = 1000000007;
  long long rem = m;
  int cnt = 0;
  for (int i = 30; i > 0; --i) {

    if (n / power(2, i) == 1) {
      ++cnt;
      if (cnt == 1) {
        rem = (rem * rem) % mod;
      } else {
        rem = (m * rem) % mod;
        rem = (rem * rem) % mod;
      }
      n -= power(2, i);
    }

    else {
      if (cnt) {
        rem = (rem * rem) % mod;
      }
    }
    // cout<<i<<" "<<n<<" "<<rem<<"\\n";
  }

  if (n == 1) {
    rem = (m * rem) % mod;
    n--;
  }

  if (n == 0) {
    cout << rem << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int m, n;
  cin >> m >> n;

  const long long mod = 1000000007;
  long long rem = 1;
  for (int i = 0; i < n; ++i) {
    rem = (m * rem) % mod;
  }

  cout << rem << "\\n";
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

const long long mod = 1000000007LL;

long long powmod(long long m, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      (res *= m) %= mod;
    n >>= 1;
    (m *= m) %= mod;
  }
  return res;
}

int main() {
  long long m, n;
  cin >> m >> n;
  cout << powmod(m, n) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long long m, n;
  cin >> m >> n;
  long long ans = 1;
  for (int i = 0; i < n; i++) {
    ans *= m;
    ans %= 1000000007;
  }
  cout << ans << endl;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define PB push_back
#define MP make_pair
using namespace std;
long long mod = 1e9 + 7;
long long mult(long long a, long long p) {
  if (p == 1)
    return a;
  else if (p % 2)
    return (mult(a, p - 1) % mod) * (a % mod);
  long long b = mult(a, p / 2);
  b %= mod;
  return b * b;
}
int main() {
  long long int n, m;
  cin >> m >> n;
  m %= mod;
  cout << mult(m, n) % mod << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define PB push_back
#define MP make_pair
using namespace std;
int main() {
  long long int n, m, p = 1;
  long long int mod = 1000000007;
  cin >> m >> n;
  m %= mod;
  n %= mod;
  REP(i, n) p = (p % mod) * (m % mod);
  cout << p % mod << endl;
}
Problem p02468 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define int long long
#define MOD 1000000007
// #define _DEBUG
using namespace std;
int N, M;
int mod_times(int a, int b) { return ((a % MOD) * (b % MOD)) % MOD; }
int pow(int n, int p) {
  // cout << n << "," << p << endl;
  if (p == 1) {
    return n;
  } else if (p % 2 == 0) {
    int n2 = pow(n, p / 2);
    return mod_times(n2, n2);
  } else {
    return mod_times(pow(n, p - 1), n);
  }
}
signed main() {
  while (cin >> N >> M) {
    cout << pow(N, M) << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#define int long long
#define MOD 1000000007
// #define _DEBUG
using namespace std;
int N, M;
int mod_times(int a, int b) { return ((a % MOD) * (b % MOD)) % MOD; }
int pow(int n, int p) {
  if (p == 1) {
    return n;
  } else if (!p % 2) {
    int n2 = pow(n, p / 2);
    return mod_times(n2, n2);
  } else {
    return mod_times(pow(n, p - 1), n);
  }
}
signed main() {
  while (cin >> N >> M) {
    cout << pow(N, M) << endl;
  }
}