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


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

#define f first
#define sz(a) ((int)(a).size())
#define s second
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define vpii vector<pii>
#define eb emplace_back
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)
#define vi vector<int>

using namespace std;
using ll = long long;
using lld = long double;

const int oo = 0x3f3f3f3f;
const ll MOD = 1000000007;

const int N = 1e5 + 10;
int hit[N + 2];

signed main() {

#ifndef ONLINE_JUDGE
  // freopen("in.txt" , "r" , stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, x, m;
  cin >> n >> x >> m;
  int f = x;
  hit[f] = 1;
  vi before_cycle, cycle;
  int ss = f;
  for (int i = 1; i < n; i++) {
    f = (f * f) % m;
    if (hit[f] == 0)
      ss += f;
    if (f == 0) {
      cout << ss << '\\n';
      return 0;
    }
    if (hit[f] == 3)
      break;
    hit[f]++;
    if (hit[f] == 2)
      cycle.pb(f);
  }

  int ans = 0;
  // return 0;
  for (int i = 0; i < m; i++)
    if (hit[i] == 1)
      ans += i, before_cycle.pb(i);

  int sum = 0;
  for (int i : cycle)
    sum += i;

  int numOfHits = cycle.size();
  n -= before_cycle.size();
  n = max(n, 0LL);
  if (numOfHits)
    ans += sum * (n / numOfHits), n %= numOfHits;

  for (int i = 0; i < n; i++)
    ans += (long long)cycle[i];

  cout << ans << '\\n';

  return 0;
}


Error: -8

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define int ll

#define f first
#define sz(a) ((int)(a).size())
#define s second
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define vpii vector<pii>
#define eb emplace_back
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)
#define vi vector<int>

using namespace std;
using ll = long long;
using lld = long double;

const int oo = 0x3f3f3f3f;
const ll MOD = 1000000007;

const int N = 1e5 + 10;
int hit[N + 2];

