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


#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll MOD;
const int sz = 2e5;
void mult(ll &a, ll b) { a = ((a % MOD) * (b % MOD)) % MOD; }
void add(ll &a, ll b) { a = ((a % MOD) + (b % MOD)) % MOD; }
ll n, x, found[sz], dp[sz];

int main() {
  fast;
  cin >> n >> x >> MOD;
  ll crsm = 0;
  foru(i, 0, n) {
    if (found[x]) {
      ll len = (ll)i - found[x], fit = (n - i) / len,
         segSum = dp[i - 1] - (found[x] ? dp[found[x] - 1] : 0);
      crsm += fit * segSum;
      ll rem = (n - i) % len;
      if (rem) {
        if (found[x]) {
          crsm += dp[found[x] + rem - 1] - dp[found[x] - 1];
        } else {
          crsm += dp[found[x] + rem - 1];
        }
      }
      break;
    }
    found[x] = i;
    crsm += x;
    dp[i] = crsm;
    mult(x, x);
  }
  cout << crsm << endl;
  return 0;
}

Error: TLE

Fixed submission:


#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll MOD;
void mult(ll &a, ll b) { a = ((a % MOD) * (b % MOD)) % MOD; }
void add(ll &a, ll b) { a = ((a % MOD) + (b % MOD)) % MOD; }
ll n, x;

int main() {
  fast;
  cin >> n >> x >> MOD;
  ll crsm = 0;
  foru(i, 0, n) {
    if (!x)
      break;
    if (x == 1) {
      crsm += (n - i);
      break;
    }
    crsm += x;
    mult(x, x);
  }
  cout << crsm << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: 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;
  if (ret + st > 0)
    ans += S.at(ret + st - 1);
  if (st > 0)
    ans -= S.at(st - 1);
  cout << ans;
  return 0;
}

Error: WA

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;
  ans += S.at(ret + st - 1);
  if (st > 0)
    ans -= S.at(st - 1);
  cout << ans;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long st[200010];
int vis[200010];

