alexjercan/bugnet huggingface dataset:
Problem p02550 (C++) — buggy submission, verdict: 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;
  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;
}


Error: WA

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

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

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
ll loc[NMAX];
ll pre_sum[NMAX];
int main(int argc, char const *argv[]) {
  ll n, ans = 0;
  ll x, m, pos = 1;
  scanf("%lld%lld%lld", &n, &x, &m);
  loc[x] = 1;
  pre_sum[1] = x % m;
  for (register ll i = 2; i <= n && !loc[x * x % m]; i++) {
    x = x * x % m;
    loc[x] = i;
    pre_sum[i] = pre_sum[i - 1] + x;
    pos = i;
  }
  if (pos == n)
    return 0 * printf("%lld\\n", pre_sum[n]);
  ans += pre_sum[loc[x * x % m] - 1];
  ll T = pos - loc[x * x % m] + 1;
  ans += (pre_sum[pos] - pre_sum[loc[x * x % m] - 1]) *
         ((n - loc[x * x % m] + 1) / T);
  ans += pre_sum[loc[x * x % m] - 1 + (n - loc[x * x % m] + 1) % T] -
         pre_sum[loc[x * x % m] - 1];
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
ll loc[NMAX];
ll pre_sum[NMAX];
int main(int argc, char const *argv[]) {
  ll n, ans = 0;
  ll x, m, pos;
  scanf("%lld%lld%lld", &n, &x, &m);
  for (register int i = 0; i <= m + 1; i++)
    loc[i] = -1;
  loc[x] = 1;
  pre_sum[1] = x;
  for (register ll i = 2; i <= n && !(~loc[x * x % m]); i++) {
    x = x * x % m;
    loc[x] = i;
    pre_sum[i] = pre_sum[i - 1] + x;
    pos = i;
  }
  if (pos == n)
    return 0 * printf("%lld\\n", pre_sum[n]);
  ans += pre_sum[loc[x * x % m] - 1];
  ll T = pos - loc[x * x % m] + 1;
  ans += (pre_sum[pos] - pre_sum[loc[x * x % m] - 1]) *
         ((n - loc[x * x % m] + 1) / T);
  ans += pre_sum[loc[x * x % m] - 1 + (n - loc[x * x % m] + 1) % T] -
         pre_sum[loc[x * x % m] - 1];
  printf("%lld\\n", ans);
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

long long n;
int x, m;
int vis[100009];
long long tmp[100009];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  memset(vis, 0xff, sizeof(vis));
  long long ans = -1, pre, cur;
  tmp[0] = x;
  vis[x] = 0;
  pre = x;
  for (int i = 1; i < n; i++) {
    cur = pre * pre;
    cur %= m;
    if (cur == 0) {
      ans = tmp[i - 1];
      break;
    }
    if (vis[cur] >= 0) {
      if (vis[cur] > 0)
        ans = tmp[vis[cur] - 1];
      else
        ans = 0;
      long long k = n - vis[cur];
      long long tt = tmp[i - 1];
      int d = i - vis[cur];
      if (vis[cur] > 0)
        tt -= tmp[vis[cur] - 1];
      ans += tt * (k / d);
      if (k % d) {
        ans += tmp[k % d - 1 + vis[cur]];
        if (vis[cur] > 0)
          ans -= tmp[vis[cur] - 1];
      }
      break;
    }
    tmp[i] = cur + tmp[i - 1];
    vis[cur] = i;
    pre = cur;
  }
  if (ans < 0)
    ans = tmp[n - 1];
  cout << ans << endl;
  // system("pause");
  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

long long n;
int x, m;
int vis[100009];
long long tmp[100009];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  memset(vis, 0xff, sizeof(vis));
  long long ans = -1, pre, cur;
  tmp[0] = x;
  vis[x] = true;
  pre = x;
  for (int i = 1; i < n; i++) {
    cur = pre * pre;
    cur %= m;
    if (cur == 0) {
      ans = tmp[i - 1];
      break;
    }
    if (vis[cur] >= 0) {
      if (vis[cur] > 0)
        ans = tmp[vis[cur] - 1];
      else
        ans = 0;
      long long k = n - vis[cur];
      long long tt = tmp[i - 1];
      int d = i - vis[cur];
      // if (vis[cur] > 0) tt -= tmp[vis[cur] - 1];
      ans += tt * (k / d);
      if (k % d) {
        ans += tmp[k % d - 1 + vis[cur]];
        if (vis[cur] > 0)
          ans -= tmp[vis[cur] - 1];
      }
      break;
    }
    tmp[i] = cur + tmp[i - 1];
    vis[cur] = i;
    pre = cur;
  }
  if (ans < 0)
    ans = tmp[n - 1];
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18;
void print() { cout << endl; }
template<typename Head,typename... Tail> void print(Head head,Tail... tail){cout<<head;if(sizeof...(Tail)>0)cout<<" ";print(tail...);}
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<head;print0(tail...);}
// clang-format on

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  vector<ll> exist(100003, -1);
  vector<ll> totals(100003, 0);
  ll iterate = 1;
  ll total = 0;
  ll a = X;
  bool loop_done = false;
  while (iterate <= N) {
    total = total + a;
    if (!loop_done) {
      if (exist[a] > 0) {
        ll loop_iter = iterate - exist[a];
        ll loop_sum = totals[iterate - 1] - totals[exist[a] - 1];
        ll loop_num = max(N / loop_iter - 3, 0L);
        total = total + loop_num * loop_sum;
        iterate = iterate + (loop_num * loop_iter);
        loop_done = true;
      } else {
        totals[iterate] = total;
        exist[a] = iterate;
      }
    }
    a = (a * a) % M;
    iterate++;
  }
  print(total);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18;
void print() { cout << endl; }
template<typename Head,typename... Tail> void print(Head head,Tail... tail){cout<<head;if(sizeof...(Tail)>0)cout<<" ";print(tail...);}
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<head;print0(tail...);}
// clang-format on

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  vector<ll> exist(100003, -1);
  vector<ll> totals(100003, 0);
  ll iterate = 1;
  ll total = 0;
  ll a = X;
  bool loop_done = false;
  while (iterate <= N) {
    if (exist[a] > 0) {
      ll loop_iter = iterate - exist[a];
      ll loop_sum = totals[iterate - 1] - totals[exist[a] - 1];
      ll loop_num = max(N / loop_iter - 3, 0L);
      total = total + loop_num * loop_sum;
      iterate = iterate + (loop_num * loop_iter);
      loop_done = true;
    }
    total = total + a;
    if (!loop_done) {
      totals[iterate] = total;
      exist[a] = iterate;
    }
    a = (a * a) % M;
    iterate++;
  }
  print(total);
}
Problem p02550 (C++) — buggy submission, verdict: 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];
  if (cnt)
    ans += (n / cnt) * sum[cnt] + sum[n % cnt];
  cout << ans << nl;

  AC;
}

