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

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ENDL '\\n'
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << ": " << a << ENDL;
#define all(a) begin(a), end(a)
#define fore(i, a, b) for (int i(a), ThkMk(b); i < ThkMk; ++i)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> ii;
typedef vector<lli> vi;

lli MOD = 998244353;

int main() {
  _ lli n, x, m;
  cin >> n >> x >> m;
  x %= m;
  vector<lli> occ(m + 10, -1);
  occ[x] = 0;
  vector<lli> acum(m + 10, 0);
  acum[0] = x;
  lli last = x;
  lli l, r;
  fore(i, 1, m + 10) {
    lli nxt = (last * last) % m;
    if (occ[nxt] != -1) {
      l = occ[nxt];
      r = i;
      break;
    }
    occ[nxt] = i;
    acum[i] += acum[i - 1] + nxt;
    last = nxt;
  }
  if (n <= r) {
    cout << acum[n - 1] << ENDL;
    return 0;
  }
  /// deb(l);
  /// deb(r);
  lli rest = n - l;
  /// deb(rest);
  lli length = r - l;
  /// deb(length);
  lli rep = (rest - rest % length) / length;
  /// deb(rep);
  lli left = rest % length;
  /// deb(left);
  lli cycleVal = acum[r - 1];
  if (l > 0)
    cycleVal -= acum[l - 1];
  /// deb(cycleVal);
  lli ans = cycleVal * rep;
  if (left + l > 0)
    ans += acum[left + l - 1];
  cout << ans << ENDL;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ENDL '\\n'
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << ": " << a << ENDL;
#define all(a) begin(a), end(a)
#define fore(i, a, b) for (int i(a), ThkMk(b); i < ThkMk; ++i)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> ii;
typedef vector<lli> vi;

lli MOD = 998244353;

int main() {
  _ lli n, x, m;
  cin >> n >> x >> m;
  x %= m;
  vector<lli> occ(m, -1);
  occ[x] = 0;
  vector<lli> acum(m, 0);
  acum[0] = x;
  lli last = x;
  lli l, r;
  fore(i, 1, m) {
    lli nxt = (last * last) % m;
    if (occ[nxt] != -1) {
      l = occ[nxt];
      r = i;
      break;
    }
    occ[nxt] = i;
    acum[i] += acum[i - 1] + nxt;
    last = nxt;
  }
  if (n <= r) {
    cout << acum[n - 1] << ENDL;
    return 0;
  }
  /// deb(l);
  /// deb(r);
  lli rest = n - l;
  /// deb(rest);
  lli length = r - l;
  /// deb(length);
  lli rep = (rest - rest % length) / length;
  /// deb(rep);
  lli left = rest % length;
  /// deb(left);
  lli cycleVal = acum[r - 1] - (l > 0 ? acum[l - 1] : 0);
  /// deb(cycleVal);
  lli ans = (left + l > 0 ? acum[left + l - 1] : 0) + cycleVal * rep;
  cout << ans << ENDL;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, x, m;
  cin >> n >> x >> m;
  if (m == 0 || x == 0)
    return cout << 0, 0;
  ll ans = 0;
  ll a = x % m;
  vector<ll> v;
  set<ll> s;
  while (n > 0) {
    if (s.find(a) != s.end())
      break;
    ans += a;
    v.push_back(a);
    s.insert(a);
    a = (a * a) % m;
    n--;
  }
  if ((int)v.size() > 0 && n > 0) {
    reverse(v.begin(), v.end());
    while (v.back() != a && (int)v.size())
      v.pop_back();
    reverse(v.begin(), v.end());
    ll fp = n / ((int)v.size());
    n -= fp * ((int)v.size());
    ans += fp * accumulate(v.begin(), v.end(), 0LL);
    for (int i = 0; i < n; i++)
      ans += v[i];
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, x, m;
  cin >> n >> x >> m;
  if (m == 0 || x == 0)
    return cout << 0, 0;
  ll ans = 0;
  ll a = x % m;
  vector<ll> v;
  set<ll> s;
  while (n > 0) {
    if (s.find(a) != s.end())
      break;
    ans += a;
    v.push_back(a);
    s.insert(a);
    a = (a * a) % m;
    n--;
  }
  if ((int)v.size() > 0) {
    reverse(v.begin(), v.end());
    while (v.back() != a && (int)v.size())
      v.pop_back();
    reverse(v.begin(), v.end());
    ll fp = n / ((int)v.size());
    n -= fp * ((int)v.size());
    ans += fp * accumulate(v.begin(), v.end(), 0LL);
    for (int i = 0; i < n; i++)
      ans += v[i];
  }
  cout << ans;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll check[110000];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, m * 2) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll c = n - n % s;
    ll b = c / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll check[110000];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, n) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll b = n / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll check[110000];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, m * 2) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll check[110000];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, n) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  ll ans = 0LL;
  bool swi = true;
  vll vv;
  rep(i, v.size()) {
    if (v[i] == a)
      swi = false;
    if (swi) {
      ans += v[i];
      n--;
    } else
      vv.push_back(v[i]);
  }
  ll s = vv.size();
  ll sum = 0LL;
  rep(i, s) sum += vv[i];
  ans += sum * (n / s);
  rep(i, n % s) ans += vv[i];
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll check[110000];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, m * 2) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  ll ans = 0LL;
  bool swi = true;
  vll vv;
  rep(i, v.size()) {
    if (v[i] == a)
      swi = false;
    if (swi) {
      ans += v[i];
      n--;
    } else
      vv.push_back(v[i]);
  }
  ll s = vv.size();
  ll sum = 0LL;
  rep(i, s) sum += vv[i];
  ans += sum * (n / s);
  rep(i, n % s) ans += vv[i];
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll check[110000];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, n) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 1001001001;

