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

#include <cmath>
#include <iostream>

using namespace std;
typedef unsigned long long ullong;

ullong power(ullong x, ullong n, ullong M) {
  ullong 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;
}

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

  cout << power(m, n, 1000000007) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define DIV 1000000007

int mod(int m, int n) {
  int x, y = 0, z = 0, i, j;
  /*printf("mod(%d,%d): called\\n",m,n);*/
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return m;
  } else {
    if (m > DIV - m)
      m = DIV - m;
    for (i = 0; i < m;
         i++) { /*????????¢???????????§???????????????????????§?????????*/
      y += m;
      if (y > DIV)
        y = y % DIV;
    }
    x = mod(y, n / 2) % DIV;
    if (n % 2 == 0) {
      return x;
    } else {
      for (j = 0; j < m; j++) {
        z += x;
        if (z > DIV)
          z = z % DIV;
      }
      return z;
    }
  }
}

int main() {
  int m, n;
  scanf("%d %d", &m, &n);
  printf("%d\\n", mod(m, n));
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define DIV 1000000007

typedef unsigned long long int verylong;

verylong mod(verylong m, int n) {
  verylong x, y = 0, z = 0, i, j;
  /*printf("mod(%d,%d): called\\n",m,n);*/
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return m;
  } else {
    x = mod((m * m) % DIV, n / 2) % DIV;
    if (n % 2 == 0) {
      return x;
    } else {
      return (x * m) % DIV;
    }
  }
}

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

Error: TLE

Fixed submission:

#include <stdio.h>
#define DIV 1000000007

typedef unsigned long long int verylong;

verylong mod(verylong m, int n) {
  verylong x, y = 0, z = 0, i, j;
  /*printf("mod(%d,%d): called\\n",m,n);*/
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return m;
  } else {
    if (m > DIV - m)
      m = DIV - m;
    for (i = 0; i < m;
         i++) { /*????????¢???????????§???????????????????????§?????????*/
      y += m;
      if (y > DIV)
        y = y % DIV;
    }
    x = mod(y, n / 2) % DIV;
    if (n % 2 == 0) {
      return x;
    } else {
      for (j = 0; j < m; j++) {
        z += x;
        if (z > DIV)
          z = z % DIV;
      }
      return z;
    }
  }
}