Error: -8

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];
  ans += (n / cnt) * sum[cnt] + sum[n % cnt];
  cout << ans << nl;

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

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 1e5 + 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  if (x == 1) {
    cout << n << "\\n";
    return 0;
  } else if (x == 0) {
    cout << "0\\n";
    return 0;
  }
  set<ll> s;
  vector<ll> rem;
  ll sum = 0;
  ll same = -1;
  while (1) {
    ans += x;
    s.insert(x);
    rem.pb(x);
    n--;
    if (n == 0) {
      break;
    }
    x *= x;
    x %= m;
    if (s.find(x) != s.end()) {
      same = x;
      break;
    }
  }
  ll l = 0;
  bool ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      sum += it;
      l++;
    }
  }
  if (l != 0) {
    ans += (n / l) * sum;
    n -= ((n / l) * l);
  }
  ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      if (n == 0) {
        break;
      }
      ans += it;
      n--;
    }
  }
  cout << ans << "\\n";
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 1e5 + 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  if (x == 1) {
    cout << n << "\\n";
    return 0;
  } else if (x == 0) {
    cout << "0\\n";
    return 0;
  }
  set<ll> s;
  vector<ll> rem;
  ll sum = 0;
  ll same = -1;
  while (1) {
    ans += x;
    s.insert(x);
    rem.pb(x);
    n--;
    if (n == 0) {
      break;
    }
    x *= x;
    x %= m;
    if (s.find(x) != s.end()) {
      same = x;
      break;
    }
  }
  ll l = 0;
  bool ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      sum += it;
      l++;
    }
  }
  ans += (n / l) * sum;
  n -= ((n / l) * l);
  ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      if (n == 0) {
        break;
      }
      ans += it;
      n--;
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

typedef vector<int> vec;
typedef vector<vec> mat;

