prithivMLmods/Coder-Stat huggingface dataset:
Problem p02550 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

ll n, x, m;

int t[100009];

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

  cin >> n >> x >> m;

  ll ans = 0;
  int time = 0;
  while (!t[x]) {
    t[x] = ++time;
    ans += x;
    x = (x * x) % m;
    n--;
  }

  int cy = 0;
  ll cost = 0, y = x;
  do {
    cost += y;
    y = (y * y) % m;
    cy++;
  } while (y != x);

  ans += (n / cy) * cost;
  n %= cy;

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

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

ll n, x, m;

int t[100009];

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

  cin >> n >> x >> m;

  ll ans = 0;
  int time = 0;
  while (!t[x]) {
    t[x] = ++time;
    ans += x;
    x = (x * x) % m;
    n--;
    if (n == 0) {
      cout << ans << endl;
      return 0;
    }
  }

  int cy = 0;
  ll cost = 0, y = x;
  do {
    cost += y;
    y = (y * y) % m;
    cy++;
  } while (y != x);

  ans += (n / cy) * cost;
  n %= cy;

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

  cout << ans << endl;

  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll f(ll n, ll x, ll m) {
  ll ref = x;
  ll now = x * x % m;
  int cnt = 1;
  vector<ll> d(m + 1);
  d[0] = 0;
  while (now != x) {
    d[cnt] = ref;
    ref += now;
    now = now * now % m;
    ++cnt;
  }
  ref = n / cnt * ref + d[n % cnt];
  return ref;
}

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> ok(n, true);
  ll cnt = 0;
  ll ans = 0;
  while (cnt < n) {
    if (!ok[x])
      break;
    ans += x;
    ok[x] = false;
    x = x * x % m;
    ++cnt;
  }
  ans += f(n - cnt, x, m);
  cout << ans << endl;
}

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