int main() {
  int m;
  int n;
  scanf("%d %d", &m, &n);
  printf("%d\\n", mod(m, n));
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MAXN 1001024
#define INF 1000000007
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
// 素数列挙
ll prime[MAXN]; // segment_sieve [a,b)
bool is_prime[MAXN];
int sieve(int n) {
  int res = 0;
  fill(is_prime, is_prime + MAXN, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (!is_prime[i])
      continue;
    prime[res++] = i;
    for (int j = 2 * i; j <= n; j += i)
      is_prime[j] = false;
  }
  return res;
}
bool is_prime_ab[MAXN];
ll segment_sieve(ll a, ll b) {
  fill(is_prime, is_prime + MAXN, true);
  fill(is_prime_ab, is_prime_ab + MAXN, true);
  for (int i = 2; i * i <= b - 1; i++) {
    if (!is_prime[i])
      continue;
    for (ll j = 2 * i; j * j <= b - 1; j += i)
      is_prime[j] = false;
    for (ll j = a - a % i; j < b; j += i) {
      if (j < a)
        continue;
      if (is_prime_ab[j - a])
        is_prime_ab[j - a] = false;
    }
  }
  ll res = 0;
  for (ll i = a; i < b; i++)
    if (is_prime_ab[i - a])
      prime[res++] = i;
  return res;
}

bool is_primef(ll n) {
  if (n == 1)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}

vector<ll> divisors(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    res.push_back(i);
    if (n / i == i)
      continue;
    res.push_back(n / i);
  }
  return res;
}
map<ll, ll> prime_factors(ll n) {
  map<ll, ll> res;
  if (n <= 1) {
    res[n] = 1;
    return res;
  }

  // for(int i=2,_n = n;i*i<=_n;i++){
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

// 1からnまででnと互いにそなものの数
ll euler_phi(ll n) {
  ll cnt = n;
  map<ll, ll> res = prime_factors(n);
  for (auto it = res.begin(), end = res.end(); it != end; it++) {
    cnt = cnt / it->first * (it->first - 1);
  }
  return cnt;
}
ll pow(ll n, ll m) {
  vector<ll> a(33);
  a[0] = n;
  for (int i = 1; i < 32; i++) {
    a[i] = (a[i - 1] * a[i - 1]) % INF;
  }
  ll res = 1;
  for (int i = 0; i < 32; i++) {
    if ((m & (1 << i))) {
      res = (res * a[i]) % INF;
    }
  }
  return res;
}

int main() {
  ll n, m;
  cin >> n >> m;
  cout << pow(n, m) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
int main(void) {
  int m, n, i, p, l = 1000000007;
  double x;
  scanf("%d %d", &m, &n);
  x = m;
  for (i = 1; i < n; i++) {
    x = x * m;
    if (x > l) {
      x = fmod(x, l);
    }
  }
  p = x;
  printf("%d\\n", p);
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdint>
#include <cstdio>

using namespace std;

constexpr int limit = 1'000'000'007;
constexpr int exp_max = 29;

int main() {
  int m, n;
  scanf("%d %d", &m, &n);

  int64_t res = m;
  int64_t m_exp[exp_max];
  m_exp[0] = m;
  int count = 1;
  int i;
  double sup = log(n) / log(2);
  for (i = 1; i <= sup; i++) {
    res *= res;
    res = res % limit;
    m_exp[i] = res;
    count *= 2;
    // printf("%d %lld %d\\n", i, res, count); fflush(stdout);
  }
  for (int j = i - 2; count < n; j--) {
    if (n - count < pow(2, j)) {
      continue;
    }
    res *= m_exp[j];
    res = res % limit;
    count += pow(2, j);
  }
  printf("%lld\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <cstdio>

using namespace std;

constexpr int limit = 1'000'000'007;

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

#include <cmath>
#include <iostream>

using namespace std;
typedef unsigned long long ullong;

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

int main() {
  int m, n;
  cin >> m >> n;
  cout << power(m, n, 1000000007) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#include <iostream>

using namespace std;

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

  long long int res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= m;
    }
    res %= 1000000007;
    m *= m;
    m %= 1000000007;
    n >>= 1;
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <limits>

using namespace std;

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

  long long int res = 1;
  for (int i = 0; i < n; i++) {
    res *= m;
    if (res >= numeric_limits<long long int>::max() / m) {
      res %= 1000000007;
    }
  }

  res %= 1000000007;
  cout << res << endl;

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a, t) for (int a = 0; a < t; ++a)
#define forever while (true)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define int_maxvalue numeric_limits<int>::max()
ll mod = 1000000007;
ll m;
ll Pow(ll n) {
  if (n == 1) {
    return m;
  }
  ll p = Pow(n / 2) % mod;
  p *= p;
  p %= mod;
  if (n % 2 == 1) {
    p *= m;
  }
  return p % mod;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> m >> n;
  // for (int i = 1; i < 1000; i++)
  { cout << Pow(n) << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a, t) for (int a = 0; a < t; ++a)
#define forever while (true)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define int_maxvalue numeric_limits<int>::max()
ll mod = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll m, n;
  cin >> m >> n;
  ll a = 1;
  if (n % 3 == 1) {
    a = m;
  }
  if (n % 3 == 2) {
    a = m * m;
  }
  for (ll i = 0; i < n / 3; i++) {
    a *= m * m * m;
    a %= mod;
  }
  cout << a << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define sz size()
#define print(X) cout << (X) << endl
#define fprint(NUM, X) cout << fixed << setprecision(NUM) << (X) << endl
#define fprints(NUM, X, Y)                                                     \\
  cout << fixed << setprecision(NUM) << (X) << " " << (Y) << endl
static const int INF = 1e+9 + 7;
ll n, m, l;
string s, t;
ll d[200010], dp[1010][1010];
double w[1000], v[1000];
double box[200010];

long long mod_pow(unsigned long long x, unsigned long long y,
                  unsigned long long mod) {
  unsigned long long ans = 1;
  while (y > 0) {
    if (y & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}

int main() {
  cin >> n >> m;
  cout << mod_pow(n, m, INF) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define sz size()
#define print(X) cout << (X) << endl
#define fprint(NUM, X) cout << fixed << setprecision(NUM) << (X) << endl
#define fprints(NUM, X, Y)                                                     \\
  cout << fixed << setprecision(NUM) << (X) << " " << (Y) << endl
static const int INF = 1e+9 + 7;
ll n, m, l;
string s, t;
ll d[200010], dp[1010][1010];
double w[1000], v[1000];
double box[200010];

int main() {
  cin >> n >> m;
  l = 1;
  while (m--) {
    l = (l * n) % INF;
  }
  print(l);
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define M 1000000007

long rec(long m, long n) {
  if (n == 1)
    return m % M;
  long long m2 = rec((m % M) * (m % M), n / 2);
  if (n % 2 == 1) {
    m2 = (m2 * (m % M));
  }
  return m2 % M;
}

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


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long m, n, l = 1;
  cin >> m >> n;
  m = m % 1000000007;

  long long s = (m * m) % 1000000007;
  long long i = n / 2;

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

  if (n % 2 == 1) {
    l = (l * m) % 1000000007;
    cout << l << endl;
  } else {
    cout << l << endl;
  }
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

// template ///////////////////////////////////////////////////////////
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/time.h>
using namespace std;

class timer {
  enum KIND { REAL, CPU }; // real_time or cpu_time
  double t0;               // start
  double t1;               // end
  KIND kind_of_time;       // default is real_time
  double get_realtime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + (double)tv.tv_usec * 1e-6;
  }
  double get_cputime() {
    struct rusage RU;
    getrusage(RUSAGE_SELF, &RU);
    return RU.ru_utime.tv_sec + (double)RU.ru_utime.tv_usec * 1e-6;
  }

public:
  timer() { kind_of_time = REAL; }
  void set(KIND k) { kind_of_time = k; }
  void start() { t0 = get_realtime(); }
  void stop() { t1 = get_realtime(); }
  void print() { printf("TotalTime : %.3fsec\\n", t1 - t0); }
};
////////////////////////////////////////////////////////////////////////
typedef long long int ll;

ll pow(ll x, int n);
ll rem(ll x, ll n, int mod);

#define MOD 1000000007

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

  ll r;
  T.start();
  r = rem(m, n, MOD);
  T.stop();

  cout << r << endl;
  // T.print();

  return 0;
}

ll rem(ll x, ll n, int mod) {
  if (!n)
    return 1;
  long long res;
  res = rem(x * x % mod, n / 2, mod);
  if (n % 2)
    res = res * x % mod;
  return res;
}

ll pow(ll x, int n) {
  if (!n)
    return 1;
  ll res;
  res = pow(x * x, n / 2);
  if (n % 2)
    res *= x;
  return res;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define QUOTI 1000000007

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

  long long int r = 1;
  for (int i = 0; i < n; i++) {
    r = r * m % QUOTI;
    // if( r > QUOTI ) r %= QUOTI;
  }
  cout << r << endl;

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

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

int power(int m, int n) {
  long long res = 1, mm = m;
  while (n) {
    if (n & 1)
      res = (res * mm) % MOD;
    mm = (mm * mm) % MOD;
    n >>= 1;
  }
  return (int)res;
}

int main() {
  int m, n;
  cin >> m >> n;
  cout << power(m, n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int power(int m, int n) {
  long long res = 1, mm = m, nn = n;
  while (n) {
    if (n & 1)
      res = (res * mm) % MOD;
    mm = (mm * mm) % MOD;
    nn >>= 1;
  }
  return (int)res;
}

int main() {
  int m, n;
  cin >> m >> n;
  cout << power(m, n) << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

const int M = 1000000007;

long power(long x, long n) {
  long res;
  if (n == 0)
    return 1;

  res = power(x * x % M, n / 2);
  if ((n & 1) != 0) {
    res = res * x % M;
  }
  return res;
}
int main() {
  int m, n;
  cin >> m >> n;
  cout << power(m, n) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

ll x = 1000000007;

int waru(ll n, ll m) {
  if (m == 1)
    return n % x;
  else {
    return (waru(n, m - 1) * n + x) % x;
  }
}

int main() {
  ll n, m;
  cin >> n >> m; // mのn乗
  cout << (waru(n, m) + x) % x << endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define MOD 1000000007LL

long long power(int a, int b) {
  if (b == 0) {
    return 1LL;
  }

  if (b % 2 == 1) {
    return power(a, b / 2) * power(a, b / 2) % MOD * a % MOD;
  }

  return power(a, b / 2) * power(a, b / 2) % MOD;
}

int main() {
  int n, m;

  cin >> m >> n;

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

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define MOD 1000000007LL

long long power(int a, int b) {
  if (b == 0) {
    return 1LL;
  }

  return power(a, b - 1) * a % MOD;
}

int main() {
  int n, m;

  cin >> m >> n;

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

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  int m, n;
  long long ans1 = 1, ans2 = 1, ans = 1;
  int div = 1000000007;
  while (cin >> m >> n) {
    for (int i = 0; i < 50; i++) {
      ans1 *= m;
      if (ans1 > div)
        ans1 = ans1 % div;
    }
    for (int j = 0; j < n % 50; j++) {
      ans2 *= m;
      if (ans2 > div)
        ans2 = ans2 % div;
    }
    for (int k = 0; k < n / 50; k++) {
      ans *= ans1;
      if (ans > div)
        ans = ans % div;
    }
    ans *= ans2;
    ans = ans % div;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

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

#include <iostream>
#include <math.h>
using namespace std;
int a[31] = {};
long long c[31] = {};
int main() {
  long long m, n;
  cin >> m >> n;
  int max = 0, e = n;
  for (int i = 30; i >= 0; i--) {
    a[i] = e / (long long)pow(2, i);
    e = e % (long long)pow(2, i);
    if (a[i] == 1 && max == 0)
      max = i;
  }
  long long k = 1, b = m;
  int l = 0;
  while (l < max) {
    c[l] = b;
    b = b * b;
    k = k * 2;
    l++;
    if (b > 1000000007)
      b = b % 1000000007;
  }
  for (int i = 0; i < max; i++) {
    if (a[i] == 1)
      b = b * c[i];
    if (b > 1000000007)
      b = b % 1000000007;
  }
  cout << b << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long m, n;
  cin >> m >> n;
  long long k = 1, b = m;
  while (n > k) {
    while (b < 1000000007 && k * 2 <= n) {
      b = b * b;
      k = k * 2;
    }
    if (k * 2 > n) {
      while (b < 1000000007 && k < n) {
        b = b * m;
        k++;
      }
    }
    b = b % 1000000007;
  }
  cout << b << endl;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

long pow(long x, long y) {
  if (y == 0) {
    return 1;
  } else if (y == 1) {
    return x;
  } else if (y % 2 == 0) {
    long z = pow(x, y / 2) % 1000000007;
    return z * z % 1000000007;

  } else {
    long z = pow(x, y / 2) % 1000000007;
    return (z * z % 1000000007) * x % 1000000007;
  }
}

int main() {
  long x, y;
  std::cin >> x >> y;
  std::cout << pow(x, y) << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

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

int main() {
  long x, y;
  std::cin >> x >> y;
  std::cout << pow(x, y) << std::endl;
  return 0;
}
Problem p02468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

long power_surp(long, long, long);

int main() {
  int m;
  long n;
  long surp; // surplus
  long a = 1000000007;

  cin >> m >> n;

  surp = power_surp(m, n, a);

  cout << surp << endl;
  return 0;
}

long power_surp(long x, long n, long a) {
  long tmp = 1;

  if (n > 0)
    tmp = power_surp(x * x % a, n / 2, a);
  if (n % 2 == 1)
    tmp = tmp * x % a;

  return tmp;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int m;
  long n;
  int surp = 1; // surplus
  long a = 1000000007;

  cin >> m >> n;

  for (int i = 0; i < n; ++i) {
    surp = (surp * (m % a)) % a;
  }

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

#include <math.h>
#include <stdio.h>

int main(void) {
  long long n, i, m, mt, mt2;
  scanf("%lld%lld", &m, &n);
  mt = m, mt2 = m;
  m = 1;
  long long a = log2(n), b = n - (int)pow(2, a);
  while (a) {
    for (i = 0; i < a; ++i) {
      mt *= mt;
      mt %= 1000000007;
    }
    a = log2(!b ? 1 : b);
    m *= mt;
    m %= 1000000007;
    if (a)
      b = b - (int)pow(2, a);
    else if (b) {
      m *= mt2;
      m %= 1000000007;
    }
    mt = mt2;
  }
  printf("%lld\\n", m);
  return 0;
}


Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>

int main(void) {
  long long n, i, m, mt, mt2;
  scanf("%lld%lld", &m, &n);
  mt = m, mt2 = m;
  m = 1;
  long long a = log2(n), b = n - (int)pow(2, a);
  while (a) {
    for (i = 0; i < a; ++i) {
      mt *= mt;
      mt %= 1000000007;
    }
    a = log2(b);
    m *= mt;
    m %= 1000000007;
    if (a)
      b = b - (int)pow(2, a);
    else if (b) {
      m *= mt2;
      m %= 1000000007;
    }
    mt = mt2;
  }
  printf("%lld\\n", m);
  return 0;
}