const ll mod = 1000000007;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll modn[m];
  fill(modn, modn + m, -1);
  ll a = x % m;
  modn[a] = 1;
  ll loop = 0;
  ll cnt = 2;
  while (cnt <= n) {
    a = (a * a) % m;
    if (modn[a] != -1) {
      loop = cnt - modn[a];
      break;
    } else {
      modn[a] = cnt;
      cnt++;
    }
  }

  ll ans = 0;
  if (loop == 0) {
    rep(i, m) {
      if (modn[i] == -1)
        continue;
      else
        ans += i;
    }
  } else {
    ll loop_num = (n - modn[a]) / loop;
    ll p = (n - modn[a]) % loop;
    rep(i, m) {
      if (modn[i] == -1)
        continue;
      if (modn[a] > modn[i])
        ans += i;
      else {
        if (modn[a] + p >= modn[i]) {
          ans += i * (loop_num + 1);
        } else {
          ans += i * loop_num;
        }
      }
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

typedef vector<int> vec;
typedef vector<vec> mat;

const ll mod = 1000000007;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll modn[m];
  fill(modn, modn + m, -1);
  ll a = x % m;
  modn[a] = 1;
  ll loop;
  ll cnt = 2;
  while (cnt <= n) {
    a = (a * a) % m;
    if (modn[a] != -1) {
      loop = cnt - modn[a];
      break;
    } else {
      modn[a] = cnt;
      cnt++;
    }
  }
  ll loop_num = (n - modn[a]) / loop;
  ll p = (n - modn[a]) % loop;
  ll ans = 0;
  rep(i, m) {
    if (modn[i] == -1)
      continue;
    if (modn[a] > modn[i])
      ans += i;
    else {
      if (modn[a] + p >= modn[i]) {
        ans += i * (loop_num + 1);
      } else {
        ans += i * loop_num;
      }
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long

using namespace std;

constexpr int MAXN = 1e5 + 5, MOD = 1e9 + 7;

int n, ans, ans2, x, m, arr[MAXN], sum[MAXN], vis[MAXN], sz;

signed main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> x >> m;
  ans += x;
  int i;
  for (i = 1; i < n; i++) {
    x = x * x % m;
    ans2 += x;
    if (x == 0)
      return cout << ans2 + ans, 0;
    if (!vis[x]) {
      arr[i] = x;
      vis[x] = i;
    } else {
      i--;
      sz = i - vis[x] + 1;
      break;
    }
  }
  if (!sz)
    return cout << ans2 + ans << endl, 0;
  int cnt = (n - vis[x]) / sz, res = (n - vis[x]) % sz;
  for (int j = 1; j <= i; j++)
    sum[j] = sum[j - 1] + arr[j];
  ans += sum[vis[x] - 1];
  ans += (sum[i] - sum[vis[x] - 1]) * cnt;
  ans += (sum[vis[x] - 1 + res] - sum[vis[x] - 1]);
  cout << ans << endl;
  return 0;
}
// 492443256176507
// 491945869537148


Error: -8

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

constexpr int MAXN = 1e5 + 5, MOD = 1e9 + 7;

long long n, ans, ans2, x, m, arr[MAXN], sum[MAXN], vis[MAXN], sz;

signed main() {
  // freopen("test.in", "r", stdin);
  // freopen("test.out", "w", stdout);
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> x >> m;
  ans += x;
  int i;
  for (i = 1; i < n; i++) {
    x = x * x % m;
    ans2 += x;
    if (x == 0)
      return cout << ans2 + ans, 0;
    if (!vis[x]) {
      arr[i] = x;
      vis[x] = i;
    } else {
      i--;
      sz = i - vis[x] + 1;
      break;
    }
  }
  // cout << vis[x] << " " << i << endl;
  int cnt = (n - vis[x]) / sz, res = (n - vis[x]) % sz;
  for (int j = 1; j <= i; j++)
    sum[j] = sum[j - 1] + arr[j];
  ans += sum[vis[x] - 1];
  ans += (sum[i] - sum[vis[x] - 1]) * cnt;
  ans += (sum[vis[x] - 1 + res] - sum[vis[x] - 1]);
  cout << ans << endl;
  return 0;
}
// 492443256176507
// 491945869537148
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll N;
ll X, M;

ll ans = 0;
ll idx[100001] = {};
ll sum[100001] = {};

signed main() {
  cin >> N >> X >> M;

  ll i = 1;
  sum[X] = ans = 1 + X;
  N--;
  while (N > 0) {
    X = X * X % M;
    ans += X;
    if (sum[X] > 0) {
      ll cycle = ans - sum[X];
      ll len = i - idx[X];
      ll tmp = (N - 1) / len;
      ans += tmp * cycle;
      N -= tmp * len;
      N--;
      break;
    }
    idx[X] = i;
    i++;
    sum[X] = ans;
    N--;
  }
  assert(N <= 100001);
  while (N > 0) {
    X = X * X % M;
    ans += X;
    N--;
  }
  cout << ans - 1 << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll N;
ll X, M;

ll ans = 0;
ll idx[100001] = {};
ll sum[100001] = {};

signed main() {
  cin >> N >> X >> M;

  ll i = 1;
  sum[X] = ans = X;
  N--;
  while (N > 0) {
    X = X * X % M;
    ans += X;
    if (sum[X] > 0) {
      ll cycle = ans - sum[X];
      ll len = i - idx[X];
      ll tmp = (N - 1) / len;
      ans += tmp * cycle;
      N -= tmp * len;
      N--;
      break;
    }
    idx[X] = i;
    i++;
    sum[X] = ans;
    N--;
  }
  assert(N <= 100001);
  while (N > 0) {
    X = X * X % M;
    ans += X;
    N--;
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = int64_t;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)

signed main() {

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

  ll ans = 0;
  set<ll> st;
  for (ll i = 0; i < n; ++i) {
    st.emplace(x);
    ans += x;
    x = x * x % m;

    if (st.find(x) != st.end()) {
      ll val = x;
      ll looplen = 0;
      ll loopgain = 0;
      do {
        loopgain += val;
        val = val * val % m;
        looplen++;
      } while (val != x);
      ans += (n - i - 1) / (looplen)*loopgain;
      ll len = (n - i - 1) % looplen;
      rep(j, len) {
        ans += x;
        x = x * x % m;
      }
      break;
    }
  }
  cout << (ans) << endl;
}

Error: TLE

Fixed submission:

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

using ll = int64_t;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)

signed main() {

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

  ll ans = 0;
  set<ll> st;
  for (ll i = 0; i < n; ++i) {
    ans += x;
    x = x * x % m;
    st.emplace(x);

    if (st.find(x) != st.end()) {
      ll val = x;
      ll looplen = 0;
      ll loopgain = 0;
      do {
        loopgain += val;
        val = val * val % m;
        looplen++;
      } while (val != x);
      ans += (n - i - 1) / (looplen)*loopgain;
      ll len = (n - i - 1) % looplen;
      rep(j, len) {
        ans += x;
        x = x * x % m;
      }
      break;
    }
  }
  cout << (ans) << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e18 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, X, M;
  cin >> N >> X >> M;
  vector<int> used(100005);
  int old = X, j = 1;
  used[X] = true;
  while (true) {
    old = old * old % M;
    if (used[old]) {
      break;
    }
    used[old] = true;
    j++;
  }
  int sum = X;
  int now = X;
  old = X;
  for (int i = 2; i <= min(N, j); i++) {
    sum += old * old % M;
    old = old * old % M;
    now = old;
  }
  if (N <= j) {
    cout << sum << endl;
    return 0;
  }
  vector<int> s(100005);
  int cnt = 1;
  while (true) {
    old = old * old % M;
    s[cnt] = s[cnt - 1] + old;
    if (old == now) {
      break;
    }
    cnt++;
  }
  cout << sum + (N - min(N, j)) / cnt * s[cnt] + s[(N - min(N, j)) % cnt]
       << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e18 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, X, M;
  cin >> N >> X >> M;
  vector<int> used(100005);
  int old = X, j = 1;
  used[X] = true;
  while (true) {
    old = old * old % M;
    if (used[old]) {
      break;
    }
    used[old] = true;
    j++;
  }
  int sum = X;
  int now = X;
  old = X;
  for (int i = 2; i <= min(N, j); i++) {
    sum += old * old % M;
    old = old * old % M;
    now = old;
  }
  vector<int> s(100005);
  int cnt = 1;
  while (true) {
    old = old * old % M;
    s[cnt] = s[cnt - 1] + old;
    if (old == now) {
      break;
    }
    cnt++;
  }
  cout << sum + (N - min(N, j)) / cnt * s[cnt] + s[(N - min(N, j)) % cnt]
       << endl;
}