ll pow_mod(ll x, ll y) {
  ll res = 1;
  for (ll i = 0; i < y; i++)
    res = res * x % mod;
  return res;
}

ll gcd(ll x, ll y) {
  if (y == 0) {
    return x;
  }
  return gcd(y, x % y);
}

int main() {
  ll n;
  int x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1);
  vector<int> a;
  int len = 0;
  ll total = 0;
  while (id[x] == -1) {
    a.push_back(x);
    len++;
    id[x] = len;
    total += x;
    x = (ll)x * x % m;
  }
  ll ans = 0;
  if (n < len) {
    for (int i = 0; i < n; i++)
      ans += a[i];
  } else {
    ans += total;
    n -= len;
    int c_len = len - id[x] + 1;
    ll c_total = 0;
    for (int i = id[x]; i <= len; i++)
      c_total += a[i - 1];
    ans += (n / c_len) * c_total;
    n %= c_len;
    for (int i = 0; i < n; i++)
      ans += a[id[x] + i - 1];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 1001001001;

ll pow_mod(ll x, ll y) {
  ll res = 1;
  for (ll i = 0; i < y; i++)
    res = res * x % mod;
  return res;
}

ll gcd(ll x, ll y) {
  if (y == 0) {
    return x;
  }
  return gcd(y, x % y);
}

int main() {
  ll n;
  int x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1);
  vector<int> a(m);
  int len = 0;
  ll total = 0;
  while (id[x] == -1) {
    a.push_back(x);
    len++;
    id[x] = len;
    total += x;
    x = x * x % m;
  }
  ll ans = 0;
  if (n < len) {
    for (int i = 0; i < n; i++) {
      ans += a[i];
    }
  } else {
    ans += total;
    n -= len;
    int c_len = len - id[x] + 1;
    ll c_total = 0;
    for (int i = id[x]; i <= len; i++) {
      c_total += a[i - 1];
    }
    n %= c_len;
    for (int i = 0; i < n; i++) {
      ans += a[id[x] + i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  ll n, x, m, logn = 1, ans = 0;
  cin >> n >> x >> m;
  while ((1ll << logn) < n)
    logn++;
  vector<vector<ll>> nex(logn, vector<ll>(m, -1)), sum(logn, vector<ll>(m, 0));

  for (ll r = 0; r < m; ++r) {
    nex[0][r] = r * r % m;
    sum[0][r] = r;
  }

  for (int p = 0; p < logn - 1; ++p)
    for (int r = 0; r < m; ++r) {
      nex[p + 1][r] = nex[p][nex[p][r]];
      sum[p + 1][r] = sum[p][r] + sum[p][nex[p][r]];
    }

  for (int p = 0; p < logn; ++p)
    if (n & (1LL << p))
      ans += sum[p][x], x = nex[p][x];

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  ll n, x, m, logn, ans = 0;
  cin >> n >> x >> m;
  logn = ceil(log2(n));
  vector<vector<ll>> next(logn, vector<ll>(m)), sum(logn, vector<ll>(m));

  rep(i, m) {
    next[0][i] = i * i % m;
    sum[0][i] = i;
  }

  rep(i, logn - 1) rep(j, m) {
    next[i + 1][j] = next[i][next[i][j]];
    sum[i + 1][j] = sum[i][next[i][j]] + sum[i][j];
  }

  rep(i, logn) if (n & (1ll << i)) ans += sum[i][x], x = next[i][x];
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  ll n, x, m, logn, ans = 0;
  cin >> n >> x >> m;
  logn = ceil(log2(n)) + 1;
  vector<vector<ll>> nex(logn, vector<ll>(m)), sum(logn, vector<ll>(m));

  rep(i, m) {
    nex[0][i] = i * i % m;
    sum[0][i] = i;
  }

  rep(i, logn - 1) rep(j, m) {
    nex[i + 1][j] = nex[i][nex[i][j]];
    sum[i + 1][j] = sum[i][nex[i][j]] + sum[i][j];
  }

  rep(i, logn) if (n & (1ll << i)) ans += sum[i][x], x = nex[i][x];
  cout << ans << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  ll n, x, m, logn, ans = 0;
  cin >> n >> x >> m;
  while ((1ll << logn) < n)
    logn++;
  vector<vector<ll>> next(logn, vector<ll>(m)), sum(logn, vector<ll>(m));

  rep(i, m) {
    next[0][i] = i * i % m;
    sum[0][i] = i;
  }

  rep(i, logn - 1) rep(j, m) {
    next[i + 1][j] = next[i][next[i][j]];
    sum[i + 1][j] = sum[i][next[i][j]] + sum[i][j];
  }

  rep(i, logn) if (n & (1LL << i)) ans += sum[i][x], x = next[i][x];
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, X, M;
  cin >> N >> X >> M;
  vector<i64> A(M + 2), sum(M + 2), ind(M + 1, -1);
  A[1] = X;
  sum[1] = X;
  ind[X] = 1;
  i64 loopStart, loopSize;
  for (i64 i = 2; i <= M + 1; i++) {
    A[i] = (A[i - 1] * A[i - 1]) % M;
    sum[i] = sum[i - 1] + A[i];
    if (i == N) {
      cout << sum[i] << endl;
      return 0;
    }
    if (ind[A[i]] != -1) {
      loopStart = ind[A[i]];
      loopSize = i - ind[A[i]];
      break;
    }
    ind[A[i]] = i;
  }
  i64 ans = 0;
  i64 loopCnt = (N - loopStart) / loopSize;
  ans += loopCnt * (sum[loopSize + loopStart] - sum[loopStart]);
  N -= loopSize * loopCnt;
  ans += sum[N];
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, X, M;
  cin >> N >> X >> M;
  vector<i64> A(M + 1), sum(M + 1), ind(M + 1, -1);
  A[1] = X;
  sum[1] = X;
  ind[X] = 1;
  i64 loopStart, loopSize;
  for (i64 i = 2; i <= M; i++) {
    A[i] = (A[i - 1] * A[i - 1]) % M;
    sum[i] = sum[i - 1] + A[i];
    if (i == N) {
      cout << sum[i] << endl;
      return 0;
    }
    if (ind[A[i]] != -1) {
      loopStart = ind[A[i]];
      loopSize = i - ind[A[i]];
      break;
    }
    ind[A[i]] = i;
  }
  i64 ans = 0;
  i64 loopCnt = (N - loopStart) / loopSize;
  ans += loopCnt * (sum[loopSize + loopStart] - sum[loopStart]);
  N -= loopSize * loopCnt;
  ans += sum[N];
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (long long i = 0; i < (long long)(n); i++)
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> check(m, -1);
  vector<int> a;
  int t = 0;
  ll total = 0;
  while (check[x] == -1) {
    a.push_back(x);
    check[x] = t;
    t++;
    total += x;
    x = (x * x) % m;
  }
  int c = t - check[x];
  ll s = 0;
  for (int i = check[x]; i < t; ++i) {
    s += a[i];
  }
  ll ans = 0;
  if (n <= t) {
    rep(i, n) { ans += a[i]; }
  } else {
    ans += total;
    n -= t;
    ans += s * (n / c);
    n %= c;
    rep(i, n) { ans += a[check[x] + i]; }
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (long long i = 0; i < (long long)(n); i++)
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
int main() {
  int n, x, m;
  cin >> n >> x >> m;
  vector<int> a(m, -1);
  ll c = 0;
  int d = 0;
  while (a[x] == 1) {
    a.push_back(x);
    a[x] = d;
    d++;
    c += x;
    x = (x * x) % m;
  }
  int e = d - a[x];
  ll f = 0;
  for (int i = a[x]; i < d; i++) {
    f += a[i];
  }
  ll ans = 0;
  if (n <= d) {
    rep(i, n) { ans += a[i]; }
  } else {
    ans += c;
    n -= d;
    ans += f * (n / e);
    n %= e;
    rep(i, n) { ans += a[a[x] + i]; }
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h> //AtCoder
#include <iostream>      //Codeforces
#include <vector>        //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;
/****************************** START ******************************/

signed main() {
  int N, X, M;
  cin >> N >> X >> M;
  map<int, int> A, index;

  A[1] = X;
  index[X] = 1;
  int start, end, last;
  int cnt = 1;
  int sum = 0;
  bool loop = false;
  repeq(i, 1, N - 1) {
    A[i + 1] = A[i] * A[i] % M;
    if (index[A[i + 1]] > 0) {
      start = index[A[i + 1]];
      end = i;
      loop = true;
      break;
    } else {
      index[A[i + 1]] = i + 1;
    }
  }

  int ans = 0;
  if (loop) {
    repeq(i, 1, start - 1) { ans += A[i]; }

    repeq(i, start, end) { sum += A[i]; }
    cnt = (N - (start - 1)) / (end - (start - 1));
    ans += sum * cnt;

    last = start + (end - (start - 1)) * cnt;
    repeq(i, start, N - (last - start)) { ans += A[i]; }
  } else {
    repeq(i, 1, N) { ans += A[i]; }
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //AtCoder
#include <iostream>      //Codeforces
#include <vector>        //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;
/****************************** START ******************************/

signed main() {
  int N, X, M;
  cin >> N >> X >> M;
  map<int, int> A;

  A[1] = X;
  int start, end, last;
  int cnt = 1;
  int sum = 0;
  bool loop = false;
  repeq(i, 2, N) {
    repeq(j, 1, i - 1) {
      if (A[j] == A[i]) {
        start = j;
        end = i - 1;
        loop = true;
        break;
      } else {
        if (j == i - 1) {
          A[i] = A[i - 1] * A[i - 1] % M;
        }
      }
    }
    if (loop) {
      break;
    }
  }
  int ans = 0;
  if (loop) {
    repeq(i, 1, start - 1) { ans += A[i]; }

    repeq(i, start, end) { sum += A[i]; }
    cnt = (N - (start - 1)) / (end - (start - 1));
    ans += sum * cnt;

    last = start + (end - (start - 1)) * cnt;
    repeq(i, last, N) { ans += A[i - (last - start)]; }
  } else {
    repeq(i, 1, N) { ans += A[i]; }
  }
  cout << ans;
}