alexjercan/bugnet huggingface dataset:
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  int x, m;
  vector<int> check(200001, 0);
  vector<long long> store(200001, 0);

  cin >> n >> x >> m;
  check[x] = 1;
  store[1] = x;
  long long next = x;
  long long ans = x;
  for (long long i = 2; i <= n; i++) {
    next = (next * next) % m;
    if (check[next]) {
      long long sum = 0;
      for (long long j = check[next]; j < i; j++) {
        sum += store[j];
      }
      long long length = i - check[next];
      long long togo = n - i + 1;
      ans += (togo / length) * sum;
      for (long long j = check[next]; j < check[next] + togo % length; j++) {
        ans += store[j];
      }
      break;
    } else {
      ans += next;
      check[next] = i;
      store[i] = next;
    }
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  int x, m;
  vector<int> check(m + 5, 0);
  vector<long long> store(m + 5, 0);

  cin >> n >> x >> m;
  check[x] = 1;
  store[1] = x;
  long long next = x;
  long long ans = x;
  for (long long i = 2; i <= n; i++) {
    next = (next * next) % m;
    if (check[next]) {
      long long sum = 0;
      for (long long j = check[next]; j < i; j++) {
        sum += store[j];
      }
      long long length = i - check[next];
      long long togo = n - i + 1;
      ans += (togo / length) * sum;
      for (long long j = check[next]; j < check[next] + togo % length; j++) {
        ans += store[j];
      }
      break;
    } else {
      ans += next;
      check[next] = i;
      store[i] = next;
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://atcoder.jp/contests/abc179/tasks/abc179_e
#include <algorithm>
#include <iostream>

using namespace std;

typedef unsigned long long ULL;

const int MAXN = 1e5 + 4;
const int MAXM = 1e5 + 4;
int idx[MAXM]; // 和位置索引
ULL sum[MAXN]; // 和

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

  // 打表
  ULL l, r;
  for (ULL i = 1, num = x;; num = num * num % m, i++) {
    if (!idx[num]) {
      // 没有计算过，标注已经计算
      idx[num] = i;
    } else {
      // 数据存在
      l = idx[num];
      r = i - 1;
      break;
    }
    // 更新数据表
    sum[i] = sum[i - 1] + num;
  }

  ULL len = r - l + 1;
  ULL ans = 0;
  ans += sum[min(n, l - 1)];

  n = max(0ULL, n - l + 1);
  if (n) {
    ans +=
        (sum[r] - sum[l - 1]) * (n / len) + (sum[n % len + l - 1] - sum[l - 1]);
  }

  cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc179/tasks/abc179_e
#include <iostream>

using namespace std;

typedef unsigned long long ULL;

ULL qmi(ULL a, ULL b, ULL mo) {
  ULL res = 1;
  while (b) {
    if (b & 1) {
      res = res * a % mo;
    }
    a = a * a % mo;
    b >>= 1;
  }
  return res;
}

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

  ULL ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += x;
    x = qmi(x, 2, m);
    // cout<<"n="<<i<<",sum="<<ans<<"\\n";
    if (0 == x) {
      break;
    }
  }

  cout << ans << "\\n";

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

#include <iostream>
#include <set>
using namespace std;
typedef long long LL;
int main() {
  LL N, X, M;
  LL S = 0;
  cin >> N >> X >> M;
  LL i;
  LL A = X;
  S += A;
  set<LL> mySet;
  set<LL>::iterator sit;
  mySet.insert(A);
  bool bClear = false;
  LL firstA = 0;
  for (i = 2; i <= N; i++) {
    A = (A * A) % M;
    sit = mySet.find(A);
    if (sit == mySet.end()) {
      mySet.insert(A);
    } else {
      if (!bClear) {
        mySet.clear();
        mySet.insert(A);
        firstA = A;
        bClear = true;
      } else {
        break;
      }
    }
    if (A == 0) {
      cout << S << endl;
      return 0;
    }
    S += A;
  }
  LL mySetNums = mySet.size();
  if (i < N && mySetNums > 0) {
    LL remain = N - i;
    LL nums = remain / mySetNums;
    i = N - remain % mySetNums;
    LL S1 = 0;
    for (sit = mySet.begin(); sit != mySet.end(); sit++) {
      S1 += (*sit);
    }
    S += S1 * nums;
    A = firstA;
    S += A;
    i++;
  }
  for (; i <= N; i++) {
    A = (A * A) % M;
    S += A;
  }
  cout << S << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long LL;
int main() {
  LL N, X, M;
  LL S = 0;
  cin >> N >> X >> M;
  LL i;
  LL A = X;
  S += A;
  for (i = 2; i <= N; i++) {
    A = (A * A) % M;
    S += A;
  }
  cout << S << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long N, X, M;
  cin >> N >> X >> M;

  long ans = 0, now = 0;
  vector<bool> used(M);
  vector<long> hist{-1};

  while (now < N) {
    if (used.at(X))
      break;
    else
      now++;
    ans += X;
    used.at(X) = true;
    hist.push_back(X);
    X = X * X % M;
  }

  if (now == N)
    return cout << ans << "\\n", 0;

  long x = -1;
  for (long i = 0; i < (long)hist.size(); i++) {
    if (hist.at(i) == X)
      x = i;
  }

  long rem = N - now;
  long len = now - x + 1;
  long sum = 0;
  for (long i = x; i <= now; i++)
    sum += hist.at(i);

  ans += rem / len * sum;
  rem %= len;

  for (long i = x; rem--; i++)
    ans += hist.at(i);
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

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

template <typename T, typename U> void cmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T, typename U> void cmin(T &a, U b) {
  if (a > b)
    a = b;
}

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  long N, X, M;
  cin >> N >> X >> M;

  vector<long> B(M);
  vector<long> R(1e6, -1);

  long ans = 0, now = 1;
  for (; now <= N; now++) {
    if (++B.at(X) == 2) {
      now--;
      break;
    }
    ans += X;
    R.at(now) = X;
    X = X * X % M;
  }

  if (now == N)
    return cout << ans << "\\n", 0;

  long x = -1;
  for (long i = 1; i < 1e6; i++) {
    if (R.at(i) == X) {
      x = i;
      break;
    }
  }
  long sum = 0;
  for (long i = x; i <= now; i++) {
    sum += R.at(i);
  }
  long kukan = now - x + 1;
  long nokori = N - now;
  ans += nokori / kukan * sum;

  nokori %= kukan;

  for (long i = 0; i < nokori; i++) {
    ans += R.at(x + i);
  }
  cout << ans << "\\n";
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(x) cerr << "[(" << __LINE__ << ") " << #x << "]: " << (x) << endl;

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

  ll n, x, MOD;
  cin >> n >> x >> MOD;

  vector<pair<ll, int>> m(MOD + 1);

  ll ans = 0;
  ll i = 1;
  while (!m[x].second && i <= n) {
    m[x] = {ans, i++};
    ans += x;
    x = x * x % MOD;
  }

  n -= i;
  if (n < 0) {
    cout << ans << "\\n";
    return 0;
  }
  ll duh = n / (i - m[x].second);
  ans += duh * (ans - m[x].first);
  n %= i - m[x].second;
  n++;

  while (n-- > 0) {
    ans += x;
    x = x * x % MOD;
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(x) cerr << "[(" << __LINE__ << ") " << #x << "]: " << (x) << endl;

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

  ll n, x, MOD;
  cin >> n >> x >> MOD;

  vector<pair<ll, int>> m(MOD + 1);

  ll ans = 0;
  ll i = 1;
  while (!m[x].first && i <= n) {
    m[x] = {ans, i++};
    ans += x;
    x = x * x % MOD;
  }

  n -= i;
  if (n < 0) {
    cout << ans << "\\n";
    return 0;
  }
  ll duh = n / (i - m[x].second);
  ans += duh * (ans - m[x].first);
  n %= i - m[x].second;
  n++;

  while (n-- > 0) {
    ans += x;
    x = x * x % MOD;
  }
  cout << ans << "\\n";
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
ll mm = 1000000000;
ll MM = mm + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589793

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  unordered_set<ll> num;
  map<ll, int> pl;
  vector<ll> sum(10000000);
  ll ans = 0;
  ans += x;
  num.insert(x);
  ll a = x;
  sum.at(0) = x;
  pl[x] = 0;

  ll b, c;
  bool d = false;
  rep(i, n - 1) {
    a = (a * a) % m;
    if (num.count(a)) {
      b = i;
      c = a;
      d = true;
      break;
    }
    num.insert(a);
    pl[a] = i + 1;
    ans += a;
    sum.at(i + 1) = ans;
  }
  if (d) {
    ll lest = n - b - 1;
    ll roop = b - pl.at(c) + 1;
    ll rosum;
    if (pl.at(c) == 0) {
      rosum = sum.at(b);
    } else
      rosum = sum.at(b) - sum.at(pl.at(c) - 1);
    ans += (lest / roop) * rosum;
    ll ama = lest % roop;
    if (ama != 0) {
      if (pl.at(c) == 0)
        ans += sum.at(pl.at(c) - 1 + ama);
      else
        ans += sum.at(pl.at(c) - 1 + ama) - sum.at(pl.at(c) - 1);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
ll mm = 1000000000;
ll MM = mm + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589793

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  unordered_set<ll> num;
  map<ll, ll> pl;
  vector<ll> sum(10000000);
  ll ans = 0;
  ans += x;
  num.insert(x);
  ll a = x;
  sum.at(0) = x;
  pl[x] = 0;

  ll b, c;
  bool d = false;
  rep(i, n - 1) {
    a = (a * a) % m;
    if (num.count(a)) {
      b = i;
      c = a;
      d = true;
      break;
    }
    num.insert(a);
    pl[a] = i + 1;
    ans += a;
    sum.at(i + 1) = ans;
  }
  if (d) {
    ll lest = n - b - 1;
    ll roop = b - pl.at(c) + 1;
    ll rosum = sum.at(b) - sum.at(pl.at(c) - 1);
    ans += (lest / roop) * rosum;
    ll ama = lest % roop;
    if (ama != 0) {
      ans += sum.at(pl.at(c) - 1 + ama) - sum.at(pl.at(c) - 1);
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll n, x, m;
  cin >> n >> x >> m;

  vector<ll> used(m, -1), dp(m);
  used[x] = n;
  dp[x] = x;
  n -= 1;
  bool flag = 1;
  ll ans = x;

  while (n > 0 && x) {
    x = x * x % m;
    ans += x;

    if (flag && used[x] != -1) {
      flag = 0;
      ll per = (used[x] - n);
      ll div = n / (used[x] - n);
      ll mod = n % (used[x] - n);
      if (per > n || div == 0)
        break;
      else if (mod == 0) {
        div -= 1;
        mod = per;
      }
      ans += (ans - dp[x]) * div;
      n = mod;
    } else {
      used[x] = n;
      dp[x] = ans;
    }
    --n;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll n, x, m;
  cin >> n >> x >> m;

  vector<ll> used(m, -1), dp(m);
  used[x] = n;
  dp[x] = x;
  n -= 1;
  bool flag = 1;
  ll ans = x;

  while (n && x) {
    x = x * x % m;
    ans += x;

    if (flag && used[x] != -1) {
      ans += (ans - dp[x]) * (n / (used[x] - n));
      n %= used[x] - n;
      flag = 0;
    } else {
      used[x] = n;
      dp[x] = ans;
    }
    --n;
  }

  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n, x, m;
  cin >> n >> x >> m;
  vector<ll> a(m * 2 + 5);
  a[0] = x;
  rep(i, m * 2 + 4) a[i + 1] = a[i] * a[i] % m;

  int s = 0;
  for (int i = m * 2 + 3; i >= 0; i--) {
    if (a[i] == a[m * 2 + 4]) {
      s = m * 2 + 4 - i;
      break;
    }
  }

  int st = -1;
  rep(i, m * 2 - s) {
    if (a[i] == a[i + s]) {
      st = i;
      break;
    }
  }

  rep(i, m * 2 + 4) a[i + 1] += a[i];
  if (n <= 2 * m + 4)
    cout << a[n - 1] << endl;
  else if (st == 0) {
    ll ans = a[s - 1] * (n / s);
    if (n % s)
      ans += a[n % s - 1];
    cout << ans << endl;
  } else {
    ll ans = (a[st + s] - a[st]) * ((n - st) / s) + a[st - 1];
    if ((n - st) % s != 0)
      ans += a[(n - st) % s + st - 1] - a[st - 1];
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n, x, m;
  cin >> n >> x >> m;
  vector<ll> a(m * 2 + 5);
  a[0] = x;
  rep(i, m * 2 + 4) a[i + 1] = a[i] * a[i] % m;

  int s = 0;
  for (int i = m * 2 + 3; i >= 0; i--) {
    if (a[i] == a[m * 2 + 4]) {
      s = m * 2 + 4 - i;
      break;
    }
  }

  int st = -1;
  rep(i, m * 2 - s) {
    if (a[i] == a[i + s]) {
      st = i;
      break;
    }
  }

  rep(i, m * 2 + 4) a[i + 1] += a[i];
  if (n <= 2 * m + 4)
    cout << a[n - 1] << endl;
  else if (st == 0) {
    assert(st < 0);
    ll ans = a[s - 1] * n / s;
    if (n % s)
      ans += a[n % s - 1];
    cout << ans << endl;
  } else {
    ll ans = (a[st + s] - a[st]) * ((n - st) / s) + a[st - 1];
    if ((n - st) % s != 0)
      ans += a[(n - st) % s + st - 1] - a[st - 1];
    cout << ans << endl;
  }

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

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

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;

map<ll, ll> mp;
vector<ll> vec;

int main() {
  ll n, x, m, ans = 0, cur = 0, val, rep, repVal = 0, need, vx, vy = 0;
  cin >> n >> x >> m;

  for (int i = 0; i < n; i++, cur++) {
    if (mp[x] != 0) {
      val = x;
      rep = (i + 1) - mp[x];
      break;
    }
    vec.push_back(x);
    mp[x] = i + 1;
    ans += x;
    x *= x;
    x %= m;
  }
  need = n - cur;
  if (need <= 0) {
    cout << ans;
  } else {
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i] == val) {
        break;
      }
      repVal += vec[i];
    }
    repVal = ans - repVal;
    vx = need / rep;
    ans += (repVal * vx);
    vx = need % rep;

    for (int i = 0; i < vec.size(); i++) {
      if (vx <= 0)
        break;
      if (vec[i] == val) {
        vy = 1;
      }
      if (vy) {
        ans += vec[i];
        vx--;
      }
    }
    cout << ans;
  }

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

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

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;

int main() {
  fastIO;
  ll n, x, m, ans = 0;
  cin >> n >> x >> m;

  for (int i = 0; i < n; i++) {
    ans += x;
    x *= x;
    x %= m;
  }
  cout << ans;

  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
long long n;
int m;
long long yu[200100];
long long sum[200100];
int pos[200100];
int hs = -1;
int he = -1;
int main() {
  scanf("%lld%lld%d", &n, &yu[1], &m); // printf("~~~\\n");
  for (int i = 0; i <= m; i++) {
    pos[i] = -1;
  }
  pos[yu[1]] = 1;
  sum[1] = yu[1];
  for (int i = 2; i <= m + 1; i++) {
    yu[i] = yu[i - 1] * yu[i - 1] % m;
    if (pos[yu[i]] == -1) {
      pos[yu[i]] = i;
    } else {
      if (hs == -1) {
        hs = pos[yu[i]];
        he = i - 1;
      } else {
      }
    }
    sum[i] = sum[i - 1] + yu[i];
  }
  if (n <= m + 1) {
    printf("%lld\\n", sum[n]);
    return 0;
  }

  long long ans = 0;

  //	printf("%d  %d %d~%d\\n",hs,he,yu[hs],yu[he+1]);
  ans = sum[hs - 1];
  n -= hs - 1;
  ans += (n / (he - hs + 1)) * (sum[he] - sum[hs - 1]);
  ans += sum[hs - 1 + (n % (he - hs + 1))] - sum[hs - 1];
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
long long n;
int m;
long long yu[100100];
long long sum[100100];
int pos[100100];
int hs = -1;
int he = -1;
int main() {
  scanf("%lld%lld%d", &n, &yu[1], &m);
  for (int i = 0; i <= n; i++) {
    pos[i] = -1;
  }
  pos[yu[1]] = 1;
  sum[1] = yu[1];
  for (int i = 2; i <= m + 1; i++) {
    yu[i] = yu[i - 1] * yu[i - 1] % m;
    if (pos[yu[i]] == -1) {
      pos[yu[i]] = i;
    } else {
      if (hs == -1) {
        hs = pos[yu[i]];
        he = i - 1;
      } else {
      }
    }
    sum[i] = sum[i - 1] + yu[i];
  }
  if (n <= m + 1) {
    printf("%lld\\n", sum[n]);
    return 0;
  }
  long long ans = 0;
  ans = sum[hs - 1];
  n -= hs - 1;
  ans += (n / he) * (sum[he] - sum[hs - 1]);
  ans += sum[hs - 1 + (n % he)] - sum[hs - 1];
  printf("%lld\\n", ans);
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
#define rep(i, n) for (usize i = 0; i < (usize)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i64 n, x, m;
  cin >> n >> x >> m;

  vector<vector<i64>> doubling(64, vector<i64>(m, -1));
  vector<vector<i64>> sum(64, vector<i64>(m, 0));
  rep(i, m) {
    doubling[0][i] = i * i % m;
    sum[0][i] = i;
  }
  rep(i, 63) {
    rep(j, m) {
      doubling[i + 1][j] = doubling[i][doubling[i][j]];
      sum[i + 1][j] = sum[i][j] + sum[i][doubling[i][j]];
    }
  }
  i64 ans = 0;
  i64 now = x;
  for (i64 i = 63; i >= 0; i--) {
    if (n & ((i64)(1) << i)) {
      ans += sum[i][now];
      now = doubling[i][now];
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
#define rep(i, n) for (usize i = 0; i < (usize)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i64 n, x, m;
  cin >> n >> x >> m;

  vector<vector<i64>> doubling(64, vector<i64>(m, -1));
  vector<vector<i64>> sum(64, vector<i64>(m, 0));
  rep(i, m) {
    doubling[0][i] = i * i % m;
    sum[0][i] = i;
  }
  rep(i, 64) {
    rep(j, m) {
      doubling[i + 1][j] = doubling[i][doubling[i][j]];
      sum[i + 1][j] = sum[i][j] + sum[i][doubling[i][j]];
    }
  }
  i64 ans = 0;
  i64 now = x;
  for (i64 i = 63; i >= 0; i--) {
    if (n && ((i64)(1) << 1)) {
      ans += sum[i][now];
      now = doubling[i][now];
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

int main() {
  lint N, X, M;
  cin >> N >> X >> M;

  vector<int> t(M, -1);
  vector<lint> S = {X};
  t[X] = 0;

  lint A = X;
  for_(i, 0, M) {
    A = (A * A) % M;
    if (t[A] == -1) {
      S.push_back(A);
      t[A] = i + 1;
    } else {
      break;
    }
  }

  int piv = t[A];
  lint ans = 0;
  int rep = (int)min(N, (lint)piv);
  for_(i, 0, rep) ans += S[i];

  // cerr << ans << " " << A << " " << piv << " " << S.size() << endl;

  if (N - piv > 0) {
    N -= piv;
    int m = S.size() - piv;
    lint tmp = N / m;
    // cerr << N << " " << m << " " << tmp << endl;
    for_(i, piv, (int)S.size()) ans += S[i] * tmp;
    tmp = N % m;
    for_(i, piv, piv + (int)tmp) ans += S[i];
  }

  cout << ans << endl;
}

Error: -6
5e0ab4fb-76d5-4b8d-86cb-87dc3ddb2316.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02550/C++/s825611422.cpp:25: int main(): Assertion `false' failed.


Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

int main() {
  lint N, X, M;
  cin >> N >> X >> M;

  vector<int> t(M, -1);
  vector<lint> S = {X};
  t[X] = 0;

  lint A = X;
  for_(i, 0, M) {
    A = (A * A) % M;
    if (t[A] == -1) {
      S.push_back(A);
      t[A] = i + 1;
    } else {
      assert(false);
      break;
    }
  }

  int piv = t[A];
  lint ans = 0;
  for_(i, 0, min((int)N, piv)) ans += S[i];

  // cerr << ans << " " << A << " " << piv << " " << S.size() << endl;

  if (N - piv > 0) {
    N -= piv;
    int m = S.size() - piv;
    int tmp = N / m;
    // cerr << N << " " << m << " " << tmp << endl;
    for_(i, piv, (int)S.size()) ans += S[i] * tmp;
    tmp = N % m;
    for_(i, piv, piv + tmp) ans += S[i];
  }

  cout << ans << endl;
}