ll f(ll n, ll x, ll m) {
  ll ref = x;
  ll now = x * x % m;
  int cnt = 1;
  vector<ll> d(m + 1);
  d[0] = 0;
  while (now != x) {
    d[cnt] = ref;
    ref += now;
    now = now * now % m;
    ++cnt;
  }
  ref = n / cnt * ref + d[n % cnt];
  return ref;
}

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> ok(m, true);
  ll cnt = 0;
  ll ans = 0;
  while (cnt < n) {
    if (!ok[x])
      break;
    ans += x;
    ok[x] = false;
    x = x * x % m;
    ++cnt;
  }
  ans += f(n - cnt, x, m);
  cout << ans << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  VL A(M, -1), Y(min(M, N));
  ll tmp = X;
  ll P = min(N, M) - 1;
  rep(i, min(N, M)) {
    if (A[tmp] >= 0) {
      P = i;
      break;
    } else {
      Y[i] = tmp;
      A[tmp] = i;
      tmp = tmp * tmp % M;
    }
  }
  ll ans = 0;
  rep(i, P + 1) ans += Y[i];
  ll ans2 = 0;
  if (P + 1 != N) {
    rep(i, P - A[tmp]) ans2 += Y[A[tmp] + i];
    ans2 = ans2 * ((N - P) / (P - A[tmp]));
    ans += ans2;
    rep(i, (N - P) % (P - A[tmp])) ans += Y[A[tmp] + i];
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  VL A(M, -1), Y(min(M, N));
  ll tmp = X;
  ll P = min(N, M) - 1;
  if (X == 0) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, min(N, M)) {
    if (A[tmp] >= 0) {
      P = i;
      break;
    } else {
      Y[i] = tmp;
      A[tmp] = i;
      tmp = tmp * tmp % M;
    }
  }
  ll ans = 0;
  rep(i, P + 1) ans += Y[i];
  ll ans2 = 0;
  if (P + 1 != N) {
    rep(i, P - A[tmp]) ans2 += Y[A[tmp] + i];
    ans2 = ans2 * ((N - P) / (P - A[tmp]));
    ans += ans2;
    rep(i, (N - P) % (P - A[tmp])) ans += Y[A[tmp] + i];
  }
  cout << ans << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

  long long sum1 = 0;
  long long sum2 = 0;
  vector<long long> L(M);
  vector<long long> count(M, 0);
  L.at(0) = X;
  count.at(X)++;
  long long A = -1;
  long long B = -1;
  long long Y = -1;

  if (N == 1) {
    cout << X << endl;
    return 0;
  }

  for (int i = 1; i < M; i++) {
    L.at(i) = (L.at(i - 1) * L.at(i - 1)) % M;
    count.at(L.at(i))++;
    if (count.at(L.at(i)) > 1) {
      A = i + 1;
      Y = L.at(i);
      break;
    }
  }

  long long ans = 0;
  if (A == -1) {
    for (int i = 0; i < N; i++) {
      ans += L.at(i);
    }
    cout << ans << endl;
    return 0;
  }

  long long judge = 0;
  for (int i = 0; i < A; i++) {
    if (L.at(i) == Y) {
      judge++;
      if (judge == 1) {
        B = i + 1;
      }
    }
    if (judge == 0) {
      sum1 += L.at(i);
    } else if (judge == 1) {
      sum2 += L.at(i);
    }
  }

  if (N < A) {
    for (int i = 0; i < N; i++) {
      ans += L.at(i);
    }
  } else {
    long long C = (N - B + 1) / (A - B);
    long long D = (N - B + 1) % (A - B);
    for (int i = B - 1; i < B + D - 1; i++) {
      ans += L.at(i);
    }
    ans += sum1 + C * sum2;
  }
  cout << ans << endl;
}

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

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

  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }

  long long sum1 = 0;
  long long sum2 = 0;
  vector<long long> L(M);
  vector<long long> count(M, 0);
  L.at(0) = X;
  count.at(X)++;
  long long A = -1;
  long long B = -1;
  long long Y = -1;

  if (N == 1) {
    cout << X << endl;
    return 0;
  }

  for (int i = 1; i < M; i++) {
    L.at(i) = (L.at(i - 1) * L.at(i - 1)) % M;
    count.at(L.at(i))++;
    if (count.at(L.at(i)) > 1) {
      A = i + 1;
      Y = L.at(i);
      break;
    }
  }

  long long ans = 0;
  if (A == -1) {
    for (int i = 0; i < N; i++) {
      ans += L.at(i);
    }
    cout << ans << endl;
    return 0;
  }

  long long judge = 0;
  for (int i = 0; i < A; i++) {
    if (L.at(i) == Y) {
      judge++;
      if (judge == 1) {
        B = i + 1;
      }
    }
    if (judge == 0) {
      sum1 += L.at(i);
    } else if (judge == 1) {
      sum2 += L.at(i);
    }
  }

  if (N < A) {
    for (int i = 0; i < N; i++) {
      ans += L.at(i);
    }
  } else {
    long long C = (N - B + 1) / (A - B);
    long long D = (N - B + 1) % (A - B);
    for (int i = B - 1; i < B + D - 1; i++) {
      ans += L.at(i);
    }
    ans += sum1 + C * sum2;
  }
  cout << ans << endl;
}
Problem p02550 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \
  sort(all((x)));                                                              \
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, x, m;

  cin >> n >> x >> m;

  ll ans = 0;

  ll cycle = 0, tot = 0, left = n;

  vector<char> u(m + 1);

  while (true) {
    ans += x;
    left--;
    if (left == 0)
      break;
    if (u[x])
      break;
    u[x] = true;
    x = (x * x) % m;
  }

  ll stop = x;

  do {
    tot += x;
    cycle++;
    x = (x * x) % m;
  } while (x != stop);

  ans += tot * (left / cycle);

  left %= cycle;

  while (left--) {
    x = (x * x) % m;
    ans += x;
  }

  cout << ans;

  return 0;
}
/*

*/


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \
  sort(all((x)));                                                              \
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, x, m;

  cin >> n >> x >> m;

  ll ans = 0;

  ll cycle = 0, tot = 0, left = n;

  vector<char> u(m + 1);

  while (true) {
    ans += x;
    left--;
    if (left == 0) {
      cout << ans;
      return 0;
    }
    if (u[x])
      break;
    u[x] = true;
    x = (x * x) % m;
  }

  ll stop = x;

  do {
    tot += x;
    cycle++;
    x = (x * x) % m;
  } while (x != stop);

  ans += tot * (left / cycle);

  left %= cycle;

  while (left--) {
    x = (x * x) % m;
    ans += x;
  }

  cout << ans;

  return 0;
}
/*

*/
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

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

  vector<int> cir;
  int cur = x;
  vector<bool> vis(m);
  int start = -1;
  long long ans = 0;
  long long sum = 0;
  while (n > 0) {
    if (cur == 0) {
      cout << ans << '\n';
      exit(0);
    }
    if (vis[cur]) {
      start = cur;
      break;
    }
    n -= 1;
    ans += cur;
    vis[cur] = true;
    cur = 1LL * cur * cur % m;
  }

  vis.assign(n, false);
  while (!vis[start]) {
    cir.emplace_back(start);
    vis[start] = true;
    sum += start;
    start = 1LL * start * start % m;
  }

  int s = cir.size();
  ans += n / s * sum;
  for (int i = 0; i < n % s; ++i) {
    ans += cir[i];
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

  vector<int> cir;
  int cur = x;
  vector<bool> vis(m);
  int start = -1;
  long long ans = 0;
  long long sum = 0;
  while (n > 0) {
    if (cur == 0) {
      cout << ans << '\n';
      exit(0);
    }
    if (vis[cur]) {
      start = cur;
      break;
    }
    n -= 1;
    ans += cur;
    vis[cur] = true;
    cur = 1LL * cur * cur % m;
  }

  if (n == 0) {
    cout << ans << '\n';
    exit(0);
  }

  for (int i = 0; i < m; ++i) {
    vis[i] = false;
  }
  while (!vis[start]) {
    cir.emplace_back(start);
    vis[start] = true;
    sum += start;
    start = 1LL * start * start % m;
  }

  int s = cir.size();
  ans += n / s * sum;
  for (int i = 0; i < n % s; ++i) {
    ans += cir[i];
  }

  cout << ans << '\n';

  return 0;
}
Problem p02550 (C++)

