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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

int main() {
  llong N, X, M;
  cin >> N >> X >> M;
  int first[M];
  llong first_sum[M];
  rep(i, 0, M) {
    first[i] = -1;
    first_sum[i] = 0;
  }

  llong ans = 0;
  if (M < N) {
    int roop;
    llong roop_ans;
    rep(i, 0, N) {
      if (first[X] >= 0) {
        roop = i - first[X];
        roop_ans = ans - first_sum[X];
        break;
      } else {
        first[X] = i;
        first_sum[X] = ans;
        ans += X;
      }
      X = (X * X) % M;
    }
    ans += roop_ans * ((N - first[X]) / roop - 1);
    N = (N - first[X]) % roop;
  }
  rep(i, 0, N) {
    ans += X;
    X = (X * X) % M;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

int main() {
  llong N, X, M;
  cin >> N >> X >> M;
  int first[M];
  llong first_sum[M];
  rep(i, 0, M) {
    first[i] = -1;
    first_sum[i] = 0;
  }

  llong ans = 0;
  if (M < N) {
    int roop;
    llong roop_ans;
    rep(i, 0, M) {
      if (first[X] >= 0) {
        roop = i - first[X];
        roop_ans = ans - first_sum[X];
        break;
      } else {
        first[X] = i;
        first_sum[X] = ans;
        ans += X;
      }
      X = (X * X) % M;
    }
    ans += roop_ans * ((N - first[X]) / roop - 1);
    N = (N - first[X]) % roop;
  }
  rep(i, 0, N) {
    ans += X;
    X = (X * X) % M;
  }
  cout << ans;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

#define ll long long

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

  vector<ll> remainderIndices(m, -1);

  vector<ll> beforeCycle;
  ll beforeCycleSum = 0, beforeCycleSize = 0;
  while (remainderIndices[x] == -1) {
    beforeCycle.push_back(x);

    remainderIndices[x] = beforeCycleSize++;
    beforeCycleSum += x;
    x = x * x % m;
  }

  ll cycleSize = beforeCycleSize - remainderIndices[x];

  ll totalSum = 0;
  if (n <= cycleSize)
    for (ll i = 0; i < n; i++)
      totalSum += beforeCycle[i];
  else {
    ll cycleSum = 0;

    for (ll i = remainderIndices[x]; i < beforeCycleSize; i++)
      cycleSum += beforeCycle[i];

    totalSum = beforeCycleSum;
    n -= beforeCycleSize;
    totalSum += (n / cycleSize) * cycleSum;
    n %= cycleSize;

    for (ll i = 0; i < n; i++)
      totalSum += beforeCycle[remainderIndices[x] + i];
  }

  cout << totalSum << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define ll long long

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

  cin >> n >> x >> m;

  ll a[n];
  ll sum = x;

  a[0] = x;

  for (ll i = 1; i < n; i++) {
    a[i] = (a[i - 1] * a[i - 1]) % m;
    sum += a[i];
  }

  cout << sum << endl;

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

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  ll X, M;
  cin >> X >> M;
  ll S = 0;          // 最終的に答えとなる和
  vector<P> memo(M); // その値Xが { 何番目に出現したか, それまでの計算結果(和) }
  for (ll i = 0; i < N; ++i) {
    if (memo[X]
            .first) { // もしその値がすでに配列にあれば、ループ最後の回りきらないフェーズまでワープさせる
      // 今の順番と、かつてXが出てきた番号の差。これが、ループ1周分の長さになる
      const auto t = i - memo[X].first;
      // この時点で、あとループを何周するか？
      const auto c = (N - i - 1) / t;
      // ループ1周分の長さ * ループを回る回数 をiに足し込む
      i += t * c;
      // ループ1周分の和(累積和っぽく求める) * ループを回る回数 をSに足し込む
      S += (S - memo[X].second) * c;
    } else {             // もし配列にまだXが無ければ
      memo[X] = P(i, S); // 配列のX番目にpairを格納する。
    }
    // 必ずSとXを更新する
    S += X;
    (X *= X) %= M;
  }
  cout << S << endl;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  ll X, M;
  cin >> X >> M;
  vector<ll> tmp;
  tmp.push_back(X);
  for (ll i = 0; i < N - 1; ++i) {
    ll b = tmp.back();
    tmp.push_back((b * b) % M);
  }
  cout << accumulate(all(tmp), 0LL) << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main(void) {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1);
  vector<int> a;
  int length = 0;
  ll total = 0;
  while (id[x] == -1 && length != n) {
    a.push_back(x);
    id[x] = length;
    length++;
    total += x;
    x = (x * x) % m;
  }
  if (length == n) {
    cout << total << endl;
    return 0;
  }

  int circleLength = length - id[x];
  ll oneCircleSum = 0;
  for (int i = id[x]; i < length; i++) {
    oneCircleSum += a[i];
  }

  ll ans = total;
  n -= length;
  ans += oneCircleSum * (n / circleLength); // 一周回った回数分
  n %= circleLength;                        // 回りきらなかった余り
  rep(i, n) ans += a[id[x] + i];
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main(void) {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1);
  vector<int> a;
  int length = 0;
  ll total = 0;
  while (id[x] == -1) {
    a.push_back(x);
    id[x] = length;
    length++;
    total += x;
    x = (x * x) % m;
    cout << x << endl;
  }

  int circleLength = length - id[x];
  ll oneCircleSum = 0;
  for (int i = id[x]; i < length; i++) {
    oneCircleSum += a[i];
  }
  ll ans = 0;

  if (length <= n) {
    rep(i, n) ans += a[i];
  } else {
    n -= length;
    ans += oneCircleSum * (n / circleLength); // 一周回った回数分
    n %= circleLength;                        // 回りきらなかった余り
    rep(i, n) ans += a[id[x] + i];
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  // 入力
  long long N, X, M, Sum_A = 0;
  cin >> N >> X >> M;

  if (M > 1) {

    // 数列{A_n}
    vector<long long> A(M + 1, 0);
    A.at(1) = X;
    for (int i = 2; i <= M; i++) {
      A.at(i) = (A.at(i - 1) * A.at(i - 1)) % M;
    }

    // 循環節があるか確認
    int nagasa = 0, hajime = 0;
    for (int i = 1; i < M; i++) {
      if (A.at(M - i) == A.at(M)) { // A_{1~M-1}のうちA_Mと同じで最も近いもの
        nagasa = i;                 // 循環節の最小の長さ
        for (int j = 1; j <= M - i; j++) {
          if (A.at(j) == A.at(M)) { // A_{1~M-1}のうちA_Mと同じで最も遠いもの
            hajime = j - 1; // 循環節1節目のうちA_Mと同じものの添え字
            break;          // ループを抜ける
          }
        }
        if (nagasa == i) {
          break; // ループを抜ける
        }
      }
    }

    // 循環しない場合
    if (nagasa == 0) {
      for (int i = 0; i <= N; i++) {
        Sum_A += A.at(i);
      }
    }

    // 循環する場合
    else {
      long long shou, amari;
      shou = (N - hajime) / nagasa; // 循環節の数-(0or1)
      // amari = hajime + (N-hajime)%nagasa;  // 循環節(or2節目)にたどり着くまで
      amari = N - shou * nagasa;

      // 循環節の部分の和
      for (int i = M - nagasa; i < M; i++) {
        Sum_A += A.at(i) * shou;
      }

      // 循環節にたどり着くまでの和
      for (int i = 0; i <= amari; i++) {
        Sum_A += A.at(i);
      }
    }
  }
  // 出力
  cout << Sum_A << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  // 入力
  long long N, X, M, Sum_A = 0;
  cin >> N >> X >> M;

  // 数列{A_n}
  vector<long long> A(M + 1, 0);
  A.at(1) = X;
  for (int i = 2; i <= M; i++) {
    A.at(i) = (A.at(i - 1) * A.at(i - 1)) % M;
  }

  // 循環節があるか確認
  int nagasa = 0, hajime = 0;
  for (int i = 1; i < M; i++) {
    if (A.at(M - i) == A.at(M)) { // A_{1~M-1}のうちA_Mと同じで最も近いもの
      nagasa = i;                 // 循環節の最小の長さ
      for (int j = 1; j <= M - i; j++) {
        if (A.at(j) == A.at(M)) { // A_{1~M-1}のうちA_Mと同じで最も遠いもの
          hajime = j - 1; // 循環節1節目のうちA_Mと同じものの添え字
          break;          // ループを抜ける
        }
      }
      if (nagasa == i) {
        break; // ループを抜ける
      }
    }
  }

  // 循環しない場合
  if (nagasa == 0) {
    for (int i = 0; i <= N; i++) {
      Sum_A += A.at(i);
    }
  }

  // 循環する場合
  else {
    long long shou, amari;
    shou = (N - hajime) / nagasa; // 循環節の数-(0or1)
    // amari = hajime + (N-hajime)%nagasa;  // 循環節(or2節目)にたどり着くまで
    amari = N - shou * nagasa;

    // 循環節の部分の和
    for (int i = M - nagasa; i < M; i++) {
      Sum_A += A.at(i) * shou;
    }

    // 循環節にたどり着くまでの和
    for (int i = 0; i <= amari; i++) {
      Sum_A += A.at(i);
    }
  }

  // 出力
  cout << Sum_A << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long int b[100001], pre[1000001], a[100001];
int main() {
  long long n, m, x;
  cin >> n >> x >> m;
  long long ans = pre[1] = a[1] = x;
  b[x] = 1;
  for (int i = 2; i <= n; i++) {
    a[i] = a[i - 1] * a[i - 1] % m;
    ans = pre[i] = pre[i - 1] + a[i];
    if (b[a[i]] > 0) {
      long long t1 = (n - b[a[i]]) / (i - b[a[i]]),
                t2 = (n - b[a[i]]) % (i - b[a[i]]);
      ans = pre[b[a[i]] + t2] + (pre[i] - pre[b[a[i]]]) * t1;
      break;
    }
    b[a[i]] = i;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long int t = 0;
long long int wy(long long int s, long long k, long long int u) {
  int y = 0;
  if (s == 1)
    return u;
  else {
    s--;
    y = wy(s, k, u);
    t += y;
    y = y * y % k;
    return y;
  }
}
int main() {
  long long int m, n, x, p;
  scanf("%d%d%d", &n, &x, &m);
  wy(n + 1, m, x);
  printf("%d", t);
  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 dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  map<ll, int> mp;
  vector<ll> v;
  ll a = x;
  int k = -1;
  for (int i = 0; i < m; i++) {
    if (mp[a]) {
      k = i;
      break;
    }
    mp[a] = i;
    v.push_back(a);
    a = (a * a) % m;
  }
  int s = mp[a];
  if (n <= s) {
    ll ans = 0;
    rep(i, n) { ans += v[i]; }
    cout << ans << endl;
    return 0;
  }
  int t = k - mp[a];

  ll w = 0;
  ll y = 0;
  rep(i, (int)v.size()) {
    if (i < s)
      w += v[i];
    else
      y += v[i];
  }
  if (n <= s) {
    ll ans = 0;
    rep(i, n) { ans += v[i]; }
    cout << ans << endl;
  } else {
    ll z = 0;
    for (int i = 0; i < (n - s) % t; i++) {
      z += v[i + s];
    }
    cout << w + (n - s) / t * y + z << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  map<ll, int> mp;
  vector<ll> v;
  ll a = x;
  int k;
  for (int i = 0; i < m; i++) {
    if (mp[a]) {
      k = i;
      break;
    }
    mp[a] = i;
    v.push_back(a);
    a = (a * a) % m;
  }
  int s = mp[a];
  int t = k - mp[a];

  ll w = 0;
  ll y = 0;
  rep(i, (int)v.size()) {
    if (i < s)
      w += v[i];
    else
      y += v[i];
  }
  if (n <= s) {
    ll ans = 0;
    rep(i, n) { ans += v[i]; }
    cout << ans << endl;
  } else {
    ll z = 0;
    for (int i = 0; i < (n - s) % t; i++) {
      z += v[i + s];
    }
    cout << w + (n - s) / t * y + z << endl;
  }
}
Problem p02550 (C++) — buggy submission, verdict: 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--;
  }
  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;
}


Error: 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--;
  }
  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, verdict: Runtime Error

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

int main(void) {
  long n, x, m, ans = 0;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<long> k, s(m);
  map<long, long> mp;
  long cur = x, d = 0, e = 0;
  k.push_back(cur);
  mp[cur] = 1;
  for (int i = 2; i <= m; i++) {
    cur = (cur * cur) % m;
    k.push_back(cur);
    if (1 <= mp[cur]) {
      d = mp[cur];
      e = i;
      break;
    }
    mp[cur] = i;
  }
  // cout << d << e << endl;
  long fsum = 0, cirsum = 0, resum = 0;
  for (int i = 0; i < d - 1; i++)
    fsum += k[i];
  for (int i = d - 1; i < e - 1; i++)
    cirsum += k[i];
  for (int i = 0; i <= (n - d) % (e - d); i++)
    resum += k[d - 1 + i];
  // cout << fsum << ' ' << cirsum <<' '<<resum << endl;
  cout << fsum + (n - d) / (e - d) * cirsum + resum << endl;
}


Error: 0

Fixed submission:

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

int main(void) {
  long n, x, m, ans = 0;
  cin >> n >> x >> m;
  vector<long> k, s(m);
  map<long, long> mp;
  long cur = x, d = 0, e = 0;
  k.push_back(cur);
  mp[cur] = 1;
  for (int i = 2; i <= m; i++) {
    cur = (cur * cur) % m;
    k.push_back(cur);
    if (1 <= mp[cur]) {
      d = mp[cur];
      e = i;
      break;
    }
    mp[cur] = i;
  }
  // cout << d << e << endl;
  long fsum = 0, cirsum = 0, resum = 0;
  for (int i = 0; i < d - 1; i++)
    fsum += k[i];
  for (int i = d - 1; i < e - 1; i++)
    cirsum += k[i];
  for (int i = 0; i <= (n - d) % (e - d); i++)
    resum += k[d - 1 + i];
  // cout << fsum << ' ' << cirsum <<' '<<resum << endl;
  cout << fsum + (n - d) / (e - d) * cirsum + resum << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  ll X, M;
  cin >> X >> M;
  ll S = 0;          // 最終的に答えとなる和
  vector<P> memo(M); // その値Xが { 何番目に出現したか, それまでの計算結果(和) }
  for (ll i = 0; i < N; ++i) {
    if (memo[X].first) { // もしその値がすでに配列にあれば
      // 今の順番と、かつてXが出てきた番号の差。これがループ1周分の長さになる
      const auto t = i - memo[X].first;
      // ループ1周分の残りの長さ
      const auto c = (N - i - 1) / t;
      i += t * c; // ループをスキップ?
      S += (S - memo[X].second) * c;
    } else {             // もし配列にまだXが無ければ
      memo[X] = P(i, S); // 配列のX番目にpairを格納する。
    }
    // 必ずSとXを更新する
    S += X;
    (X *= X) %= M;
  }
  cout << S << endl;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  ll X, M;
  cin >> X >> M;
  ll S = 0;          // 最終的に答えとなる和
  vector<P> memo(M); // その値Xが { 何番目に出現したか, それまでの計算結果(和) }
  for (int i = 0; i < N; ++i) {
    if (memo[X].first) { // もしその値がすでに配列にあれば
      // 今の順番と、かつてXが出てきた番号の差。これがループ1周分の長さになる
      const auto t = i - memo[X].first;
      // ループ1周分の残りの長さ
      const auto c = (N - i - 1) / t;
      i += t * c; // ループをスキップ?
      S += (S - memo[X].second) * c;
    } else {             // もし配列にまだXが無ければ
      memo[X] = P(i, S); // 配列のX番目にpairを格納する。
    }
    // 必ずSとXを更新する
    S += X;
    (X *= X) %= M;
  }
  cout << S << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  ll prev = x;
  vector<ll> a;
  a.push_back(x);
  vector<ll> mods(m, -1);
  ll t = -1;
  ll si = -1;
  rep1(i, n) {
    ll curr = prev * prev % m;
    if (mods[curr] == -1)
      mods[curr] = i;
    else {
      t = i - mods[curr];
      si = mods[curr];
      break;
    }
    a.push_back(curr);
    prev = curr;
  }
  // rep(i, a.size()) cout << a[i] << " ";
  // cout << endl;
  if (t == -1) {
    ll res = 0;
    rep(i, static_cast<int>(a.size())) res += a[i];
    cout << res << endl;
    return 0;
  }

  ll res = 0;
  rep(i, si) res += a[i];
  ll loop = 0;
  for (int i = si; i < si + t; ++i)
    loop += a[i];
  ll times = (n - si) / t;
  res += loop * times;
  ll remain = (n - si) % t;
  for (int i = si; i < si + remain; ++i)
    res += a[i];
  // cout << si << " " << t << " " << loop << " " << times << " " << remain <<
  // endl;

  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

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

  vector<ll> mods(m, 0);
  mods[x] = 1;
  ll res = x;
  ll prev = x;
  ll start = -1;
  ll end = -1;
  vector<ll> a;
  a.push_back(x);
  for (int i = 1; i < n; ++i) {
    ll curr = prev * prev % m;
    if (start == -1 && mods[curr] == 1) {
      start = i;
    }
    if (start != -1 && mods[curr] == 2) {
      end = i;
      break;
    }
    res += curr;
    a.push_back(curr);
    if (i == n - 1) {
      cout << res << endl;
      return 0;
    }
    ++mods[curr];
    prev = curr;
  }

  // cout << start << " " << end << endl;
  // // for(int i = 0; i < a.size(); ++i) cout << a[i] << " ";
  // cout << endl;

  ll ans = 0;
  for (int i = 0; i < start; ++i)
    ans += a[i];
  ll loop = 0;
  for (int i = start; i < end; ++i)
    loop += a[i];
  ll l = end - start;
  ll t = (n - start) / l;
  ans += loop * t;

  ll r = (n - start) % l;
  // cout << r <s< endl;
  for (int i = start - l; i <= r + 1; ++i) {
    ans += a[i];
    // cout s<< i << " " << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
const int N = 2e5 + 7;
const ll mod = 998244353;

int main() {
  fastIO;
  ll n, x, m;
  cin >> n >> x >> m;
  map<ll, int> first;
  set<ll> seen;
  ll prev = x;
  seen.insert(x);
  first[x] = 1;
  vector<ll> accum = {0, x};
  ll sum = x;
  int st = -1, len = 0;
  for (int i = 2; i <= 2 * m + 1; i++) {
    ll curr = (prev * prev) % m;
    if (seen.count(curr)) {
      st = first[curr];
      len = i - st;
      break;
    } else {
      first[curr] = i;
      seen.insert(curr);
    }
    sum += curr;
    accum.push_back(sum);
    prev = curr;
  }
  ll ans = 0;
  if (n < accum.size()) {
    ans = accum[n];
  } else {
    assert(st != -1);
    ans = accum[st - 1];
    n -= st - 1;
    ans += (n / len) * (accum.back() - accum[st - 1]);
    ans += accum[st + (n % len) - 1] - accum[st - 1];
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
const int N = 2e5 + 7;
const ll mod = 998244353;

int main() {
  fastIO;
  ll n, x, m;
  cin >> n >> x >> m;
  map<ll, int> first;
  set<ll> seen;
  ll prev = x;
  seen.insert(x);
  vector<ll> accum = {0, x};
  ll sum = x;
  int st = -1, len = 0;
  for (int i = 2; i <= m + 1; i++) {
    ll curr = (prev * prev) % m;
    if (seen.count(curr)) {
      st = first[curr];
      len = i - st;
      break;
    } else {
      first[curr] = i;
      seen.insert(curr);
    }
    sum += curr;
    accum.push_back(sum);
    prev = curr;
  }
  ll ans = 0;
  if (n < accum.size()) {
    ans = accum[n];
  } else {
    assert(st > 0);
    ans = accum[st - 1];
    n -= st - 1;
    ans += (n / len) * (accum.back() - accum[st - 1]);
    ans += accum[st + (n % len) - 1] - accum[st - 1];
  }
  cout << ans;
}