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

#include <bits/stdc++.h>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define mod 998244353
typedef long long ll;
#define NMAX 10000000001
int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1);
  vector<int> a;
  int len = 0;
  ll tot = 0;
  while (id[x] == -1) {
    a.push_back(x);
    id[x] = len;
    len++;
    tot += x;
    x = x * x % m;
  }

  int c = len - id[x];
  ll s = 0;
  for (int i = id[x]; i < len; i++)
    s += a[i];

  ll ans = 0;
  if (n <= len) {
    rep(i, n) ans += a[i];
  } else {
    ans += tot;
    n -= len;
    ans += s * (n / c);
    n %= c;
    rep(i, n) ans += a[id[x] + i];
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define mod 998244353
typedef long long ll;
#define NMAX 10000000001
int main() {
  ll n, x, m;
  vector<bool> memo(NMAX, false);
  cin >> n >> x >> m;
  ll ans = 0;
  ll a1 = 0;
  ll a2 = 0;

  a1 = x;
  memo[a1] = true;
  int p = -1;
  int l = -1;
  ans = a1;
  int length = 1;

  while (1) {
    a2 = a1 * a1 % m;
    if (memo[a2]) {
      p = a2;
      l = a1;
      break;
    }
    length++;
    memo[a2] = true;
    ans += a2;
    a1 = a2;
    if (n == length) {
      cout << ans << endl;
      return 0;
    }
  }

  ll cnt = 0;
  vector<int> value(0);
  a1 = p;
  value.push_back(a1);
  ll nowans = a1;
  while (1) {
    a2 = a1 * a1 % m;
    if (a2 == l)
      break;
    value.push_back(a2);
    nowans += a2;
  }

  ans += nowans * ((n - length) / value.size());
  for (int i = 0; i < (n - length) % value.size(); i++)
    ans += value[i];

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

// E - Sequence Sum
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[]) {
  long long n, x, m, c = 0, l = 0;
  cin >> n >> x >> m;
  long long ans = 0;
  map<long long, int> s;
  for (c = 0; c < n; c++) {
    if (s.count(x) > 0) {
      l = c - s[x];
      break;
    }
    ans += x;
    s[x] = c;
    x = (x * x) % m;
  }

  long long z = 0;
  for (long long t = x, i = 0; i < l; i++, t = (t * t) % m)
    z += t;

  if (l > 0) {
    ans += (n - c) / l * z;
    for (long long i = 0, t = x; i < (n - c) % l; i++, t = (t * t) % m)
      ans += t;
  }

  cout << ans << endl;

  return 0;
}


Error: -8

Fixed submission:

// E - Sequence Sum
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[]) {
  long long n, x, m, c = 0, l = 0;
  cin >> n >> x >> m;
  long long ans = 0;
  map<long long, int> s;
  for (c = 0; c < n; c++) {
    if (s.count(x) > 0) {
      l = c - s[x];
      break;
    }
    ans += x;
    s[x] = c;
    x = (x * x) % m;
  }

  long long z = 0;
  for (long long t = x, i = 0; i < l; i++, t = (t * t) % m)
    z += t;

  if (l > 0)
    ans += (n - c) / l * z;

  for (long long i = 0, t = x; i < (n - c) % l; i++, t = (t * t) % m)
    ans += t;

  cout << ans << endl;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<ll> v;
  ll f, g = -1;
  set<ll> st;
  int k = 0;
  for (ll y = x; k < min(m + 1, n); k++) {
    // cout << y << " ";
    if (st.find(y) != st.end()) {
      f = y;
      g = k;
      break;
    }

    st.insert(y);
    v.push_back(y);
    y = (y * y) % m;
  }
  // cout << endl << g << endl;

  if (g == -1) {
    ll ans = 0, y = x;
    for (int i = 0; i < n; i++) {
      ans += y;
      y = y * y % m;
    }
    cout << ans << endl;
    return 0;
  }

  int j;
  for (int i = 0; i < v.size(); i++) {
    if (f == v[i]) {
      j = i;
      break;
    }
  }
  // cout << j << endl;

  ll ans = 0, l = g - j;
  // cout << endl;
  for (ll i = 0; i < v.size(); i++) {
    if (i < j) {
      ans += v[i];
    } else {
      // cout << v[i] << " " << (n-1-i)/l + 1 << endl;
      // cout << v[i] * ((n-1-i)/l + 1) << endl;
      ans += v[i] * ((n - 1 - i) / l + 1);
    }
  }
  cout << ans << endl;
  return 0;
}