Buggy submission (status: 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) {
    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;
}

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 && 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;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll N, X, M, c = 0, lp, st, lpp;
  cin >> N >> X >> M;
  map<ll, ll> mp;
  mp[X] = c;
  vector<ll> A(M + 100), S(M + 100);
  A[0] = X;
  S[0] = X;
  while (1) {
    c++;
    X = (X * X) % M;
    if (mp.count(X)) {
      lp = c - mp.at(X);
      st = mp.at(X);
      lpp = S.at(c - 1);
      if (st > 0)
        lpp -= S.at(st - 1);
      break;
    }
    mp[X] = c;
    A[c] = X;
    S[c] = S[c - 1] + A[c];
    // cout << A[c] << "--";
  }
  ll ans = 0, lpc = (N - st) / lp;
  ans += lpc * lpp;
  // cout << ";;" << lpp << endl;
  if (st > 0)
    ans += S.at(st - 1);
  ll ret = (N - st) % lp;
  ans += S.at(ret + st - 1);
  if (st > 0)
    ans -= S.at(st - 1);
  cout << ans;
  return 0;
}

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

typedef long long ll;

int main() {
  ll N, X, M, c = 0, lp, st, lpp;
  cin >> N >> X >> M;
  map<ll, ll> mp;
  mp[X] = c;
  vector<ll> A(M + 100), S(M + 100);
  A[0] = X;
  S[0] = X;
  while (1) {
    c++;
    X = (X * X) % M;
    if (mp.count(X)) {
      lp = c - mp.at(X);
      st = mp.at(X);
      lpp = S.at(c - 1);
      if (st > 0)
        lpp -= S.at(st - 1);
      break;
    }
    mp[X] = c;
    A[c] = X;
    S[c] = S[c - 1] + A[c];
    // cout << A[c] << "--";
  }
  ll ans = 0, lpc = (N - st) / lp;
  ans += lpc * lpp;
  // cout << ";;" << lpp << endl;
  if (st > 0)
    ans += S.at(st - 1);
  ll ret = (N - st) % lp;
  if (ret + st > 0)
    ans += S.at(ret + st - 1);
  if (st > 0)
    ans -= S.at(st - 1);
  cout << ans;
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int main(void) {
  ll i, j, num, x, m, ans, a = 0, b = 0, k = 0;
  cin >> num >> x >> m;
  vector<ll> d(m, 0), s(m, 0);
  rep(i, num) {
    k++;
    a += x;
    if (d[x]) {
      k -= s[x];
      b = a - d[x];
      a = d[x];
      int c = (num - s[x]) % k;
      k = (num - s[x]) / k;
      rep(j, c) {
        x *= x;
        x %= m;
        a += x;
      }
      break;
    }
    d[x] = a;
    s[x] = k;
    x *= x;
    x %= m;
  }
  if (i == num)
    ans = a;
  else
    ans = a + b * k;
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int main(void) {
  ll i, j, num, x, m, ans, a = 0, b = 0, k = 0;
  cin >> num >> x >> m;
  if (m == 1) {
    printf("0\n");
    return 0;
  }
  vector<ll> d(m, 0), s(m, 0);
  rep(i, num) {
    k++;
    a += x;
    if (d[x]) {
      k -= s[x];
      b = a - d[x];
      a = d[x];
      int c = (num - s[x]) % k;
      k = (num - s[x]) / k;
      rep(j, c) {
        x *= x;
        x %= m;
        a += x;
      }
      break;
    }
    d[x] = a;
    s[x] = k;
    x *= x;
    x %= m;
  }
  if (i == num)
    ans = a;
  else
    ans = a + b * k;
  cout << ans << "\n";
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

ll f(ll n, ll m) { return (n * n) % m; }

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  ll A, preA, ans = 0;
  ll loopcnt = 0, cnt = 0, sum_loop = 0, rec_A;
  vector<bool> visit(100001, false);
  bool dub;
  for (int i = 1; i <= N; i++) {
    if (i == 1) {
      A = X;
      ans += A;
      visit[A] = true;
      cnt++;
      continue;
    }
    preA = A;
    A = f(A, M);
    if (dub) {
      if (A == rec_A)
        break;
      ans += A;
      sum_loop += A;
      loopcnt++;
      cnt++;
    } else {
      if (visit[A]) {
        dub = true;
        rec_A = A;
        sum_loop += A;
        loopcnt++;
      }
      ans += A;
      cnt++;
      visit[A] = true;
    }
  }
  A = preA;
  if (dub) {
    ans += sum_loop * ((N - cnt) / loopcnt);
    ll rem = (N - cnt) % loopcnt;
    for (int i = 0; i < rem; i++) {
      A = f(A, M);
      ans += A;
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

ll f(ll n, ll m) { return (n * n) % m; }

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  ll A, preA, ans = 0;
  ll loopcnt = 0, cnt = 0, sum_loop = 0, rec_A;
  vector<bool> visit(1000010, false);
  bool dub = false;
  for (int i = 1; i <= N; i++) {
    if (i == 1) {
      A = X;
      ans += A;
      visit[A] = true;
      cnt++;
      continue;
    }
    preA = A;
    A = f(A, M);
    if (dub) {
      if (A == rec_A)
        break;
      ans += A;
      sum_loop += A;
      loopcnt++;
      cnt++;
    } else {
      if (visit[A]) {
        dub = true;
        rec_A = A;
        sum_loop += A;
        loopcnt++;
      }
      ans += A;
      cnt++;
      visit[A] = true;
    }
  }
  A = preA;
  if (dub) {
    ans += sum_loop * ((N - cnt) / loopcnt);
    ll rem = (N - cnt) % loopcnt;
    for (int i = 0; i < rem; i++) {
      A = f(A, M);
      ans += A;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: 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) {
    int o = X;
    for (int h = 1; h <= N - 1; h++) {
      o += pow(X, 2 * h);
    }
    cout << o << endl;
  }
}

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) {
    cout << 0 << endl;
  }
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1e9 + 7LL;

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  ll f = X;
  ll ans = 0;
  map<ll, ll> m;
  vector<ll> sum(2 * M + 1);
  sum.at(0) = X;
  m[X] = 0;
  bool flag = false;

  rep(i, N) {
    if (i == 0)
      continue;
    f = f * f % M;
    sum.at(i) = sum.at(i - 1) + f;

    if (m.count(f)) {
      flag = true;
      ll x = i - m[f];
      ll tmp = sum.at(i) - sum.at(m[f]);

      ll a = 0;
      if (m[f] != 0)
        a = sum.at(m[f] - 1);
      ll b = tmp * ((N - m[f]) / x);
      ll c = 0;
      if ((N - m[f]) % x != 0)
        c = sum.at((N - m[f]) % x + m[f] - 1) - sum.at(m[f] - 1);
      ans = a + b + c;
      break;
    } else
      m[f] = i;
  }
  if (!flag)
    ans = sum.at(N - 1);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1e9 + 7LL;

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  ll f = X;
  ll ans = 0;
  map<ll, ll> m;
  vector<ll> sum(2 * M + 1);
  sum.at(0) = X;
  m[X] = 0;
  bool flag = false;

  rep(i, N) {
    if (i == 0)
      continue;
    f = f * f % M;
    sum.at(i) = sum.at(i - 1) + f;

    if (m.count(f)) {
      flag = true;
      ll x = i - m[f];
      ll tmp = sum.at(i) - sum.at(m[f]);

      ll a = 0;
      if (m[f] != 0)
        a = sum.at(m[f] - 1);
      ll b = tmp * ((N - m[f]) / x);
      ll c = 0;
      if ((N - m[f]) % x != 0) {
        c = sum.at((N - m[f]) % x + m[f] - 1);
        if (m[f] != 0)
          c -= sum.at(m[f] - 1);
      }
      ans = a + b + c;
      break;
    } else
      m[f] = i;
  }
  if (!flag)
    ans = sum.at(N - 1);
  cout << ans << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
using ll = long long;
const ll mod = (ll)998244353;

ll powmod(ll a, ll k, ll m) {
  if (k == 0)
    return 1;
  ll t = powmod(a, k / 2, m);
  t = (t * t) % mod;
  if (k % 2 == 1)
    t = (t * a) % mod;
  return t;
}

int vis[1 << 17];
long pre[1 << 17];

int main() {
  ll n;
  ll x, m;
  cin >> n >> x >> m;
  map<ll, int> mp;
  vector<ll> val;
  int idx = 0;
  auto it = mp.end();
  ll idxsum = 0;
  while ((it = mp.find(x)) == mp.end() && n > 0) {
    val.push_back(x);
    mp[x] = idx++;
    idxsum += x;
    x = (x * x) % m;
    n--;
  }
  int loop = idx - it->second;
  // n -= idx;
  ll loopsum = 0;
  for (int i = it->second; i < idx; i++) {
    loopsum += val[i];
  }
  ll mult = n / loop;
  n -= loop * mult;
  ll modsum = 0;
  for (int i = it->second; --n >= 0; i++) {
    modsum += val[i];
  }

  ll ans = idxsum + loopsum * mult + modsum;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
using ll = long long;
const ll mod = (ll)998244353;

ll powmod(ll a, ll k, ll m) {
  if (k == 0)
    return 1;
  ll t = powmod(a, k / 2, m);
  t = (t * t) % mod;
  if (k % 2 == 1)
    t = (t * a) % mod;
  return t;
}

int vis[1 << 17];
long pre[1 << 17];

int main() {
  ll n;
  ll x, m;
  cin >> n >> x >> m;
  map<ll, int> mp;
  vector<ll> val;
  int idx = 0;
  auto it = mp.end();
  ll idxsum = 0;
  while ((it = mp.find(x)) == mp.end() && n > 0) {
    val.push_back(x);
    mp[x] = idx++;
    idxsum += x;
    x = (x * x) % m;
    n--;
  }
  if (n == 0) {
    cout << idxsum << endl;
    return 0;
  }
  int loop = idx - it->second;
  // n -= idx;
  ll loopsum = 0;
  for (int i = it->second; i < idx; i++) {
    loopsum += val[i];
  }
  ll mult = n / loop;
  n -= loop * mult;
  ll modsum = 0;
  for (int i = it->second; --n >= 0; i++) {
    modsum += val[i];
  }

  ll ans = idxsum + loopsum * mult + modsum;
  cout << ans << endl;

  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  ll x;
  cin >> x;
  ll m;
  cin >> m;
  ll c = 1;
  vll d(m + 10);
  ll count = 0;
  ll sum = 0;
  vll s(m + 10);
  while (true) {
    if (count != 0)
      c = c * c % m;
    else
      c = x % m;
    if (d[c] != 0)
      break;
    count++;
    d[c] = count;
    sum = sum + c;
    s[count] = sum;
  }
  ll ans = 0;
  ll sum2 = s[count] - s[d[c] - 1];
  ans = sum + sum2 * ((n - count) / (count - d[c] + 1));
  ans += s[d[c] - 1 + (n - count) % (count - d[c] + 1)] - s[d[c] - 1];
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  ll x;
  cin >> x;
  ll m;
  cin >> m;
  ll c = 1;
  vll d(m + 10);
  ll count = 0;
  ll sum = 0;
  vll s(m + 10);
  while (true) {
    if (count != 0)
      c = c * c % m;
    else
      c = x % m;
    if (d[c] != 0)
      break;
    count++;
    d[c] = count;
    sum = sum + c;
    s[count] = sum;
    if (count == n) {
      cout << sum << endl;
      return 0;
    }
  }
  ll ans = 0;
  ll sum2 = s[count] - s[d[c] - 1];
  ans = sum + sum2 * ((n - count) / (count - d[c] + 1));
  ans += s[d[c] - 1 + (n - count) % (count - d[c] + 1)] - s[d[c] - 1];
  cout << ans << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; (s) <= i; i--)

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  rep(i, 0, n) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      rep(j, 0, one_cycle.size()) if (one_cycle[j] == now) {
        start = j;
        break;
      }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = one_cycle.size() - start;
  rep(i, 0, start) s1 += one_cycle[i];
  rep(i, start, one_cycle.size()) s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  rep(i, 0, (n - start) % loop) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (n); ++i)

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  rep(i, 0, n) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      rep(j, 0, one_cycle.size()) if (one_cycle[j] == now) {
        start = j;
        break;
      }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = one_cycle.size() - start;
  rep(i, 0, start) s1 += one_cycle[i];
  rep(i, start, one_cycle.size()) s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  rep(i, 0, (n - start) % loop) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define llint long long int

using namespace std;

int main() {
  llint N, X, M;

  cin >> N >> X >> M;

  // ループするまで
  vector<bool> sumi(M, false);

  llint sum = 0;
  llint A;

  bool loop = false;
  llint loop_start = -1;

  for (int i = 1; i <= N; i++) {
    if (i == 1) {
      A = X;
    } else {
      A = (A * A) % M;
    }
    sum += A;
    loop_start = A;

    if (sumi[A] == false) {
      sumi[A] = true;
    } else {
      loop = true;

      break;
    }
  }
  if (loop = false) {
    cout << sum << endl;

    return 0;
  }
  // ループの開始まで
  sum = 0;

  llint i = 1;

  while (true) {
    if (i == 1) {
      A = X;
    } else {
      A = (A * A) % M;
    }
    sum += A;
    i++;

    if (A == loop_start) {
      break;
    }
  }
  // ループの長さ
  llint count = 0;
  llint sum_part = 0;

  while (true) {
    A = (A * A) % M;
    sum_part += A;
    count++;
    i++;

    if (A == loop_start) {
      break;
    }
  }

  i -= count;
  sum += ((N + 1 - i) / count) * sum_part;
  i += ((N + 1 - i) / count) * count;
  // ループの終了から
  for (; i <= N; i++) {
    A = (A * A) % M;
    sum += A;
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define llint long long int

using namespace std;

int main() {
  llint N, X, M;

  cin >> N >> X >> M;

  if (N == 1) {
    cout << X << endl;

    return 0;
  }
  // ループするまで
  vector<bool> sumi(M, false);

  llint sum = 0;
  llint A;

  bool loop = false;
  llint loop_start = -1;

  for (int i = 1; i <= N; i++) {
    if (i == 1) {
      A = X;
    } else {
      A = (A * A) % M;
    }
    sum += A;
    loop_start = A;

    if (sumi[A] == false) {
      sumi[A] = true;
    } else {
      loop = true;

      break;
    }
  }
  if (loop = false) {
    cout << sum << endl;

    return 0;
  }
  // ループの開始まで
  sum = 0;

  llint i = 1;

  while (true) {
    if (i == 1) {
      A = X;
    } else {
      A = (A * A) % M;
    }
    sum += A;
    i++;

    if (A == loop_start) {
      break;
    }
  }
  // ループの長さ
  llint count = 0;
  llint sum_part = 0;

  while (true) {
    A = (A * A) % M;
    sum_part += A;
    count++;
    i++;

    if (A == loop_start) {
      break;
    }
  }

  i -= count;
  sum += ((N + 1 - i) / count) * sum_part;
  i += ((N + 1 - i) / count) * count;
  // ループの終了から
  for (; i <= N; i++) {
    A = (A * A) % M;
    sum += A;
  }
  cout << sum << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; mt19937_64
// rng(std::chrono::system_clock::now().time_since_epoch().count());

// /mnt/c/Users/vince/Desktop/POST-IOI/

long n, x, m;
vector<long> vec;
int visited[100003];

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);

  // scanf("%lld %lld %lld", &n, &x, &m);
  long a = x;
  while (1) {
    visited[a] = vec.size();
    vec.push_back(a);
    a = (a * a) % m;
    if (visited[a]) {
      // printf("%lld\n", a);
      break;
    }
  }
  int sz1 = 0, sz2 = vec.size();
  long sum = 0;
  for (int i = visited[a]; i < vec.size(); i++) {
    sz2--;
    sz1++;
    sum += vec[i];
  }
  // printf("%lld\n", sum);
  // for(auto x : vec) printf("%lld ", x); printf("\n");

  long res = 0;
  if (n <= sz2) {
    for (int i = 0; i < n; i++)
      res += vec[i];
  } else {
    for (int i = 0; i < sz2; i++)
      res += vec[i];
    n -= sz2;

    res += sum * (n / sz1);
    n %= sz1;
    for (int i = 0; i < n; i++)
      res += vec[visited[a] + i];
  }
  printf("%lld\n", res);
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; mt19937_64
// rng(std::chrono::system_clock::now().time_since_epoch().count());

// /mnt/c/Users/vince/Desktop/POST-IOI/

long n, x, m;
vector<long> vec;
int visited[100003];

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);

  scanf("%lld %lld %lld", &n, &x, &m);
  long a = x;
  while (1) {
    visited[a] = vec.size();
    vec.push_back(a);
    a = (a * a) % m;
    if (visited[a]) {
      // printf("%lld\n", a);
      break;
    }
  }
  int sz1 = 0, sz2 = vec.size();
  long sum = 0;
  for (int i = visited[a]; i < vec.size(); i++) {
    sz2--;
    sz1++;
    sum += vec[i];
  }
  // printf("%lld\n", sum);
  // for(auto x : vec) printf("%lld ", x); printf("\n");

  long res = 0;
  if (n <= sz2) {
    for (int i = 0; i < n; i++)
      res += vec[i];
  } else {
    for (int i = 0; i < sz2; i++)
      res += vec[i];
    n -= sz2;

    res += sum * (n / sz1);
    n %= sz1;
    for (int i = 0; i < n; i++)
      res += vec[visited[a] + i];
  }
  printf("%lld\n", res);
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <map>
using namespace std;
long long n, x, m;
long long a[200005];
map<long long, int> ma;
int l, r;
long long sum, asum;
int main() {
  scanf("%lld%lld%lld", &n, &x, &m);
  a[1] = x;
  for (int i = 2;; ++i) {
    a[i] = a[i - 1] * a[i - 1] % m;
    if (ma.count(a[i])) {
      l = ma[a[i]];
      r = i;
      break;
    }
    ma[a[i]] = i;
  }
  if (n < l) {
    for (int i = 1; i <= n; ++i)
      sum += a[i];
    printf("%lld\n", sum);
    return 0;
  }
  for (int i = 1; i < l; ++i) {
    sum += a[i];
  }
  n -= l;
  ++n;
  for (int i = l; i < r; ++i)
    asum += a[i];
  sum += asum * (n / (r - l));
  for (int i = l; i < l + n; ++i)
    sum += a[i];
  printf("%lld\n", sum);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <map>
using namespace std;
long long n, x, m;
long long a[200005];
map<long long, int> ma;
int l, r;
long long sum, asum;
int main() {
  scanf("%lld%lld%lld", &n, &x, &m);
  a[1] = x;
  for (int i = 2;; ++i) {
    a[i] = a[i - 1] * a[i - 1] % m;
    if (ma.count(a[i])) {
      l = ma[a[i]];
      r = i;
      break;
    }
    ma[a[i]] = i;
  }
  if (n < l) {
    for (int i = 1; i <= n; ++i)
      sum += a[i];
    printf("%lld\n", sum);
    return 0;
  }
  for (int i = 1; i < l; ++i) {
    sum += a[i];
  }
  n -= l;
  ++n;
  for (int i = l; i < r; ++i)
    asum += a[i];
  sum += asum * (n / (r - l));
  n %= r - l;
  for (int i = l; i < l + n; ++i)
    sum += a[i];
  printf("%lld\n", sum);
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: 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].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";
}


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].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";
}
Problem p02550 (C++)

Buggy submission (status: 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) {
    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;
}

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) {
    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 (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, mod, x, ans;
int a[1000005];
int a2[1000005];
signed main() {
  cin >> n >> x >> mod;
  int pos = 0, len = 0, pos2 = n, sum = 0;
  for (int i = 1; i <= n; i++) {
    ans += x;
    a[x] = i;
    a2[i] = x;
    x = (x * x) % mod;
    if (a[x]) {
      pos = a[x];
      pos2 -= i;
      len = max(1LL, i - pos + 1);
      for (int k = pos; k <= i; k++)
        sum += a2[k];
      break;
    }
  }
  ans += (pos2 / len) * sum;
  pos2 %= len;
  for (int i = 1; i <= pos2; i++) {
    ans += x;
    x = (x * x) % mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, mod, x, ans;
int a[1000005];
int a2[1000005];
signed main() {
  cin >> n >> x >> mod;
  int pos = 0, len = 0, pos2 = n, sum = 0;
  if (n <= 100000) {
    for (int i = 1; i <= pos2; i++) {
      ans += x;
      x = (x * x) % mod;
    }
    cout << ans;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    ans += x;
    a[x] = i;
    a2[i] = x;
    x = (x * x) % mod;
    if (a[x]) {
      pos = a[x];
      pos2 -= i;
      len = max(1LL, i - pos + 1);
      for (int k = pos; k <= i; k++)
        sum += a2[k];
      break;
    }
  }
  ans += (pos2 / len) * sum;
  pos2 %= len;
  for (int i = 1; i <= pos2; i++) {
    ans += x;
    x = (x * x) % mod;
  }
  cout << ans;
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a, b) memset(a, b, sizeof a)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a, b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6 + 10, M = 2e6 + 10, mod = 998244353, inf = 0x3f3f3f;
ll v[N], sum[N], mm[N];

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

  while (n--) {
    ans += x;
    v[x]++;
    if (v[x] > 1)
      mm[++cnt] = x;
    x = x * x % m;
    if (v[x] > 2 || !x)
      break;
  }
  if (!x) {
    cout << ans << nl;
    AC;
  }
  rep1(i, cnt) sum[i] = sum[i - 1] + mm[i];
  ans += (n / cnt) * sum[cnt] + sum[n % cnt];
  cout << ans << nl;

  AC;
}

Fixed submission:
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a, b) memset(a, b, sizeof a)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a, b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6 + 10, M = 2e6 + 10, mod = 998244353, inf = 0x3f3f3f;
ll v[N], sum[N], mm[N];

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

  while (n--) {
    ans += x;
    v[x]++;
    if (v[x] > 1)
      mm[++cnt] = x;
    x = x * x % m;
    if (v[x] > 2 || !x)
      break;
  }
  if (!x) {
    cout << ans << nl;
    AC;
  }
  rep1(i, cnt) sum[i] = sum[i - 1] + mm[i];
  if (cnt)
    ans += (n / cnt) * sum[cnt] + sum[n % cnt];
  cout << ans << nl;

  AC;
}