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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VPI = vector<PII>;
using VPL = vector<PLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
#define REP(i, m, n) for (LL i = (m); i < (n); i++)
// i=m -> n-1(昇順)
#define RREP(i, m, n) for (int i = (m); i >= (n); i--)
// i=m -> n(降順)

int main() {
  const LL up_to = 300010;
  LL n, x, m;
  cin >> n >> x >> m;
  VLL checked(m, 0), a(min(n + 1, up_to), -1);
  a[1] = x;
  checked[x]++;
  bool start = false;
  LL span = 0, sum_span = 0, sum = x, mark;

  REP(i, 2, n + 1) {
    a[i] = (a[i - 1] * a[i - 1]) % m;
    checked[a[i]]++;
    sum += a[i];
    if (checked[a[i]] == 2) {
      start = true;
    }
    if (checked[a[i]] == 3) {
      mark = i - 1;
      sum -= a[i];
      break;
    }
    if (start) {
      span++;
      sum_span += a[i];
    }
  }

  if (a[a.size() - 1] >= 0) {
    cout << sum << endl;
    return 0;
  }

  sum += ((n - mark) / span) * sum_span;
  LL rest = (n - mark) % span;
  LL cnt = mark - (2 * span) + 1;
  REP(i, 0, rest) { sum += a[cnt + i]; }
  cout << sum << endl;

  return 0;
}


Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VPI = vector<PII>;
using VPL = vector<PLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
#define REP(i, m, n) for (LL i = (m); i < (n); i++)
// i=m -> n-1(昇順)
#define RREP(i, m, n) for (int i = (m); i >= (n); i--)
// i=m -> n(降順)