int main() {
  int x, m;
  long long n;
  scanf("%lld%d%d", &n, &x, &m);
  memset(vis, -1, sizeof(vis));
  long long ans = 0;
  int i;
  long long j;
  for (i = 0, j = x; vis[j] == -1; ++i, j = j * j % m)
    vis[j] = i, st[i] = j;
  if (n == vis[j]) {
    for (int k = 0; k < n; ++k)
      ans += st[k];
    printf("%lld\\n", ans);
    return 0;
  }

  for (int k = 0; k < vis[j]; ++k)
    ans += st[k];
  long long sum = 0;
  for (int k = vis[j]; k < i; ++k)
    sum += st[k];
  for (int k = 0; k < (n - vis[j]) % (i - vis[j]); ++k)
    ans += st[k + vis[j]];
  printf("%lld\\n", ans + (n - vis[j]) / (i - vis[j]) * sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <unordered_map>
#define ll long long
using namespace std;
ll n, a, mod;
ll res = 0;

int main() {

  scanf("%lld%lld%lld", &n, &a, &mod);

  ll res = 0;
  for (int i = 1; i <= n; i++) {
    res += a;
    a = a * a % mod;
  }
  cout << res << endl;
}
Problem p02550 (C++) — buggy submission, verdict: 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(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;
}


Error: 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(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;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

// Author : heyuhhh
// Created Time : 2020/09/19 20:48:35
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// head
const int N = 1e5 + 5;
void run() {
  ll n;
  int x, m;
  cin >> n >> x >> m;

  vector<int> f(m + 2);
  vector<ll> sum(m + 2);
  f[1] = sum[1] = x;

  if (n <= m) {
    for (int i = 2; i <= n; i++) {
      f[i] = 1ll * f[i - 1] * f[i - 1] % m;
      sum[i] = sum[i - 1] + f[i];
    }
    ll res = sum[n];
    cout << res << '\\n';
    return;
  }

  ll ans = x, S;
  vector<int> last(m + 2, -1);
  last[x] = 1;
  int len, k;
  for (int i = 2; i <= m + 1; i++) {
    f[i] = 1ll * f[i - 1] * f[i - 1] % m;
    sum[i] = sum[i - 1] + f[i];
    if (last[f[i]] != -1) {
      k = last[f[i]];
      len = i - k;
      S = sum[i] - sum[last[f[i]]];
      n -= i - 1;
      break;
    }
    ans += f[i];
    last[f[i]] = i;
  }
  ans += (n / len) * S + sum[k + n % len - 1] - sum[k - 1];
  cout << ans << '\\n';
}
int main() {
#ifdef Local
  freopen("input.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  run();
  return 0;
}

Error: 0

Fixed submission:

// Author : heyuhhh
// Created Time : 2020/09/19 20:48:35
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// head
const int N = 1e5 + 5;
void run() {
  ll n;
  int x, m;
  cin >> n >> x >> m;

  vector<int> f(m + 1);
  vector<ll> sum(m + 1);
  f[1] = sum[1] = x;

  if (n <= m) {
    for (int i = 2; i <= n; i++) {
      f[i] = 1ll * f[i - 1] * f[i - 1] % m;
      sum[i] = sum[i - 1] + f[i];
    }
    ll res = sum[n];
    cout << res << '\\n';
    return;
  }

  ll ans = x, S;
  vector<int> last(m + 1, -1);
  last[x] = 1;
  int len, k;
  for (int i = 2; i <= m; i++) {
    f[i] = 1ll * f[i - 1] * f[i - 1] % m;
    sum[i] = sum[i - 1] + f[i];
    if (last[f[i]] != -1) {
      k = last[f[i]];
      len = i - k;
      S = sum[i] - sum[last[f[i]]];
      n -= i - 1;
      break;
    }
    ans += f[i];
    last[f[i]] = i;
  }
  ans += (n / len) * S + sum[k + n % len - 1] - sum[k - 1];
  cout << ans << '\\n';
}
int main() {
#ifdef Local
  freopen("input.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  run();
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans(m, 0);
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans.at(j) = x;
    x = (x * x) % m;
    len++;
    j++;
  }

  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans(n, 0);
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans.at(j) = x;
    x = (x * x) % m;
    len++;
    j++;
  }

  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

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

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans;
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans.push_back(x);
    x = (x * x) % m;
    len++;
    j++;
  }
  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

  return 0;
}

Error: -11

Fixed submission:

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans;
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans[j] = x;
    x = (x * x) % m;
    len++;
    j++;
  }
  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << 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) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

#define INF ((1 << 30) - 1)
#define LLINF (1LL << 60)
#define EPS (1e-10)

ll f(ll x, ll m) { return x % m; }

ll sums[201010], vis[202020];

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

  if (N <= 1000000) {
    ll sum = X;
    ll A = f(X * X, M);
    rep(i, N - 1) {
      sum += A;
      A = f(A * A, M);
    }
    cout << sum << endl;
  } else {
    ll ans = 0, cnt = 0;
    fill(vis, vis + M, -1);
    while (N > 0) {
      if (vis[X] >= 0) {
        // 一度見たところ
        ll len = cnt - vis[X];
        ll tmpAns = ans;
        ans += (tmpAns - sums[vis[X]]) * (N / len);
        N %= len;
        break;
      }

      if (N > 0) {
        vis[X] = cnt++;
        ans += X;
        sums[cnt] = ans;
        X = f(X * X, M);
        N--;
      }
    }
    while (N > 0) {
      if (N > 0) {
        ans += X;
        X = f(X * X, M);
        N--;
      }
    }

    cout << ans << endl;
  }
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

#define INF ((1 << 30) - 1)
#define LLINF (1LL << 60)
#define EPS (1e-10)

ll f(ll x, ll m) { return x % m; }

