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

/*Author : Yuvraj Singh
 * Patience is Power
 *
 * */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define ar array
ll power(ll x, ll y) {
  ll temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return ((temp % mod) * (temp % mod)) % mod;
  else
    return ((x % mod) * (temp % mod) * (temp % mod)) % mod;
}

int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }

bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

#define mm 998244353
void solve() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll res = 0;
  ll i = 0;
  vector<ll> v;
  ll tmp = x;
  vector<ll>::iterator it;
  while (i < n) {
    it = find(v.begin(), v.end(), tmp);
    if (it != v.end())
      break;
    res += tmp;
    v.push_back(tmp);
    tmp = tmp * tmp % m;
    i++;
  }
  if (i == n) {
    cout << res;
    return;
  } else {
    ll sum = 0;
    ll diff = v.end() - it;
    for (auto tt = it; tt != v.end(); tt++)
      sum += *tt;
    ll d = (n - i) / diff;
    res += d * sum;
    ll rim = (n - i) % diff;
    auto tt = it;
    for (ll j = 0; j < rim; j++) {
      res += *tt;
      ++tt;
    }
    cout << res;
    return;
  }
}

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

  // int t; cin>>t;
  // while(t--)
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/*Author : Yuvraj Singh
 * Patience is Power
 *
 * */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define ar array
ll power(ll x, ll y) {
  ll temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return ((temp % mod) * (temp % mod)) % mod;
  else
    return ((x % mod) * (temp % mod) * (temp % mod)) % mod;
}

int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }

bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

#define mm 998244353
void solve() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll res = 0;
  ll i = 0;
  vector<ll> v;
  ll tmp = x;
  auto it = v.begin();
  while (i < n) {
    it = find(v.begin(), v.end(), tmp);
    if (it != v.end())
      break;
    res += tmp;
    v.push_back(tmp);
    tmp = tmp * tmp % m;
    i++;
  }
  if (i == n) {
    cout << res;
    return;
  } else {
    while (i < n) {

      for (auto tt = it; tt != v.end() && i < n; tt++) {
        res += *tt;
        i++;
      }
    }
    cout << res;
    return;
  }
}

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

  // int t; cin>>t;
  // while(t--)
  solve();
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

#define trav(a, A) for (auto &a : A)
#define f first
#define s second

typedef long long ll;

ll MOD = 1e+9 + 7;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const ll MAXN = 1e+5 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll i, j, n, x, m, ans = 0;
  cin >> n >> x >> m;
  vector<ll> rm(m, -1);
  for (i = 1; i < n && rm[x] == -1; ++i)
    ans += x, rm[x] = (x * x) % m, x = rm[x];
  if (i == n) {
    ans += x;
    cout << ans << '\\n';
    return 0;
  }
  ll per = 1, qnt = x, ax = x;
  for (ax = rm[x]; ax != x; ax = rm[ax], ++per)
    qnt += ax;
  ans += qnt * ((n - i) / per);
  for (j = 0; j < (n - i) % per; ++j)
    ans += x, x = rm[x];
  ans += x;
  cout << ans << '\\n';
  // trav (a, rm) cout << a << ' ';
  // cout << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

#define trav(a, A) for (auto &a : A)
#define f first
#define s second

typedef long long ll;

ll MOD = 1e+9 + 7;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const ll MAXN = 1e+5 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll i, j, n, x, m, ans = 0;
  cin >> n >> x >> m;
  vector<ll> rm(m, -1);
  for (i = 1; i < n && rm[x] == -1; ++i)
    ans += x, rm[x] = (x * x) % m, x = rm[x];
  if (i == n) {
    ans += x;
    cout << ans << '\\n';
  }
  ll per = 1, qnt = x, ax = x;
  for (ax = rm[x]; ax != x; ax = rm[ax], ++per)
    qnt += ax;
  ans += qnt * ((n - i) / per);
  for (j = 0; j < (n - i) % per; ++j)
    ans += x, rm[x] = (x * x) % m, x = rm[x];
  ans += x;
  cout << ans << '\\n';
  // trav (a, rm) cout << a << ' ';
  // cout << '\\n';

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

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