// 小数点精度
// cout << fixed << std::setprecision(15) << y << endl;

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<ll> v;
  ll f, g = -1;
  set<ll> st;
  int k = 0;
  for (ll y = x; y * y % m != x && k < n; k++) {
    // cout << y << " ";
    if (st.find(y) != st.end()) {
      f = y;
      g = k;
      break;
    }

    st.insert(y);
    v.push_back(y);
    y = y * y % m;
  }
  // cout << endl << g << endl;

  ll l = v.size();
  if (l == 0) {
    cout << x * n << endl;
    return 0;
  }

  if (g == -1) {
    ll y = x;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ans += y;
      y = y * y % m;
    }
    cout << ans << endl;
    return 0;
  }

  int j;
  for (int i = 0; i < v.size(); i++) {
    if (f == v[i]) {
      j = i;
      break;
    }
  }
  // cout << j << endl;

  ll ans = 0;
  l = g - j;
  // cout << endl;
  for (ll i = 0; i < v.size(); i++) {
    if (i < j) {
      ans += v[i];
    } else {
      // cout << v[i] << " " << (n-1-i)/l + 1 << endl;
      // cout << v[i] * ((n-1-i)/l + 1) << endl;
      ans += v[i] * ((n - 1 - i) / l + 1);
    }
  }
  cout << ans << endl;
  return 0;
}

// 小数点精度
// cout << fixed << std::setprecision(15) << y << endl;
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