ll sums[201010];

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

  if (N <= 1000000) {
    ll sum = X;
    ll A = f(X * X, M);
    rep(i, N - 1) {
      sum += A;
      A = f(A * A, M);
    }
    cout << sum << endl;
  } else {
    ll sum = X, cnt = 1;
    ll A = X;
    map<ll, ll> nxt;
    rep(i, N - 1) {
      if (nxt.find(A) == nxt.end()) {
        // ない場合
        ll nxtA = f(A * A, M);

        if (nxtA == 0) {
          cout << sum << endl;
          return 0;
        }

        cnt++;
        sum += nxtA;
        nxt[A] = nxtA;
        A = nxtA;
      } else {
        break;
      }
    }

    ll tmpA = A, cur2 = 0;
    while (true) {
      A = f(A * A, M);
      sums[cur2] = A;
      if (cur2 > 0)
        sums[cur2] += sums[cur2 - 1];
      cur2++;
      if (A == tmpA) {
        break;
      }
    }

    // cout << cur2 << ", " << sums[cur2-1] << endl;

    // 残り個数
    ll let = N - cnt;

    // ループ分足し合わせる
    sum += (let / cur2) * sums[cur2 - 1];
    // 使った分引く
    let %= cur2;
    if (let > 0)
      sum += sums[let - 1];

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

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

  ll ans = 0;

  ll now = x;
  vector<ll> sum;

  map<ll, int> mp;

  if (x == 1) {
    cout << x * n << endl;
    return 0;
  }

  sum.push_back(0);

  rep(i, n) {
    sum.push_back(now);
    if (mp.count(x) == 0)
      mp[x] = i + 1;
    else {
      ll sa = n - i;
      ll phase = i - mp[x] + 1;
      ll plus = sum[sum.size() - 1] - sum[mp[x]];

      // cout << plus << endl;

      ans += sa / phase * plus;
      sa -= sa / phase * phase;

      ans += sum[mp[x] + sa - 1] - sum[mp[x] - 1];
      break;
    }

    ans += x;

    x *= x;
    x %= m;

    now += x;

    if (x == 0)
      break;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

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

  ll ans = 0;

  ll now = x;
  vector<ll> sum;

  map<ll, int> mp;

  if (x == 1) {
    cout << x * n << endl;
    return 0;
  }

  sum.push_back(0);
  mp[0] = 0;

  rep(i, n) {
    sum.push_back(now);
    if (mp.count(x) == 0)
      mp[x] = i + 1;
    else {
      ll sa = n - i;
      ll phase = i - mp[x];
      ll plus = sum[sum.size() - 1] - sum[mp[x]];

      ans += sa / phase * plus;
      sa -= sa / phase * phase;

      if (mp[x] != 0)
        ans -= sum[mp[x] - 1];

      ans += sum[mp[x] + sa - 1];
      break;
    }

    ans += x;

    x *= x;
    x %= m;

    now += x;

    if (x == 0)
      break;
  }

  cout << ans << endl;

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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long memo[100010] = {0}, memo1[100010] = {0};
int main() {
  long long n, x, m;
  cin >> n >> x >> m;
  long long ans = x;
  long long loop, loop1;
  bool loop2 = false;
  memo[x] = 1;
  memo1[1] = x;
  for (int i = 2; i <= min(n, m); i++) {
    x *= x;
    x %= m;
    if (memo[x] != 0) {
      loop = ans - memo1[memo[x] - 1];
      loop1 = i - memo[x];
      loop2 = true;
      n -= i - 1;
      break;
    }
    ans += x;
    memo[x] = i;
    memo1[i] = ans;
  }
  if (loop2) {
    ans += loop * (n / loop1);
    for (int i = 0; i < n % loop1; i++) {
      ans += x;
      x *= x;
      x %= m;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long memo[100010] = {0}, memo1[100010] = {0};
int main() {
  long long n, x, m;
  cin >> n >> x >> m;
  long long ans = x;
  long long loop, loop1;
  memo[x] = 1;
  memo1[1] = x;
  for (int i = 2; i <= m; i++) {
    x *= x;
    x %= m;
    if (memo[x] != 0) {
      loop = ans - memo1[memo[x] - 1];
      loop1 = i - memo[x];
      n -= i - 1;
      break;
    }
    ans += x;
    memo[x] = i;
    memo1[i] = ans;
  }
  ans += loop * (n / loop1);
  for (int i = 0; i < n % loop1; i++) {
    ans += x;
    x *= x;
    x %= m;
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  vector<int> check(m + 5, 0);
  vector<ll> suma(m + 5, 0);
  vector<ll> a(m + 5);
  a.at(1) = x;
  suma.at(1) = x;
  check.at(x) = 1;

  int loop = 0;
  int first = 0;
  ll loopsum = 0;
  ll firstsum = 0;

  for (int i = 2; i <= m + 5; i++) {
    a.at(i) = a.at(i - 1) * a.at(i - 1);
    if (a.at(i) >= m)
      a.at(i) %= m;
    // cout << a.at(i) << endl;
    if (check.at(a.at(i)) == 0) {
      check.at(a.at(i)) = i;
      suma.at(i) = suma.at(i - 1) + a.at(i);
    } else {
      loop = i - check.at(a.at(i));
      first = max(0, check.at(a.at(i)) - 1);
      loopsum = suma.at(i - 1) - suma.at(first);
      firstsum = suma.at(first);
      break;
    }
  }

  int k = first;
  if (n < first)
    k = n;
  ll p = (n - k) / loop;
  ll r = n - k - loop * p;
  if (r < 0)
    r = 0;
  ll ans = suma.at(k) + loopsum * p + suma.at(first + r) - suma.at(first);
  cout << ans;
}

Error: WA

Fixed submission:

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

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

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

  vector<int> check(m + 5, 0);
  vector<ll> suma(m + 5, 0);
  vector<ll> a(m + 5);
  a.at(1) = x;
  suma.at(1) = x;

  int loop = 0;
  int first = 0;
  ll loopsum = 0;
  ll firstsum = 0;

  for (int i = 2; i <= m; i++) {
    a.at(i) = a.at(i - 1) * a.at(i - 1);
    if (a.at(i) >= m)
      a.at(i) %= m;
    // cout << a.at(i) << endl;
    if (check.at(a.at(i)) == 0) {
      check.at(a.at(i)) = i;
      suma.at(i) = suma.at(i - 1) + a.at(i);
    } else {
      loop = i - check.at(a.at(i));
      first = max(0, check.at(a.at(i)) - 1);
      loopsum = suma.at(i - 1) - suma.at(first);
      firstsum = suma.at(first);
      break;
    }
  }

  int k = first;
  if (n < first)
    k = n;
  int p = (n - k) / loop;
  int r = n - k - loop * p;
  if (r < 0)
    r = 0;
  ll ans = suma.at(k) + loopsum * p + suma.at(first + r) - suma.at(first);
  cout << ans;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL n, x, m, a[100005], cnt, sum[100005], mpd[100005];
bool vis[100005];
int main() {
  scanf("%lld %lld %lld", &n, &x, &m);
  LL now = x;
  while (!vis[now]) {
    vis[now] = true;
    a[++cnt] = now;
    sum[cnt] = sum[cnt - 1] + now;
    mpd[now] = cnt;
    now *= now;
    now %= m;
  }
  if (n <= cnt)
    printf("%lld", sum[n]);
  else {
    LL ans = sum[cnt];
    LL beg = mpd[now];
    LL len = (cnt - beg + 1);
    n -= cnt;
    LL dodo = n / len;
    ans += dodo * (sum[cnt] - sum[beg - 1]);
    n -= dodo * len;
    LL i = beg;
    while (n-- > 0) {
      ans += a[i];
      ++i;
    }
    printf("%lld", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL n, x, m, a[100005], cnt, sum[100005], mpd[100005];
bool vis[100005];
int main() {
  scanf("%lld %lld %lld", &n, &x, &m);
  LL now = x;
  while (!vis[now]) {
    vis[now] = true;
    a[++cnt] = now;
    sum[cnt] = sum[cnt - 1] + now;
    mpd[now] = cnt;
    now *= now;
    now %= m;
  }
  if (n <= cnt)
    printf("%lld", sum[n]);
  else {
    LL ans = sum[cnt];
    LL beg = mpd[now];
    LL len = (cnt - beg + 1);
    n -= cnt;
    while (n >= len) {
      n -= len;
      ans += sum[cnt] - sum[beg - 1];
    }
    LL i = beg;
    while (n-- > 0) {
      ans += a[i];
      ++i;
    }
    printf("%lld", ans);
  }
  return 0;
}