signed main() {

#ifndef ONLINE_JUDGE
  // freopen("in.txt" , "r" , stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, x, m;
  cin >> n >> x >> m;
  int f = x;
  hit[f] = 1;
  vi before_cycle, cycle;
  int ss = f;
  for (int i = 1; i < n; i++) {
    f = (f * f) % m;
    ss += f;
    if (f == 0) {
      cout << ss << '\\n';
      return 0;
    }
    if (hit[f] == 3)
      break;
    hit[f]++;
    if (hit[f] == 2)
      cycle.pb(f);
  }

  for (int i = 0; i < m; i++)
    if (hit[i] == 1)
      before_cycle.pb(i);

  int ans = 0;
  int sum = 0;
  for (int i : cycle)
    sum += i;

  int numOfHits = cycle.size();
  n -= before_cycle.size();
  ans += sum * (n / numOfHits);
  n %= numOfHits;

  for (int i = 0; i < n; i++)
    ans += (long long)cycle[i];

  cout << ans << '\\n';

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

#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
int main() {
  ll n, x, m, ans = 0, flag = -1, pan = 1, t = 0, tt = 0, k = 150000;
  cin >> n >> x >> m;
  ll xx = x;
  for (ll i = 0; i < n; i++) {
    int p = 1;
    ans += x;
    x = x * x;
    if (k > 1)
      k--;
    if (pan == 1 && flag == -1 && k == 1)
      pan = 0, flag = x % m, p = 0;
    x %= m;
    if (p && !pan && x == flag) {
      ll ttt = ((n - 1 - i) / tt);
      ans += ttt * t;
      i += ttt * tt;
      pan = 2;
    } else if (!pan) {
      t += x, tt++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
int main() {
  ll n, x, m, ans = 0, flag = -1, pan = 1, t = 0, tt = 0;
  cin >> n >> x >> m;
  for (ll i = 0; i < n; i++) {
    int p = 1;
    ans += x;
    x = x * x;
    if (x == 1) {
      ans += n - i - 1;
      break;
    }
    if (pan == 1 && x >= m && flag == -1)
      pan = 0, flag = x % m, p = 0;
    x %= m;
    if (p && !pan && x == flag) {
      ll ttt = ((n - 1 - i) / tt);
      ans += ttt * t;
      i += ttt * tt;
      pan = 2;
    } else if (!pan) {
      t += x, tt++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)
#define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \\
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \\
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd_(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd_(b, a % b);
}
ll lcm_(ll a, ll b) {
  ll c = gcd_(a, b);
  return ((a / c) * b);
}
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = ll(1e16) + ll(7);
const ll MOD = 1000000007LL;
#define out(a) cout << fixed << setprecision((a))
// tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a) {
  ll r = 0;
  while (a) {
    a /= 10;
    r++;
  }
  return r;
}

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

  // 今iにいて2^j個先に行ったとき

  ll pla[M][35] = {}, bal[M][35] = {};

  FOR(i, 0, M) {
    pla[i][0] = i * i % M;
    bal[i][0] = i * i % M;
  }
  FOR(j, 1, 35) FOR(i, 0, M) {
    pla[i][j] = pla[pla[i][j - 1]][j - 1];
    bal[i][j] = bal[i][j - 1] + bal[pla[i][j - 1]][j - 1];
  }

  ll ans = X;
  N--;
  FOR(i, 0, 35) {
    if (N & (1LL << i)) {
      ans += bal[X][i];
      X = pla[X][i];
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)
#define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \\
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \\
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd_(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd_(b, a % b);
}
ll lcm_(ll a, ll b) {
  ll c = gcd_(a, b);
  return ((a / c) * b);
}
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = ll(1e16) + ll(7);
const ll MOD = 1000000007LL;
#define out(a) cout << fixed << setprecision((a))
// tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a) {
  ll r = 0;
  while (a) {
    a /= 10;
    r++;
  }
  return r;
}

int main() {

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

  ll ans = 0;
  FOR(i, 0, N) {
    ans += X;
    X = X * X % M;
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll sum[2 * N];
ll n, x, mod;
int main() {
  cin >> n >> x >> mod;
  map<int, int> m;
  ll s = -1, e, i = 1;
  while (1) {
    sum[i] = x + sum[i - 1];
    if (m[x] == 0)
      m[x]++;
    else if (m[x] == 1) {
      if (s == -1)
        s = i;
      m[x]++;
    } else {
      e = i - 1;
      break;
    }
    x = ((x % mod) * (x % mod)) % mod;
    i++;
    // if(x<0) cout<<x<<endl;
  }

  ll k = (e - s + 1);
  if (s > k)
    s -= k, e -= k;
  // cout<<s<<" "<<e<<endl;
  ll ans = 0;
  ll res = sum[e] - sum[s - 1];
  ans = sum[s - 1] + res * ((n - (s - 1)) / k) +
        (sum[s + (n - (s - 1)) % k - 1] - sum[s - 1]);
  // else ans=sum[s-1];
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll sum[2 * N];
ll n, x, mod;
int main() {
  cin >> n >> x >> mod;
  map<int, int> m;
  ll s = -1, e;
  for (ll i = 1; i <= 2 * mod; i++) {
    sum[i] = x + sum[i - 1];
    if (m[x] == 0)
      m[x]++;
    else if (m[x] == 1) {
      if (s == -1)
        s = i;
      m[x]++;
    } else {
      e = i - 1;
      break;
    }
    x = ((x % mod) * (x % mod)) % mod;
    // if(x<0) cout<<x<<endl;
  }

  ll k = (e - s + 1);
  if (s > k)
    s -= k, e -= k;
  // cout<<s<<" "<<e<<endl;
  ll ans = 0;
  ll res = sum[e] - sum[s - 1];
  ans = sum[s - 1] + res * ((n - (s - 1)) / k) +
        (sum[s + (n - (s - 1)) % k - 1] - sum[s - 1]);
  // else ans=sum[s-1];
  cout << ans;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
#define print(x) std::cout << (x) << std::endl
using LL = long long;

int main() {
  // freopen("in","r",stdin);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  LL n, r = 0;
  int x, m;
  std::cin >> n >> x >> m;
  std::vector<LL> s;
  std::vector<int> mp(m, -1);
  int d, id = 0, now = x;
  while (1) {
    if (mp[now] != -1) {
      d = id - mp[now];
      id = mp[now];
      break;
    }
    mp[now] = id;
    s.emplace_back(now);
    now = LL(now) * now % m;
    ++id;
  }
  if (id >= n) {
    for (int i = 0; i < n; ++i)
      r += s[i];
    std::cout << r << std::endl;
    return 0;
  }
  for (int i = 0; i < id; ++i)
    r += s[i];
  n -= id;
  LL sd = 0;
  for (int i = 0; i < d; ++i)
    sd += s[id + i];
  r += (n / d) * sd;
  n %= d;
  for (int i = 0; i < n; ++i)
    r += s[id + i];
  std::cout << r << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
#define print(x) std::cout << (x) << std::endl
using LL = long long;

int main() {
  // freopen("in","r",stdin);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  LL n, r = 0;
  int x, m;
  std::cin >> n >> x >> m;
  std::vector<LL> s;
  std::vector<int> mp(m, -1);
  int d = 0, id = 0, now = x % m;
  while (1) {
    if (mp[now] != -1) {
      d = id - mp[now];
      id = mp[now];
      break;
    }
    mp[now] = id;
    s.emplace_back(now);
    now = LL(now) * now % m;
    ++id;
  }
  if (id >= n) {
    for (int i = 0; i < n; ++i)
      r += s[i];
    std::cout << r << std::endl;
    return 0;
  }
  for (int i = 0; i < id; ++i)
    r += s[i];
  n -= id;
  LL sd = 0;
  for (int i = 0; i < d; ++i)
    sd += s[id + i];
  r += (n / d) * sd;
  n %= id;
  for (int i = 0; i < n; ++i)
    r += s[id + i];
  std::cout << r << std::endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

ll f(ll n, ll m) { return (n * n) % m; }

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  ll A, preA, ans = 0;
  ll loopcnt = 0, cnt = 0, sum_loop = 0, rec_A;
  vector<int> visit(1000000);
  bool dub = false;
  for (int i = 1; i <= N; i++) {
    if (i == 1) {
      A = X;
      ans += A;
      visit[A] = 1;
      cnt++;
      continue;
    }
    preA = A;
    A = f(A, M);
    if (dub) {
      if (A == rec_A)
        break;
      ans += A;
      sum_loop += A;
      loopcnt++;
      cnt++;
    } else {
      if (visit[A]) {
        dub = true;
        rec_A = A;
        sum_loop += A;
        loopcnt++;
      }
      ans += A;
      cnt++;
      visit[A] = 1;
    }
  }
  A = preA;
  if (dub) {
    ans += sum_loop * ((N - cnt) / loopcnt);
    ll rem = (N - cnt) % loopcnt;
    for (int i = 0; i < rem; i++) {
      A = f(A, M);
      ans += A;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

ll f(ll n, ll m) { return (n * n) % m; }

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  ll A, preA, ans = 0;
  ll loopcnt = 0, cnt = 0, sum_loop = 0, rec_A;
  vector<bool> visit(100001);
  bool dub;
  for (int i = 1; i <= N; i++) {
    if (i == 1) {
      A = X;
      ans += A;
      visit[A] = true;
      cnt++;
      continue;
    }
    preA = A;
    A = f(A, M);
    if (dub) {
      if (A == rec_A)
        break;
      ans += A;
      sum_loop += A;
      loopcnt++;
      cnt++;
    } else {
      if (visit[A]) {
        dub = true;
        rec_A = A;
        sum_loop += A;
        loopcnt++;
      }
      ans += A;
      cnt++;
      visit[A] = true;
    }
  }
  A = preA;
  if (dub) {
    ans += sum_loop * ((N - cnt) / loopcnt);
    ll rem = (N - cnt) % loopcnt;
    for (int i = 0; i < rem; i++) {
      A = f(A, M);
      ans += A;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/sort.hpp>

using namespace std;
using boost::sort;
using boost::adaptors::reversed;

int main() {
  long n, x, m;
  cin >> n >> x >> m;
  vector<long> a, s;
  unordered_map<long, long> ls;
  a.push_back(-1);
  a.push_back(x);
  s.push_back(0);
  s.push_back(x);
  ls[x] = 1;
  long l = 1, r = n;
  for (long i = 1; i < n; i++) {
    long an1 = a[i] * a[i] % m;
    a.push_back(an1);
    s.push_back(an1 + s[i]);
    if (ls[an1] > 0) {
      l = ls[an1];
      r = i + 1;
      break;
    } else {
      ls[an1] = i + 1;
    }
  }
  long ans = 0;
  n -= l;
  ans += s[l];
  if (l < r) {
    ans += (s[r] - s[l]) * (n / (r - l));
    ans += s[l + n % (r - l)] - s[l];
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/sort.hpp>

using namespace std;
using boost::sort;
using boost::adaptors::reversed;

int main() {
  long n, x, m;
  cin >> n >> x >> m;
  vector<long> a, s;
  unordered_map<long, long> ls;
  a.push_back(-1);
  a.push_back(x);
  s.push_back(0);
  s.push_back(x);
  ls[x] = 1;
  long l = 1, r = n;
  for (long i = 1; i < n; i++) {
    long an1 = a[i] * a[i] % m;
    a.push_back(an1);
    s.push_back(an1 + s[i]);
    if (ls[an1] > 0) {
      l = ls[an1];
      r = i + 1;
      break;
    } else {
      ls[an1] = i + 1;
    }
  }

  long ans = 0;
  n -= l;
  ans += s[l];
  ans += (s[r] - s[l]) * (n / (r - l));
  ans += s[l + n % (r - l)] - s[l];
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i < (n); i++)
typedef long long ll;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll u = 100100;

int main() {
  ll n, x, m;
  ll ans = 0;
  cin >> n >> x >> m;
  vector<ll> a(u, 0);
  a[1] = x;
  ans += a[1];
  rep2(i, u) {

    a[i + 1] = a[i] * a[i] % m;
    // if(i<=5) cout << a[i] << endl;
    if (i < u - 1 || i == n - 1)
      ans += a[i + 1];
    if (i == n - 1) {
      cout << ans << endl;
      return 0;
    }
  }

  // uから下に向かって一致するものを探す。
  for (ll i = u - 1; i > 0; i--) {
    if (a[i] == a[u]) {
      // どこかで必ず一致するから、このカッコ内で全ての処理を行う。
      ll loopSet = 0;
      for (ll j = i; j < u; j++) {
        loopSet += a[j];
      }
      // ループの輪の長さ
      ll loopcnt = u - i;

      // u〜nでのループの回数（セットの個数）及び、余りを求める。
      ll SetNum = (n - u + 1) / loopcnt;
      ll amari = (n - u + 1) % loopcnt;
      // cout << "now ans:" << ans << endl;

      ans += SetNum * loopSet;
      // cout << "now 2 ans: " << ans << endl;
      // cout << SetNum * loopSet << endl;
      for (ll j = i; j < i + amari; j++) {
        ans += a[j];
      }
      cout << ans << endl;
      // cout << "loopcnt = " << loopcnt << endl << "loopSet = " << loopSet <<
      // endl << "SetNum = " << SetNum << endl;
      return 0;
    }
  }
}

// 632660880097787
// 492443256176507

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i < (n); i++)
typedef long long ll;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll u = 200100;

int main() {
  ll n, x, m;
  ll ans = 0;
  cin >> n >> x >> m;
  vector<ll> a(u, 0);
  a[1] = x;
  ans += a[1];
  rep2(i, u) {

    a[i + 1] = a[i] * a[i] % m;
    // if(i<=5) cout << a[i] << endl;
    if (i < u - 1 || i == n - 1)
      ans += a[i + 1];
    if (i == n - 1) {
      cout << ans << endl;
      return 0;
    }
  }

  for (ll i = u - 1; i > 0; i--) {
    if (a[i] == a[u]) {
      ll loopSet = 0;
      for (ll j = i; j < u; j++) {
        loopSet += a[j];
      }
      ll loopcnt = u - i;

      ll SetNum = (n - u + 1) / loopcnt;
      ll amari = (n - u + 1) % loopcnt;

      ans += SetNum * loopSet;
      for (ll j = u; j < u + amari; j++) {
        ans += a[j];
      }
      cout << ans << endl;
      return 0;
    }
  }
}

// 632660880097787
// 492443256176507
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<ll> roop(m, 0);
  ll a = x;
  ll cnt = 1;
  ll ans = x;
  bool lim = false;
  for (ll i = 0; i < m + 10; i++) {
    a = (a * a) % m;
    cnt++;
    ans += a;
    if (cnt == n) {
      lim = true;
      break;
    }
    bool ok = false;
    roop[a]++;
    if (roop[a] >= 2)
      ok = true;
    if (ok)
      break;
  }
  if (lim) {
    cout << ans << endl;
    return 0;
  }
  n -= cnt;
  ans -= a;
  n++;
  vector<ll> num;
  bool judge = true;
  ll b = a;
  num.push_back(a);
  ll sum = a;
  while (judge) {
    b = (b * b) % m;
    if (b == a)
      break;
    num.push_back(b);
    sum += b;
  }
  ll siz = num.size();
  ll aa = n / siz;
  ans += sum * aa;
  n -= aa * siz;
  for (ll i = 0; i < n; i++) {
    ans += num[i];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<ll> roop(m, 0);
  ll a = x;
  ll cnt = 1;
  ll ans = x;
  bool lim = false;
  for (ll i = 0; i < n; i++) {
    a = (a * a) % m;
    cnt++;
    ans += a;
    if (cnt == n) {
      lim = true;
      break;
    }
    bool ok = false;
    roop[a]++;
    for (ll j = 0; j < roop.size(); j++) {
      if (roop[j] >= 2) {
        ok = true;
      }
    }
    if (ok)
      break;
  }
  if (lim) {
    cout << ans << endl;
    return 0;
  }
  n -= cnt;
  ans -= a;
  n++;
  vector<ll> num;
  bool judge = true;
  ll b = a;
  num.push_back(a);
  ll sum = a;
  while (judge) {
    b = (b * b) % m;
    if (b == a)
      break;
    num.push_back(b);
    sum += b;
  }
  ll siz = num.size();
  ll aa = n / siz;
  ans += sum * aa;
  n -= aa * siz;
  for (ll i = 0; i < n; i++) {
    ans += num[i];
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, x, m;
  cin >> n >> x >> m;
  vector<vector<ll>> sum(40, vector<ll>(m));
  vector<vector<ll>> doubling(40, vector<ll>(m));

  for (ll i = 0; i < m; i++) {
    sum[0][i] = i;
    doubling[0][i] = i * i % m;
  }

  for (int i = 0; i < 40 - 1; i++) {
    for (int j = 0; j < m; j++) {
      doubling[i + 1][j] = doubling[i][doubling[i][j]];
      sum[i + 1][j] = sum[i][j] + sum[i][doubling[i][j]];
    }
  }

  ll now = x, ans = 0;
  for (ll i = 40 - 1; i >= 0; i--) {
    if (n >> i & 1) {
      ans += sum[i][now];
      now = doubling[i][now];
    }
  }

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const long long INF = 1e15;

struct Fenwick {
  long long N;
  vector<long long> seg;
  Fenwick(long long N) : N(N) {
    seg.resize(N + 1);
    fill_n(begin(seg), N + 1, 0);
  }

  void add(long long i, long long x) {
    i++;
    while (i <= N) {
      seg[i] += x % mod;
      seg[i] %= mod;
      i += i & -i;
    }
  }

  long long sum(long long i) {
    long long s{0};
    while (i > 0) {
      s += seg[i];
      i -= i & -i;
    }
    return s % mod;
  }

  long long sum(long long a, long long b) { return (sum(b) - sum(a)) % mod; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long>> p;
  for (long long i = 0; i < k; i++) {
    long long l, r;
    cin >> l >> r;
    p.push_back(make_pair(l, r));
  }

  Fenwick tree(n);
  tree.add(0, 1);
  for (long long i = 1; i < n; i++) {
    ll plus = 0;
    for (long long j = 0; j < k; j++) {
      if (i - p[j].first >= 0) {
        plus += tree.sum(max(0ll, i - p[j].second), i - p[j].first + 1);
        plus %= mod;
      }
    }
    tree.add(i, plus);
  }
  cout << tree.sum(n - 2, n - 1) << endl;
}