int main() {
  ll n;
  int x, m;
  cin >> n >> x >> m;
  set<ll> st;
  vector<ll> a;
  a.push_back(x);
  st.insert(x);
  int nyan;
  ll sum = x;
  ll now = x;
  if (n < 101010) {
    rep(i, n - 1) {
      now *= now;
      now %= m;
      sum += now;
    }
    cout << sum << endl;
    return 0;
  }
  for (int i = 1; i < 101010; i++) {
    a.push_back(a[i - 1] * a[i - 1] % m);
    if (st.count(a[i])) {
      nyan = a[i];
      break;
    }
    if (a[i] == 0)
      break;
    st.insert(a[i]);
    sum += a[i];
  }
  if (a[a.size() - 1] == 0) {
    cout << sum << endl;
    return 0;
  }
  a.pop_back();
  int begin;
  rep(i, a.size()) {
    if (a[i] == nyan)
      begin = i;
  }
  int syuki = a.size() - begin;
  ll ans = 0;
  ll ikkai = 0;
  ll sai = 0;
  for (int i = 0; i < begin; i++) {
    sai += a[i];
  }
  for (int i = begin; i < a.size(); i++) {
    ikkai += a[i];
  }
  ans = sai + ((n - begin) / syuki) * ikkai;
  for (int i = begin; i < begin + (n - begin) % syuki; i++) {
    ans += a[i];
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

int main() {
  ll n;
  int x, m;
  cin >> n >> x >> m;
  set<ll> st;
  vector<ll> a;
  a.push_back(x);
  st.insert({x, 0});
  int nyan;
  ll sum = x;
  for (int i = 1; i < 101010; i++) {
    a.push_back(a[i - 1] * a[i - 1] % m);
    if (st.count(a[i])) {
      nyan = a[i];
      break;
    }
    if (a[i] == 0)
      break;
    st.insert(a[i]);
    sum += a[i];
  }
  if (a[a.size() - 1] == 0 || n < 101010) {
    cout << sum << endl;
  }
  a.pop_back();
  int begin;
  rep(i, a.size()) {
    if (a[i] == nyan)
      begin = i;
  }
  int syuki = a.size() - begin;
  ll ans = 0;
  ll ikkai = 0;
  ll sai = 0;
  for (int i = 0; i < begin; i++) {
    sai += a[i];
  }
  for (int i = begin; i < a.size(); i++) {
    ikkai += a[i];
  }
  ans = sai + ((n - begin) / syuki) * ikkai;
  for (int i = begin; i < begin + (n - begin) % syuki; i++) {
    ans += a[i];
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define PI 3.14159265358979
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#include <set>
int main(void) {
  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  vector<int> lasta(m + 1);
  vector<ll> lastans(m + 1);
  ll a = x;
  ans += a;
  bool f = true;
  for (ll i = 2; i <= n; i++) {

    a = (a * a) % m;
    if (lasta[a] == 0 || f == false) {
      lasta[a] = i;
      ans += a;
      lastans[a] = ans;

    }

    else {
      ans += a;
      ll loop = i - lasta[a];

      ll nloop = (n - i) / loop;
      ll dans = ans - lastans[a];

      ans += dans * nloop;
      i += nloop * loop;
      f = false;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define PI 3.14159265358979
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#include <set>
int main(void) {
  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  vector<int> lasta(m + 1);
  vector<ll> lastans(m + 1);
  ll a = x;
  ans += a;
  bool f = true;
  for (ll i = 2; i <= n; i++) {

    a = (a * a) % m;
    if (lasta[a] == 0 || f == false) {
      lasta[a] = i;
      lastans[a] = ans;
      ans += a;
    }

    else {
      int loop = i - lasta[a];

      int nloop = (n - i) / loop;
      ll dans = ans - lastans[a];

      ans += dans * nloop;
      i = nloop * loop;
      f = true;
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

long long n, x, m;
long long nextX[100010];
long long ans;

int main() {
  scanf("%lld%lld%lld", &n, &x, &m);

  for (int i = 0; i < m; i++) {
    nextX[i] = -1;
  }

  ans = x;
  long long nLeft = n - 1;
  long long newX;
  std::vector<long long> cycle;
  while (nLeft > 0) {
    nLeft--;
    newX = (x * x) % m;
    nextX[x] = newX;

    if (nextX[newX] != -1) {
      long long px = newX;
      cycle.push_back(px);
      while (px != x) {
        px = nextX[px];
        cycle.push_back(px);
      }

      break;
    }
    ans = ans + newX;
    x = newX;
  }

  if (nLeft >= 0) {
    if (cycle.size() != 0) {
      long long sz = cycle.size();
      //		printf("%d\\n", sz);

      long long sect = 0;
      for (int i = 0; i < cycle.size(); i++) {
        //			printf("%d ", cycle[i]);
        sect = sect + cycle[i];
      }
      //		printf("\\n");

      long long count = nLeft / sz;
      long long r = nLeft % sz;
      ans = ans + sect * count;
      for (int i = 0; i <= r; i++)
        ans = ans + cycle[i];
    }
  }

  printf("%lld\\n", ans);

  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

long long n, x, m;
long long nextX[100010];
long long ans;

int main() {
  scanf("%lld%lld%lld", &n, &x, &m);

  for (int i = 0; i < m; i++) {
    nextX[i] = -1;
  }

  ans = x;
  long long nLeft = n - 1;
  long long newX;
  std::vector<long long> cycle;
  while (nLeft > 0) {
    nLeft--;
    newX = (x * x) % m;
    nextX[x] = newX;

    if (nextX[newX] != -1) {
      long long px = newX;
      cycle.push_back(px);
      while (px != x) {
        px = nextX[px];
        cycle.push_back(px);
      }

      break;
    }
    ans = ans + newX;
    x = newX;
  }

  if (nLeft >= 0) {
    long long sz = cycle.size();
    //		printf("%d\\n", sz);

    long long sect = 0;
    for (int i = 0; i < cycle.size(); i++) {
      //			printf("%d ", cycle[i]);
      sect = sect + cycle[i];
    }
    //		printf("\\n");

    long long count = nLeft / sz;
    long long r = nLeft % sz;
    ans = ans + sect * count;
    for (int i = 0; i <= r; i++)
      ans = ans + cycle[i];
  }

  printf("%lld\\n", ans);

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


Error: 0

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

  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;
}

Error: TLE

Fixed submission:

#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;
}
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 < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<int>())
#define mp make_pair
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf = vector<vector<ll>>;
#define MAX 1000000007

int main() {
  ll n;
  ll x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  ll loop_st;
  int loop_before = 0;
  int loop_num = 0;
  int loop_after = 0;
  ll loop_vol = 0;
  vector<bool> visit(m + 1, false);
  if (n < m) {
    rep(i, n) {
      ans += x;
      x *= x;
      x %= m;
    }
    cout << ans;
    return 0;
  }
  rep(i, m) {
    if (visit[x] == false) {
      ans += x;
      visit[x] = true;
      x *= x;
      x %= m;
      loop_before++;
    } else if (x == 0) {
      cout << ans << endl;
      return 0;
    } else {
      loop_st = x;
      break;
    }
  }
  while (loop_num == 0 || x != loop_st) {
    loop_vol += x;
    x *= x;
    x %= m;
    loop_num++;
  }
  ans += loop_vol * ((n - loop_before) / loop_num);
  loop_after = (n - loop_before) % loop_num;
  rep(i, loop_after) {
    ans += x;
    x *= x;
    x %= m;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<int>())
#define mp make_pair
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf = vector<vector<ll>>;
#define MAX 1000000007

int main() {
  ll n;
  ll x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  vector<ll> visit(m + 1, false);
  ll count = 0;
  ll count_tmp = 0;
  bool flag = false;
  ll tmp = 0;
  rep(i, n) {
    if (x == 0) {
      flag = true;
      break;
    } else {
      if (visit[x] == false) {
        ans += x;
        visit[x] = true;
        count_tmp++;
        x = x * x % m;
      } else {
        tmp = x;
        break;
      }
    }
  }
  ll tmp_ans = 0;

  if (flag == false) {
    while (x != tmp || count == 0) {
      tmp_ans += x;
      x = x * x % m;
      count++;
    }
    ll num = (n - count_tmp) / count;
    ll times = (n - count_tmp) % count;
    ans += (tmp_ans * (num));
    rep(i, times) {
      ans += x;
      x = x * x % m;
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
#define sz(x) (int)(x.size())
#define all(v) v.begin(), v.end()
#define isOdd(n) (n & 1)
#define pow(n, m) (ll) powl(n, m)
#define clr(x, val) memset(x, val, sizeof(x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1e5 + 5;
unordered_map<int, int> mp;
ll pfx[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

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

  ll t1 = 0, t2 = n, s = 0;
  a1 = x;
  pfx[1] = a1;
  mp[a1] = 1;
  for (int i = 2; i <= n; i++) {
    ll r = (a1 * a1) % m;
    if (mp.count(r)) {
      t1 = mp[r];
      t2 = i;
      break;
    }
    mp[r] = i;
    a1 = r;
    pfx[i] = r;
  }
  for (int i = 0; i < t1; i++)
    s += pfx[i], pfx[i] = 0;
  for (int i = 1; i <= t2; i++)
    pfx[i] += pfx[i - 1];

  if (t1 == 0)
    return cout << pfx[n], 0;

  ll p = t2 - t1;
  ll pt = (n + 1 - t1) / p;
  ll sum = pt * (pfx[t2 - 1] - pfx[t1 - 1]);
  if ((n + 1 - t1) % p) {
    ll sp = (n - t1) % p;
    sum += pfx[t1 + sp] - pfx[t1 - 1];
  }
  cout << sum + s;
  return 0;
}


Error: -11

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
#define sz(x) (int)(x.size())
#define all(v) v.begin(), v.end()
#define isOdd(n) (n & 1)
#define pow(n, m) (ll) powl(n, m)
#define clr(x, val) memset(x, val, sizeof(x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1e5 + 5;
unordered_map<int, int> mp;
ll pfx[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  ll n, x, m, a1;
  cin >> n >> x >> m;
  ll t1 = 0, t2, s = 0;
  a1 = x;
  pfx[1] = a1;
  mp[a1] = 1;
  for (int i = 2; i <= n; i++) {
    ll r = (a1 * a1) % m;
    if (mp.count(r)) {
      t1 = mp[r];
      t2 = i;
      break;
    }
    mp[r] = i;
    a1 = r;
    pfx[i] = r;
  }

  for (int i = 0; i < t1; i++)
    s += pfx[i], pfx[i] = 0;
  for (int i = 1; i <= t2; i++)
    pfx[i] += pfx[i - 1];

  if (t1 == 0)
    return cout << pfx[n], 0;
  ll p = t2 - t1; // 7 - 3 = 4
  ll pt = (n + 1 - t1) / p;
  ll sum = pt * (pfx[t2 - 1] - pfx[t1 - 1]);
  if ((n + 1 - t1) % p) {
    ll sp = (n - t1) % p;
    sum += pfx[t1 + sp] - pfx[t1 - 1];
  }
  cout << sum + s;
  return 0;
}