int main() {
  long int N, X, M;
  cin >> N >> X >> M;
  if (M > 1) {
    vector<long int> vec(2 * M);
    vec.at(0) = X % M;
    for (long int i = 1; i <= 2 * M - 1; i++) {
      long int k = vec.at(i - 1) % M;
      long int l = (k * k) % M;
      vec.at(i) = l;
    }
    long int s = 0;
    long int t = 0;
    for (long int p = 0; p < 2 * M - 2; p++) {
      if (s > 0) {
        break;
      }
      for (long int q = p + 1; q < 2 * M - 1; q++) {
        if (vec.at(p) == vec.at(q)) {
          s = p;
          t = q;
          break;
        }
      }
    }
    long int r = t - s;

    long int a = 0;
    if (r > 1) {
      for (long int b = s; b < t; b++) {
        a += vec.at(b);
      }
    } else {
      a = vec.at(s);
    }

    long int A = 0;
    for (long int w = 0; w < s; w++) {
      A += vec.at(w);
    }

    long int f = 0;
    f = (N - s) / r;

    long int d = 0;
    if ((N - s) % r != 0) {
      for (long int y = s; y <= s + (N - s) % r - 1; y++) {
        d += vec.at(y);
      }
    }
    A += a * f + d;
    cout << A << endl;
  } else if (M == 1) {
    cout << 0 << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  long int N, X, M;
  cin >> N >> X >> M;
  if (M > 1) {
    vector<long int> vec(2 * M);
    vec.at(0) = X % M;
    for (long int i = 1; i <= 2 * M - 1; i++) {
      long int k = vec.at(i - 1) % M;
      long int l = (k * k) % M;
      vec.at(i) = l;
    }
    long int s = 0;
    long int t = 0;
    for (long int p = 0; p < 2 * M - 2; p++) {
      if (s > 0) {
        break;
      }
      for (long int q = p + 1; q < 2 * M - 1; q++) {
        if (vec.at(p) == vec.at(q)) {
          s = p;
          t = q;
          break;
        }
      }
    }
    long int r = t - s;

    long int a = 0;
    if (r > 1) {
      for (long int b = s; b < t; b++) {
        a += vec.at(b);
      }
    } else {
      a = vec.at(s);
    }

    long int A = 0;
    for (long int w = 0; w < s; w++) {
      A += vec.at(w);
    }

    long int f = 0;
    f = (N - s) / r;

    long int d = 0;
    if ((N - s) % r != 0) {
      for (long int y = s; y <= s + (N - s) % r - 1; y++) {
        d += vec.at(y);
      }
    }
    A += a * f + d;
    cout << A << endl;
  } else if (M == 1) {
    int o = X;
    for (int h = 1; h <= N - 1; h++) {
      o += pow(X, 2 * h);
    }
    cout << o << endl;
  }
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ll = long long;

int main() {

  // ここから入力
  ll n, ans = 0;
  ll x, m, b = 0, cnt = 0;
  bool lp = false;
  cin >> n >> x >> m;
  vector<ll> aa;
  vector<int> checked(m + 1, 0);

  int i = 0;
  while (i < n) {
    ans += x;
    checked.at(x)++;
    x = (x * x) % m;
    cnt++;
    if (x == 0)
      break;
    if (checked.at(x) == 2) {
      aa.push_back(x);
      b += x;
      lp = true;
    }
    if (checked.at(x) == 3) {
      break;
    }
    i++;
  }

  ans -= 3 * b;
  if (lp) {
    ll c = aa.size();
    n -= (cnt - 3 * c);
    ll kakeru = n / c;
    ll amari = n % c;
    ans += b * kakeru;
    REP(i, amari) ans += aa[i];
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ll = long long;

int main() {

  // ここから入力
  ll n, ans = 0;
  ll x, m, b = 0, c = 0, d = 0, e = 0, f = 0, cnt = 1;
  cin >> n >> x >> m;
  vector<ll> a(m + 1);
  vector<ll> aa;
  vector<int> checked(m + 1, 0);

  a[0] = x;
  checked[x] += 1;
  int i = 1;
  while (i < n) {
    x = (x * x) % m;
    a.at(i) = x;
    cnt++;
    if (x == 0)
      break;
    if (checked.at(x) == 2) {
      aa.push_back(x);
      b = i;
      c++;
      d += x;
    }
    if (checked.at(x) == 3) {
      break;
    }

    checked.at(x) += 1;
    i++;
  }

  if (b == 0) {
    REP(i, cnt) {
      // cout << a[i] << endl;
      ans += a.at(i);
    }
  } else {
    REP(i, m + 1) {
      if (checked.at(i) == 1) {
        // cout << i << endl;
        e++;
        ans += i;
      }
    }

    // cout << ans << endl;
    n -= e;
    ll kakeru = n / c;
    ll amari = n % c;

    ans += d * kakeru;

    REP(i, amari) { ans += aa.at(i); }
    // cout << kakeru << endl;
  }

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
int main() {
  long long int n, x, m, ans = 0, f = 0, b = 0, c = 0, d = 0, t = 0;
  cin >> n >> x >> m;
  t = x;
  vector<long long int> a(1000000);
  for (long long int i = 0; i < m; i++) {
    if (a[x] == 0) {
      a[x]++;
      x = (x * x) % m;
    } else {
      d = i;
      break;
    }
  }
  for (long long int i = 0; i < m; i++) {
    if (t == x && f == 0) {
      c = i;
      f = 1;
      b += t;
      t = (t * t) % m;
    } else if (f == 0) {
      ans += t;
      t = (t * t) % m;
    } else if (f == 1 && t == x) {
      break;
    } else {
      b += t;
      t = (t * t) % m;
    }
  }
  if (a[0] == 0) {
    ans += ((n - c) / (d - c)) * b;
    for (long long int i = 0; i < (n - c) % (d - c); i++) {
      ans += t;
      t = (t * t) % m;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
int main() {
  long long int n, x, m, ans = 0, f = 0, b = 0, c = 0, d = 0, t = 0;
  cin >> n >> x >> m;
  t = x;
  vector<long long int> a(1000000);
  for (long long int i = 0; i < n; i++) {
    if (a[x] == 0) {
      a[x]++;
      x = (x * x) % m;
    } else {
      d = i;
      break;
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (t == x && f == 0) {
      c = i;
      f = 1;
      b += t;
      t = (t * t) % m;
    } else if (f == 0) {
      ans += t;
      t = (t * t) % m;
    } else if (f == 1 && t == x) {
      break;
    } else {
      b += t;
      t = (t * t) % m;
    }
  }
  if (x != 0) {
    ans += ((n - c) / (d - c)) * b;
    for (long long int i = 0; i < (n - c) % (d - c); i++) {
      ans += t;
      t = (t * t) % m;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef long long int LL;
LL c[110000], p[110000];
int main(void) {
  LL n, i, j;
  LL ans = 0, x, m;
  bool u = false;
  for (i = 0; i < 110000; i++) {
    c[i] = 0;
    p[i] = -1;
  }
  cin >> n >> x >> m;
  for (i = 0; i < n; i++) {
    ans += x;
    if (p[x] == -1 || u) {
      c[x] = ans; // 答え保管
      p[x] = i;   // 場所保管
      x = x * x % m;
    } else {
      u = true;
      j = (n - i - 1) / (i - p[x]); // 高速ループ回数
      ans += (ans - c[x]) * j;
      i += j * (i - p[x]); //*/
      x = x * x % m;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef long long int LL;
LL c[110000], p[110000];
int main(void) {
  LL n, i, j;
  LL ans = 0, x, m;
  for (i = 0; i < 110000; i++) {
    c[i] = 0;
    p[i] = -1;
  }
  cin >> n >> x >> m;
  for (i = 0; i < n; i++) {
    ans += x;
    if (p[x] == -1) {
      c[x] = ans; // 答え保管
      p[x] = i;   // 場所保管
      x = x * x % m;
    } else {
      j = (n - i) / (i - p[x]); // 高速ループ回数
      ans += (ans - c[x]) * j;
      i += j * (i - p[i]); //*/
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  if (n <= m * 1000 || n < (ll)1e8) {
    vector<ll> a(n + 1);
    a[1] = x;
    for (int i = 2; i <= n; i++) {
      a[i] = a[i - 1] * a[i - 1] % m;
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++)
      ans += a[i];
    cout << ans << endl;
    return 0;
  }

  vector<ll> A;
  set<ll> st;
  A.push_back(x);
  st.insert(x);
  ll t, sz_A = 1;
  while (1) { // 高々m回
    t = A[sz_A - 1] * A[sz_A - 1] % m;
    if (st.find(t) != st.end())
      break;
    A.push_back(t);
    st.insert(t);
    sz_A++;
  }

  int i;
  for (i = 0; i < sz_A; i++)
    if (A[i] == t)
      break;

  int T = sz_A - i;

  ll ans = 0;
  for (int j = 0; j < i; j++)
    ans += A[j];

  ll q = (n - i) / T, r = (n - i) % T;
  ll sum = 0;
  for (int j = i; j < sz_A; j++)
    sum += A[j];
  sum *= q;
  for (int j = i; j < i + r; j++)
    sum += A[j];

  ans += sum;

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  if (n <= m * 1000 || n < (ll)1e8) {
    vector<ll> a(n + 1);
    a[1] = x;
    for (int i = 2; i <= n; i++) {
      a[i] = a[i - 1] * a[i - 1] % m;
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++)
      ans += a[i];
    cout << ans << endl;
    return 0;
  }

  vector<ll> A, a_sort;
  A.push_back(x);
  a_sort.push_back(x);
  ll t, sz_A = 1;
  while (1) { // 高々m回
    t = A[sz_A - 1] * A[sz_A - 1] % m;
    auto it = lower_bound(a_sort.begin(), a_sort.end(), t);
    if (it != a_sort.end() && *it == t)
      break;
    A.push_back(t);
    a_sort.push_back(t);
    sz_A++;
    sort(a_sort.begin(), a_sort.end());
  }

  int i;
  for (i = 0; i < sz_A; i++)
    if (A[i] == t)
      break;

  int T = sz_A - i;

  ll ans = 0;
  for (int j = 0; j < i; j++)
    ans += A[j];

  ll q = (n - i) / T, r = (n - i) % T;
  ll sum = 0;
  for (int j = i; j < sz_A; j++)
    sum += A[j];
  sum *= q;
  for (int j = i; j < i + r; j++)
    sum += A[j];

  ans += sum;

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

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

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  vector<ll> A(2 * M + 20);
  set<ll> S;
  S.insert(X);
  A[0] = X;
  ll l = 0, r = 0;
  for (ll i = 1;; i++) {
    A[i] = ((A[i - 1] % M) * (A[i - 1] % M)) % M;
    if (S.find(A[i]) != S.end()) {
      for (ll j = 0;; j++) {
        if (A[i] == A[j]) {
          l = j;
          r = i;
          break;
        }
      }
    }
    if (r != 0)
      break;
    S.insert(A[i]);
  }
  ll w = r - l;
  ll s = 0;
  for (ll i = l; i < r; i++)
    s += A[i];
  ll ans = 0;
  ll n = 0;
  for (ll i = 0; i < l && n < N; i++) {
    ans += A[i];
    n++;
  }
  ans += s * ((N - n) / w);
  n += w * ((N - n) / w);
  for (ll i = l; n < N; i++) {
    ans += A[i];
    n++;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  vector<ll> A(2 * M + 20);
  set<ll> S;
  S.insert(X);
  A[0] = X;
  ll l = 0, r = 0;
  for (ll i = 1;; i++) {
    A[i] = (A[i - 1] * A[i - 1]) % M;
    if (S.find(A[i]) != S.end()) {
      for (ll j = 0; j < N; j++) {
        if (A[i] == A[j]) {
          l = j;
          r = i;
          break;
        }
      }
    }
    if (r != 0)
      break;
    S.insert(A[i]);
  }
  ll w = r - l;
  ll s = 0;
  for (ll i = l; i < r; i++)
    s += A[i];
  ll ans = 0;
  ll n = 0;
  for (ll i = 0; i < l && n < N; i++) {
    ans += A[i];
    n++;
  }
  ans += s * ((N - n) / w);
  n += w * ((N - n) / w);
  for (ll i = l; n < N; i++) {
    ans += A[i];
    n++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long long n;
  int x, m;
  cin >> n >> x >> m;
  int prv = x;
  vector<int> rep;
  rep.reserve((int)1e6);
  vector<bool> vis(m);
  while (!vis[prv]) {
    vis[prv] = 1;
    rep.push_back(prv);
    prv = (long long)prv * prv % m;
  }
  int i = 0;
  uint64_t ans = 0;
  for (; i < (int)rep.size() && n; ++i) {
    if (rep[i] == prv)
      break;
    ans += rep[i];
    --n;
  }
  if (n) {
    ll period = (int)rep.size() - i;
    ll divs = n / period;
    ll left = n % period;
    assert(i + left <= (int)rep.size());
    ans += ((long long)divs *
                accumulate(rep.begin() + i, rep.end(), (long long)0) +
            accumulate(rep.begin() + i, rep.begin() + i + left, (long long)0));
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

// THINK: INT vs LONG LONG?

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

  ll n, x, m;
  cin >> n >> x >> m;
  if (n == 1LL) {
    cout << x << endl;
    return 0;
  }
  if (x == 0LL) {
    cout << 0 << endl;
    return 0;
  }
  // powers modulo m are cyclical - find period?
  long long fi = x;
  long long se = ((long long)x * x) % m;
  long long prv = se;
  vector<long long> rep;
  rep.reserve((int)1e6);
  while (true) {
    prv = ((long long)prv * prv) % m;
    if (prv == (long long)se * se % m && !rep.empty())
      break;
    rep.push_back(prv);
  }
  // HANDLE WHEN N = 1
  n -= 2;
  long long ans = fi + se;
  ll period = (ll)rep.size();
  if (period == 0) {
    return 0;
  }
  ll divs = n / period;
  ll left = n % period;
  ans += ((long long)divs * accumulate(rep.begin(), rep.end(), (long long)0));
  for (int i = 0; i < left; ++i) {
    ans += rep[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 09/19/20 20:59:59
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll n;
  int x, m;
  cin >> n >> x >> m;
  vector<ll> A = {x};
  A.reserve(m);
  int s = 0;
  {
    vector<int> used(m, -1);
    used[x] = 0;
    while (true) {
      ll a = A.back() * A.back() % m;
      if (used[a] == -1) {
        used[a] = A.size();
        A.push_back(a);
      } else {
        s = used[a];
        break;
      }
    }
  }
  int f = A.size();
  for (int i = 1; i < s; i++) {
    A[i] += A[i - 1];
  }
  for (int i = s + 1; i < f; i++) {
    A[i] += A[i - 1];
  }
  ll ans = 0;
  if (s > 0)
    ans += A[min((ll)s - 1, n - 1)];
  if (n > s) {
    ll l = n - s;
    ll k = f - s;
    ll c = l / k;
    ll d = l % k;
    ans += c * A[f - 1] + (d > 0 ? A[s + d - 1] : 0);
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 09/19/20 20:59:59
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> used(m, -1);
  vector<ll> A(m);
  A[0] = x;
  int s = 0;
  used[x] = 0;
  for (int i = 1; i < m; i++) {
    ll a = A[i - 1] * A[i - 1] % m;
    if (used[a] == -1) {
      A[i] = a;
      used[a] = i;
    } else {
      A.resize(i);
      s = used[a];
      break;
    }
  }
  int f = A.size();
  for (int i = 1; i < s; i++) {
    A[i] += A[i - 1];
  }
  for (int i = s + 1; i < f; i++) {
    A[i] += A[i - 1];
  }
  ll ans = A[min(s - 1, (int)n - 1)];
  if (n > s) {
    ll l = n - s;
    int k = f - s;
    ll c = l / k;
    ll d = l % k;
    ans += c * A[f - 1] + (d > 0 ? A[s + d - 1] : 0);
  }
  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#define pb push_back
using namespace std;
const int P = 139;
const int mod = 998244353;
const int maxn = 1e8 + 10;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int minn = 0xc0c0c0c0;
ll k, m, n, x, ans, f[maxn], pre[maxn];
int main() {
  scanf("%lld%lld%lld", &n, &x, &m);
  for (int i = 0; i < m; i++)
    f[i] = -1;
  int a = 0;
  ans = 0;
  while (n--) {
    f[x] = a++;
    pre[x] = ans;
    ans += x;
    x = x * x % m;
    if (f[x] >= 0) {
      int b = a - f[x];
      ans += (ans - pre[x]) * (n / b);
      n %= b;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#define pb push_back
using namespace std;
const int P = 139;
const int mod = 998244353;
const int maxn = 1e8 + 5;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int minn = 0xc0c0c0c0;
int k, m, n, x, f[maxn], pre[maxn];
int main() {
  scanf("%d%d%d", &n, &x, &m);
  for (int i = 0; i < m; i++)
    f[i] = -1;
  int a = 0;
  ll ans = 0;
  while (n--) {
    f[x] = a++;
    pre[x] = ans;
    ans += x;
    x = x * x % m;
    if (f[x] >= 0) {
      int b = a = f[x];
      ans += (ans - pre[x]) * (n / b);
      n %= b;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 998244353
#define MAX 100005
using namespace std;
int v[MAX + 1];
long long rsq[MAX + 1];
int mp[MAX + 1];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  int x, m;
  cin >> n >> x >> m;
  if (n == 1) {
    cout << x << endl;
    return 0;
  }
  v[1] = x;
  mp[x] = 1;
  long long sum = v[1];
  int a = 0, b = 0;
  for (int i = 2;; ++i) {
    int y = (v[i - 1] * 1LL * v[i - 1]) % m;
    sum += y * 1LL;
    if (i == n) {
      cout << sum << endl;
      return 0;
    }
    if (mp[y] == 0)
      v[i] = y, mp[y] = i;
    else {
      a = mp[y];
      b = i - 1;
      break;
    }
  }
  long long ans = 0;
  for (int i = 1; i < a; ++i)
    ans += 1LL * v[i], --n;
  int total = b - a + 1;
  for (int i = a, j = 1; i <= b; ++i, ++j)
    rsq[j] = rsq[j - 1] + 1LL * v[i];
  ans += (n / total) * rsq[total];
  n %= total;
  ans += rsq[n];
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 998244353
#define MAX 100005
using namespace std;
int v[MAX + 1];
long long rsq[MAX + 1];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int mp[MAX + 1];
  long long n;
  int x, m;
  for (int i = 0; i < m; ++i)
    mp[i] = 0;
  cin >> n >> x >> m;
  if (1 == n) {
    cout << x << endl;
    return 0;
  }
  v[1] = x;
  mp[x] = 1;
  long long sum = v[1];
  int a = 0, b = 0;
  for (int i = 2;; ++i) {
    int y = (v[i - 1] * 1LL * v[i - 1]) % (m * 1LL);
    sum += y * 1LL;
    if (i == n) {
      cout << sum << endl;
      return 0;
    }
    if (mp[y] == 0)
      v[i] = y, mp[y] = i;
    else {
      a = mp[y];
      b = i - 1;
      break;
    }
  }
  long long ans = 0;
  for (int i = 1; i < a; ++i)
    ans += v[i] * 1LL, --n;
  int total = b - a + 1;
  for (int i = a, j = 1; i <= b; ++i, ++j)
    rsq[j] = rsq[j - 1] + 1LL * v[i];
  ans += (n / total) * rsq[total];
  n %= total;
  ans += rsq[n];
  cout << ans << endl;
  return 0;
}