int main() {
  LL n, x, m;
  cin >> n >> x >> m;
  VLL checked(m, 0), a(min(n + 10, 3 * m), -1);
  a[1] = x;
  checked[x]++;
  bool start = false;
  LL span = 0, sum_span = 0, sum = x, mark;

  REP(i, 2, n + 1) {
    a[i] = (a[i - 1] * a[i - 1]) % m;
    checked[a[i]]++;
    sum += a[i];
    if (checked[a[i]] == 2) {
      start = true;
    }
    if (checked[a[i]] == 3) {
      mark = i - 1;
      sum -= a[i];
      break;
    }
    if (start) {
      span++;
      sum_span += a[i];
    }
  }

  if (n < 3 * m && a[n] >= 0) {
    cout << sum << endl;
    return 0;
  }

  sum += ((n - mark) / span) * sum_span;
  LL rest = (n - mark) % span;
  LL cnt = mark - (2 * span) + 1;
  REP(i, 0, rest) { sum += a[cnt + i]; }
  cout << sum << 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;
  cin >> N >> X >> M;

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

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

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

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

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

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

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

Error: 0

Fixed submission:

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

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

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

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

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

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

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

  if (N < A) {
    for (int i = 0; i < N; i++) {
      ans += L.at(i);
    }
  } else {
    long long C = (N - B + 1) / (A - B);
    long long D = (N - B + 1) % (A - B);
    for (int i = B - 1; i < B + D - 1; i++) {
      ans += L.at(i);
    }
    ans += sum1 + C * sum2;
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1); // iが何回目に登場したか
  vector<int> list;

  // 周期を得るまで回す
  ll x1 = x;
  int len = 0;
  ll total = 0;
  while (id[x1] == -1) {
    list.push_back(x1);
    id[x1] = len;
    len++;
    total += x1;
    x1 = x1 * x1 % m;
  }
  int c = len - id[x1]; // 周期のノード数
  ll c_total = 0;       // 周期の合計点数
  for (int i = id[x1]; i < len; i++)
    c_total += list[i];

  ll ans = 0;
  ll x2 = x;
  if (n <= len) {
    for (int i = 1; i <= n; i++) {
      ans += x2;
      x2 = x2 * x2 % m;
    }
  } else {
    // 周期を使える時
    ans += total;
    n -= len; // 残り回数
    ans += (n / c) * c_total;
    n %= c;
    for (int i = 0; i < n; i++)
      ans += list[id[x1] + i];
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// 素因数分解

long long N, X, M;
int main() {
  cin >> N >> X >> M;
  long long ans = X;
  long long before = X;
  for (int i = 2; i <= N; i++) {
    before = (before * before) % M;
    ans += before;
  }
  cout << ans;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#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: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL multi(LL a, LL b, LL m) {

  // a * b % m

  LL ret = 0;

  while (b > 0) {

    if (b & 1) {

      ret = (ret + a) % m;
    }

    b >>= 1;

    a = (a << 1) % m;
  }

  return ret;
}

LL quick_mod(LL a, LL b, LL m) {

  LL ans = 1;

  while (b) {

    if (b & 1) {

      ans = multi(ans, a, m);

      b--; // 可要可不要;
    }

    b /= 2;

    a = multi(a, a, m);
  }
  // 可以视情况而定是否再取一次模
  return ans;
}
int main() {
  LL n, x, m;
  cin >> n >> x >> m;
  LL x2 = 0;
  LL ans = 0;
  for (LL i = 0; i < n; i++) {
    ans += x;
    x = quick_mod(x, 2, m);
    if (x == 0)
      break;
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

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

  // 出てきた余りを記録
  vector<int> mod;
  // 繰り返しの最初のインデックスを保持
  vector<int> idx(m, -1);
  long long a = x;
  int count = 0;

  while (idx[a] == -1) {
    idx[a] = count;
    mod.push_back(a);
    count++;
    if (count == n)
      break;
    x = a;
    a = (x * x) % m;
  }

  long long ans = 0;
  // 繰り返されない部分の和
  for (int i = 0; i <= idx[a] - 1; i++) {
    ans += mod[i];
  }
  // 繰り返す部分
  // countはmodの長さ
  long long repeated = 0;
  for (int i = idx[a]; i < count; i++) {
    repeated += mod[i];
  }
  ans += repeated * ((n - idx[a]) / (count - idx[a]));
  if ((n - idx[a]) / (count - idx[a])) {
    for (int i = 0; i < (n - idx[a]) % (count - idx[a]); i++) {
      ans += mod[idx[a] + i];
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

  // 余りの累積和の数列
  vector<long long> cuml;
  cuml.push_back(0);
  cuml.push_back(x);
  // 数列の何番目に登場したか
  vector<int> mod(m, 0);
  mod[x] = 1;
  // Anを保持
  long long pre = x;
  int a;
  for (int i = 0; i < m; i++) {
    if (i == n - 1)
      break;
    a = (pre * pre) % m;
    // 同じものが2回出たら終わり
    if (mod[a])
      break;
    pre = a;
    mod[a] = cuml.size();
    cuml.push_back(cuml[cuml.size() - 1] + a);
  }

  long long ans;
  if (mod[a] == cuml.size() - 1) {
    ans = cuml[cuml.size() - 1];
  } else {
    long long repeated_sum = cuml[cuml.size() - 1] - cuml[mod[a] - 1];
    // 1周期の長さ
    int length = cuml.size() - mod[a];
    ans = cuml[mod[a] - 1] + repeated_sum * ((n - mod[a] + 1) / length);
    if ((n - mod[a] + 1) / length != 0)
      ans += cuml[mod[a] - 1 + (n - mod[a] + 1) % length] - cuml[mod[a] - 1];
  }

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

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

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

  int s = 0;
  ULL ans = 0;
  int I[100000];
  rep(i, M) I[i] = -1;
  {
    int i = 1;
    while (N != 0) {
      ans += X;
      I[X] = i;
      N--;
      X = X * X % M;
      i++;
      if (I[X] != -1) {
        s = i - I[X];
        break;
      }
    }
  }

  if (N != 0) {
    vector<ULL> V(s + 1);
    rep(i, s) {
      V[i + 1] = V[i] + X;
      X = X * X % M;
    }
    ans += (N / s) * V[s];
    N %= s;

    ans += V[N];
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ULL N, X, M;
  cin >> N >> X >> M;
  ULL nx[100000];
  rep(i, M) nx[i] = i * i % M;

  int s;
  ULL ans = 0;
  int I[100000];
  rep(i, M) I[i] = -1;
  {
    int i = 1;
    while (N != 0) {
      ans += X;
      I[X] = i;
      N--;
      X = X * X % M;
      i++;
      if (I[X] != -1) {
        s = i - I[X];
        break;
      }
    }
  }

  vector<ULL> V(s + 1);
  rep(i, s) {
    V[i + 1] = V[i] + X;
    X = X * X % M;
  }
  ans += (N / s) * V[s];
  N %= s;

  ans += V[N];

  cout << ans << endl;

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

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

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

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

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

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

Error: 0

Fixed submission:

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

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

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

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

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

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

template <typename T> void print_vec(const vector<T> &v) {
  int n = v.size();
  for (int i = 0; i < n; ++i) {
    if (i > 0)
      cout << ' ';
    cout << v[i];
  }
  cout << '\\n';
}

int main() {

  ll n, x, m;
  cin >> n >> x >> m;
  ll a = x;
  ll r = a;
  if (n < m) {
    for (ll i = 2; i <= n; ++i) {
      a = a * a % m;
      r += a;
    }
    cout << r << '\\n';
    return 0;
  }

  vector<ll> exist(m + 2, false);
  vector<ll> mod;
  mod.push_back(x);
  exist[x] = true;
  ll loop_val = -1;
  for (ll i = 2; i <= m + 1; ++i) {
    a = a * a % m;
    if (!exist[a]) {
      exist[a] = true;
      mod.push_back(a);
    } else {
      loop_val = a;
      break;
    }
  }
  ll loop_id = find(mod.begin(), mod.end(), loop_val) - mod.begin();
  ll sz = mod.size() - loop_id;
  ll loop_sum = accumulate(mod.begin() + loop_id, mod.end(), 0ll);
  ll ans = accumulate(mod.begin(), mod.begin() + loop_id, 0ll);
  ll re = n - loop_id;
  ans += re / sz * loop_sum + accumulate(mod.begin() + loop_id,
                                         mod.begin() + loop_id + re % sz, 0ll);
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

template <typename T> void print_vec(const vector<T> &v) {
  int n = v.size();
  for (int i = 0; i < n; ++i) {
    if (i > 0)
      cout << ' ';
    cout << v[i];
  }
  cout << '\\n';
}

int main() {

  ll n, x, m;
  cin >> n >> x >> m;
  ll a = x;
  ll r = a;
  if (n < m) {
    for (ll i = 2; i <= n; ++i) {
      a = a * a % m;
      r += a;
    }
    cout << r << '\\n';
    return 0;
  }

  vector<ll> exist(m, false);
  vector<ll> mod;
  mod.push_back(x);
  mod[x] = true;
  ll loop_val = -1;
  for (ll i = 2; i <= m + 1; ++i) {
    a = a * a % m;
    if (!exist[a]) {
      exist[a] = true;
      mod.push_back(a);
    } else {
      loop_val = a;
      break;
    }
  }
  ll loop_id = find(mod.begin(), mod.end(), loop_val) - mod.begin();
  ll sz = mod.size() - loop_id;
  ll loop_sum = accumulate(mod.begin() + loop_id, mod.end(), 0ll);
  ll ans = accumulate(mod.begin(), mod.begin() + loop_id, 0ll);
  ll re = n - loop_id;
  ans += re / sz * loop_sum + accumulate(mod.begin() + loop_id,
                                         mod.begin() + loop_id + re % sz, 0ll);
  cout << ans << '\\n';
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rept(i, k, n) for (long long i = (k); i < (long long)(n); i++)
#define All(v) v.begin(), v.end()
#define Sort(v) sort(v.begin(), v.end())
#define Rsort(v) sort(v.begin(), v.end(), [](auto a, auto b) { return a > b; })
#define Reverse(v) reverse(v.begin(), v.end())
#define MAX 1000000007
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<long long, long long> Pll;
typedef tuple<int, int, int> Tii;

template <typename T> ostream &operator<<(ostream &s, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i)
      s << ',';
    else
      s << '{';
    s << setw(2) << vec[i];
  }
  s << '}' << endl;
  return s;
}

int main() {
  ll n, x, m, s = 0;
  cin >> n >> x >> m;
  vector<ll> sum(m, 0);
  map<ll, ll> mp;
  rep(i, n) {
    sum[i + 1] = sum[i] + x;
    if (mp.count(x)) {
      ll k = mp[x];
      ll c = i - k;
      ll h = (n - k) % c;
      ll ans = sum[k] + (sum[i] - sum[k]) * ((n - k) / c) + sum[k + h] - sum[k];
      cout << ans << endl;
      return 0;
    }
    mp[x] = i;

    x = x * x % m;
  }
  cout << sum[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rept(i, k, n) for (long long i = (k); i < (long long)(n); i++)
#define All(v) v.begin(), v.end()
#define Sort(v) sort(v.begin(), v.end())
#define Rsort(v) sort(v.begin(), v.end(), [](auto a, auto b) { return a > b; })
#define Reverse(v) reverse(v.begin(), v.end())
#define MAX 1000000007
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<long long, long long> Pll;
typedef tuple<int, int, int> Tii;

template <typename T> ostream &operator<<(ostream &s, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i)
      s << ',';
    else
      s << '{';
    s << setw(2) << vec[i];
  }
  s << '}' << endl;
  return s;
}

int main() {
  ll n, x, m, s = 0;
  cin >> n >> x >> m;
  rep(i, n) {
    s += x;
    x = (x * x) % m;
  }
  cout << s << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll N, X, M;
  cin >> N >> X >> M;
  vector<ll> his;
  vector<bool> is(100000 + 10, false);

  ll res{0};
  ll m{-1};
  REP(i, N) {
    if (is[X]) {
      m = X;
      break;
    } else
      is[X] = true;
    res += X;
    his.push_back(X);
    (X *= X) %= M;
  }

  if (m == -1) {
    std::cout << res << std::endl;
    return 0;
  }

  vector<ll> hiss;
  ll it = find(ALL(his), m) - his.begin();
  for (ll i = it; i < his.size(); ++i) {
    hiss.push_back(his[i]);
    DEBUG(his[i]);
  }

  ll rem = N - it;
  DEBUG(rem);
  ll sum = accumulate(ALL(hiss), 0ll);
  DEBUG(sum);
  ll cs = hiss.size();
  DEBUG(cs);
  res += sum * (rem / cs - 1);
  res += accumulate(hiss.begin(), hiss.begin() + (rem % cs), 0ll);

  std::cout << res << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll N, X, M;
  cin >> N >> X >> M;
  ll XX = X;

  ll res{0};
  REP(i, N) {
    if (X == 0 || X == 1) {
      res += X * (N - i);
      break;
    }
    res += X;
    (X *= X) %= M;
  }

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

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, X, M;
  cin >> N >> X >> M;

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

  ll logk{0};
  while ((1ll << logk) < N)
    ++logk;

  vector<vector<ll>> ding(logk, vector<ll>(M));
  vector<vector<ll>> ding2(logk, vector<ll>(M));
  REP(i, M)
  ding[0][i] = (i * i) % M;
  REP(k, logk - 1) {
    REP(i, M) { ding[k + 1][i] = ding[k][ding[k][i]]; }
  }

  REP(i, M)
  ding2[0][i] = i;
  REP(k, logk - 1) {
    REP(i, M) { ding2[k + 1][i] = ding2[k][ding[k][i]] + ding2[k][i]; }
  }

  ll now{X};
  ll res{0};
  --N;
  for (ll bit = 0;; bit++) {
    if (N & 1) {
      res += ding2[bit][now];
      now = ding[bit][now];
    }
    N = N >> 1;
    if (N == 0) {
      res += now;
      break;
    }
  }

  std::cout << res << std::endl;
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, X, M;
  cin >> N >> X >> M;
  ll logk{0};
  while ((1ll << logk) < N)
    ++logk;

  vector<vector<ll>> ding(logk, vector<ll>(M));
  vector<vector<ll>> ding2(logk, vector<ll>(M));
  REP(i, M)
  ding[0][i] = (i * i) % M;
  REP(k, logk - 1) {
    REP(i, M) { ding[k + 1][i] = ding[k][ding[k][i]]; }
  }

  REP(i, M)
  ding2[0][i] = i;
  REP(k, logk - 1) {
    REP(i, M) { ding2[k + 1][i] = ding2[k][ding[k][i]] + ding2[k][i]; }
  }

  ll now{X};
  ll res{0};
  --N;
  for (ll bit = 0;; bit++) {
    if (N & 1) {
      res += ding2[bit][now];
      now = ding[bit][now];
    }
    N = N >> 1;
    if (N == 0) {
      res += now;
      break;
    }
  }

  std::cout << res << std::endl;
  return 0;
}