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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

ll n, x, m;

int t[100009];

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

  cin >> n >> x >> m;

  ll ans = 0;
  int time = 0;
  while (!t[x]) {
    t[x] = ++time;
    ans += x;
    x = (x * x) % m;
    n--;
    if (n == 0) {
      cout << ans << endl;
      return 0;
    }
  }

  int cy = 0;
  ll cost = 0, y = x;
  do {
    cost += y;
    y = (y * y) % m;
    cy++;
  } while (y != x);

  ans += (n / cy) * cost;
  n %= cy;

  while (n--) {
    ans += x;
    x = (x * x) % m;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

ll n, x, m;

int t[100009];

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

  cin >> n >> x >> m;

  ll ans = 0;
  int time = 0;
  while (!t[x]) {
    t[x] = ++time;
    ans += x;
    x = (x * x) % m;
    n--;
  }

  int cy = 0;
  ll cost = 0, y = x;
  do {
    cost += y;
    y = (y * y) % m;
    cy++;
  } while (y != x);

  ans += (n / cy) * cost;
  n %= cy;

  while (n--) {
    ans += x;
    x = (x * x) % m;
  }

  cout << ans << endl;

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

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

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

  long long ans = 0;
  if (X == 0) {
    cout << ans << endl;
    return 0;
  }
  if (X == 1) {
    ans = N;
    cout << ans << endl;
    return 0;
  }
  long long A = X;
  long long a = -1;
  long long b = -1;
  long long sta;
  long long suma = X;
  long long sumb;
  set<long long> st;
  for (long long i = 1; i <= N; i++) {
    ans += A;
    A = A * A % M;
    if (a < 0) {
      if (st.count(A)) {
        a = i;
        sta = A;
        sumb = A;
      } else
        suma += A;
      st.insert(A);
    } else if (b < 0) {
      if (A == sta) {
        b = i;
        break;
      } else
        sumb += A;
    }
  }
  if (a < 0 || b < 0) {
    cout << ans << endl;
    return 0;
  }
  long long n = N - a;
  long long c = b - a;
  long long d = n / c;
  n = a + c * d + 1;
  ans = suma + sumb * d;
  A = sta;
  for (long long i = n; i <= N; i++) {
    ans += A;
    A = A * A % M;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  long long ans = 0;
  if (X == 0) {
    cout << ans << endl;
    return 0;
  }
  if (X == 1) {
    ans = N;
    cout << ans << endl;
    return 0;
  }
  long long A = X;
  long long last = X;
  long long a = -1;
  long long b = -1;
  long long sta;
  long long suma = X;
  long long sumb;
  for (long long i = 1; i <= N; i++) {
    ans += A;
    A = A * A % M;
    if (a < 0) {
      if (last > A) {
        a = i;
        sta = A;
        sumb = A;
      } else
        suma += A;
    } else if (b < 0) {
      if (A == sta) {
        b = i;
        break;
      } else
        sumb += A;
    }
    last = A;
  }
  if (a < 0 || b < 0) {
    cout << ans << endl;
    return 0;
  }
  long long n = N - a;
  long long c = b - a;
  long long d = n / c;
  n = a + c * d + 1;
  ans = suma + sumb * d;
  A = sta;
  for (long long i = n; i <= N; i++) {
    ans += A;
    A = A * A % M;
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define ll long long
#define INF 2000000000
#define INF2 2000000000000000000

using namespace std;

int main() {

  IOS;

  ll n, x, m;
  cin >> n >> x >> m;
  ll arr[m + 5];
  arr[0] = x;
  map<ll, int> s;
  s[x] = 0;
  if (n <= m + 4) {
    ll sum = x;
    for (int i = 1; i < n; i++) {
      arr[i] = (arr[i - 1] * arr[i - 1]) % m;
      sum += arr[i];
    }
    cout << sum;
    return 0;
  }

  n--;
  ll tot = x;
  int startPos;
  int len;
  for (int i = 1; i <= m + 3; i++) {
    arr[i] = (arr[i - 1] * arr[i - 1]) % m;
    if (s.find(arr[i]) != s.end()) {
      startPos = s[arr[i]];
      len = i - startPos;
      break;
    }
    tot += arr[i];
    s[arr[i]] = i;
    n--;
  }
  ll sum = 0;
  for (int j = startPos; j <= startPos + len - 1; j++)
    sum += arr[j];
  tot += (n / len) * sum;
  n %= len;
  for (int j = startPos; j <= startPos + n - 1; j++)
    tot += arr[j];
  cout << tot;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define ll long long
#define INF 2000000000
#define INF2 2000000000000000000

using namespace std;

int main() {

  IOS;

  ll n, x, m;
  cin >> n >> x >> m;
  ll arr[m];
  arr[0] = x;
  map<ll, int> s;
  s[x] = 0;
  if (n <= m) {
    ll sum = x;
    for (int i = 1; i < n; i++) {
      arr[i] = (arr[i - 1] * arr[i - 1]) % m;
      sum += arr[i];
    }
    cout << sum;
    return 0;
  }

  ll tot = x;
  int startPos;
  int len;
  for (int i = 1; i < m; i++) {
    arr[i] = (arr[i - 1] * arr[i - 1]) % m;
    if (s.find(arr[i]) != s.end()) {
      startPos = s[arr[i]];
      len = i - startPos;
      break;
    }
    tot += arr[i];
    s[arr[i]] = i;
    n--;
  }
  ll sum = 0;
  for (int j = startPos; j <= startPos + len - 1; j++)
    sum += arr[j];
  tot += (n / len) * sum;
  n %= len;
  for (int j = startPos; j < startPos + n - 1; j++)
    tot += arr[j];
  cout << tot;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

ll f(ll n, ll x, ll m) {
  ll ref = x;
  ll now = x * x % m;
  int cnt = 1;
  vector<ll> d(m + 1);
  d[0] = 0;
  while (now != x) {
    d[cnt] = ref;
    ref += now;
    now = now * now % m;
    ++cnt;
  }
  ref = n / cnt * ref + d[n % cnt];
  return ref;
}

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> ok(m, true);
  ll cnt = 0;
  ll ans = 0;
  while (cnt < n) {
    if (!ok[x])
      break;
    ans += x;
    ok[x] = false;
    x = x * x % m;
    ++cnt;
  }
  ans += f(n - cnt, x, m);
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

ll f(ll n, ll x, ll m) {
  ll ref = x;
  ll now = x * x % m;
  int cnt = 1;
  vector<ll> d(m + 1);
  d[0] = 0;
  while (now != x) {
    d[cnt] = ref;
    ref += now;
    now = now * now % m;
    ++cnt;
  }
  ref = n / cnt * ref + d[n % cnt];
  return ref;
}

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> ok(n, true);
  ll cnt = 0;
  ll ans = 0;
  while (cnt < n) {
    if (!ok[x])
      break;
    ans += x;
    ok[x] = false;
    x = x * x % m;
    ++cnt;
  }
  ans += f(n - cnt, x, m);
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  ll n, x, m, ans = 0, cnt = 0;
  cin >> n >> x >> m;
  vector<int> f(m);
  bool loop = true;
  while (1) {
    x %= m;
    if (x == 0)
      break;
    if (x == 1) {
      ans += n - cnt;
      break;
    }
    if (f[x] && loop) {
      ll nx = x, ncnt = 0, sum = 0;
      while (f[nx] != 2) {
        f[nx]++;
        ncnt++;
        sum += nx;
        nx *= nx;
        nx %= m;
      }
      ll r = (n - cnt) / ncnt;
      ans += sum * r;
      cnt += ncnt * r;
      loop = false;
    }
    // 11
    // 12342342342
    // cnt = 4
    else {
      f[x]++;
      ans += x;
      x *= x;
      cnt++;
    }
    if (cnt == n)
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  ll n, x, m, ans = 0;
  cin >> n >> x >> m;
  vector<int> f(m);
  ll cnt = 0;
  bool loop = true;
  while (1) {
    x %= m;
    if (x == 0)
      break;
    if (x == 1) {
      ans += n - cnt;
      break;
    }
    if (f[x] && loop) {
      ll nx = x, ncnt = 0, sum = 0;
      while (f[nx] < 2) {
        nx %= m;
        f[nx]++;
        ncnt++;
        sum += nx;
        nx *= nx;
      }
      ll r = (n - cnt) / ncnt;
      ans += sum * r;
      cnt += ncnt * r;
      loop = false;
    }
    // 11
    // 12342342342
    // cnt = 4
    else {
      f[x]++;
      ans += x;
      x *= x;
      cnt++;
    }
    if (cnt == n)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, X, M;
  cin >> N >> X >> M;
  if (M == 0) {
    cout << X << "\\n";
    return 0;
  }
  vector<ll> already(M + 1, -1);
  already.at(X) = 1;
  ll cnt = 1;
  ll len = 0, seg_sum = X;
  ll now = X;
  vector<ll> seg_sums = {0, now};
  while (true) {
    now = (ll)pow(now, 2) % M;
    if (already.at(now) != -1) {
      len = cnt + 1 - already.at(now);
      break;
    }
    already.at(now) = cnt + 1;
    cnt += 1;
    seg_sum += now;
    seg_sums.push_back(seg_sum);
  }
  if (N < cnt) {
    cout << seg_sums.at(N) << "\\n";
  } else {
    ll ans =
        seg_sum +
        (seg_sums.at(seg_sums.size() - 1) -
         seg_sums.at(max(0ll, (ll)seg_sums.size() - len - 1))) *
            ((N - cnt) / len) +
        seg_sums.at(max(0ll, (ll)seg_sums.size() - len - 1 + (N - cnt) % len)) -
        seg_sums.at(max(0ll, (ll)seg_sums.size() - len - 1));
    cout << ans << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, X, M;
  cin >> N >> X >> M;
  if (M == 0) {
    cout << X << "\\n";
    return 0;
  }
  vector<ll> already(M + 1, -1);
  already.at(X) = 1;
  ll cnt = 1;
  ll len = N, seg_sum = X;
  ll now = X;
  vector<ll> seg_sums = {0, now};
  while (true) {
    now = (ll)pow(now, 2) % M;
    if (already.at(now) != -1) {
      len = cnt + 1 - already.at(now);
      break;
    }
    already.at(now) = cnt + 1;
    cnt += 1;
    seg_sum += now;
    seg_sums.push_back(seg_sum);
  }
  ll ans = seg_sum +
           (seg_sums.at(seg_sums.size() - 1) -
            seg_sums.at(seg_sums.size() - len - 1)) *
               ((N - cnt) / len) +
           seg_sums.at(seg_sums.size() - len - 1 + (N - cnt) % len) -
           seg_sums.at(seg_sums.size() - len - 1);
  cout << ans << "\\n";
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 998244353LL
#define INF 1000000000000000LL

using namespace std;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> l;
  ll c = x;
  l.push_back(c);
  map<int, int> mm;
  mm[c]++;

  rep(i, 100000 * 4) {
    c = c * c % m;
    l.push_back(c);
    mm[c]++;
  }

  if (n < 400000) {
    ll result = 0;
    rep(i, n) result += l[i];
    cout << result << endl;

    return 0;
  }

  ll loop_len = 0;
  ll loop_sum = 0;
  for (auto p : mm)
    if (p.second > 1) {
      loop_len++;
      loop_sum += p.first;
    }

  ll first_len = 0;
  ll first_sum = 0;
  for (auto p : mm)
    if (p.second == 1) {
      first_len++;
      first_sum += p.first;
    }

  vector<ll> loop;
  rep(i, m * 4) {
    if (mm[l[i]] > 1)
      loop.push_back(l[i]);
    if (loop.size() > 1 && loop[0] == l[i])
      break;
  }

  ll result = first_sum;
  n -= first_len;
  ;
  result += n / loop_len * loop_sum;

  n = n % loop_len;
  ll last_sum = 0;
  rep(i, n) last_sum += loop[i];

  cout << result + last_sum << endl;
  return 0;

  cout << first_len << ' ' << first_sum << endl;
  cout << last_sum << endl;
  cout << loop_sum << ' ' << loop_len << endl;

  // cout<<loop.size()<<endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 998244353LL
#define INF 1000000000000000LL

using namespace std;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> l;
  l.push_back(x);
  ll c = x * x % m;
  map<int, int> mm;
  mm[c]++;

  rep(i, 100000 * 4) {
    l.push_back(c);
    c = c * c % m;
    mm[c]++;
  }

  if (n <= 100000) {
    ll result = 0;
    rep(i, n) result += l[i];
    cout << result << endl;

    return 0;
  }

  int loop_len = 0;
  ll loop_sum = 0;
  for (auto p : mm)
    if (p.second > 2) {
      loop_len++;
      loop_sum += p.first;
    }
  int first_amari = 0;
  ll first_sum = 0;
  rep(i, m) if (mm[l[i]] == 1) {
    first_amari++;
    first_sum += l[i];
  }

  vector<ll> loop;
  rep(i, m * 4) {
    if (mm[l[i]] > 1)
      loop.push_back(l[i]);
    if (i && loop[0] == l[i])
      break;
  }

  ll result = first_sum;
  n -= first_amari;
  result += n / loop_len * loop_sum;
  n %= loop_len;
  rep(i, n) result += loop[i];

  cout << result << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

ll n, x, m;

ll order[1000009];
ll sum[1000009];

ll solve(ll n, ll x, ll m) {
  ll s = 0;
  ll loop_b = -1, loop_e = -1;
  for (ll i = 1; i <= n; i++) {
    if (order[x] != 0) {
      loop_b = order[x];
      loop_e = i;
      break;
    }
    s += x;
    order[x] = i;
    sum[i] = s;
    x = (x * x) % m;
  }
  ll ans = s;
  if (loop_b != -1) {
    ll cycle_len = loop_e - loop_b;
    ll a = (n - loop_e + 1) % cycle_len;
    ll cycle_sum = sum[loop_e - 1] - sum[loop_b - 1];
    ll cycle_num = ((n - loop_e + 1) / cycle_len);
    ans += cycle_sum * cycle_num + (sum[loop_b + a - 1] - sum[loop_b - 1]);
  }
  return ans;
}
ll _solve(ll n, ll x, ll m) {
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += x;
    x = (x * x) % m;
  }
  return ans;
}
int main() {
  cin >> n >> x >> m;
  printf("%lld\\n", solve(n, x, m));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

ll n, x, m;

ll order[1000009];
ll sum[1000009];

ll solve(ll n, ll x, ll m) {
  ll s = 0;
  ll loop_b = -1, loop_e = -1;
  reps(i, n) {
    if (order[x] != 0) {
      loop_b = order[x];
      loop_e = i;
      break;
    }
    s += x;
    order[x] = i;
    sum[i] = s;
    x = (x * x) % m;
  }
  ll ans = s;
  if (loop_b != -1) {
    ll cycle_len = loop_e - loop_b;
    ll a = (n - loop_e + 1) % cycle_len;
    ll cycle_sum = sum[loop_e - 1] - sum[loop_b - 1];
    ll cycle_num = ((n - loop_e + 1) / cycle_len);
    ans += cycle_sum * cycle_num + (sum[loop_b + a - 1] - sum[loop_b - 1]);
  }
  return ans;
}
ll _solve(ll n, ll x, ll m) {
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += x;
    x = (x * x) % m;
  }
  return ans;
}
int main() {
  cin >> n >> x >> m;
  printf("%lld\\n", _solve(n, x, m));
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>
const int mxN = 2e5;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll sum = x;
  vector<ll> nums;
  vector<ll> c(m + 1, 0);
  ll used = 0;
  bool iszero = false;
  for (int i = 1; i < n; i++) {
    x = (x * x) % m;
    sum += x;
    c[x]++;
    if (x == 0) {
      iszero = true;
      break;
    }
    if (c[x] == 2) {
      nums.pb(x);
    }
    if (c[x] == 3) {
      used = i;
      sum -= x;
      break;
    }
    // cout << x << " " << sum << "\\n";
  }
  if (used != 0 && !iszero) {
    ll hsum = 0;
    ll numssize = nums.size();
    for (ll i = 0; i < numssize; i++)
      hsum += nums[i];
    n -= used;
    ll multi = n / nums.size();
    sum += multi * hsum;
    ll remain = n - multi * nums.size();
    for (ll i = 0; i < remain; i++)
      sum += nums[i];
  }
  cout << sum;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>
const int mxN = 2e5;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll sum = x;
  vector<ll> nums;
  vector<ll> c(m + 1, 0);
  ll used = 0;
  for (int i = 1; i <= n; i++) {
    x = (x * x) % m;
    sum += x;
    c[x]++;
    if (c[x] == 2) {
      nums.pb(x);
    }
    if (c[x] == 3) {
      used = i;
      sum -= x;
      break;
    }
  }
  ll hsum = 0;
  for (ll i = 0; i < nums.size(); i++)
    hsum += nums[i];
  n -= used;
  ll multi = n / nums.size();
  sum += multi * hsum;
  ll remain = n - multi * nums.size();
  for (ll i = 0; i < remain; i++)
    sum += nums[i];
  cout << sum;
}
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 < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, x, m;
  cin >> n >> x >> m;
  vector<pair<bool, int>> samefirst(m);
  rep(i, m) samefirst[i].first = false;
  vl a(m);
  a[0] = x;
  samefirst[a[0]].first = true;
  samefirst[a[0]].second = 0;
  int end = -1, aend = -1;
  for (int i = 1; i < n; i++) {
    a[i] = a[i - 1] * a[i - 1] % m;
    if (samefirst[a[i]].first) {
      end = i;
      aend = a[i];
      break;
    } else {
      samefirst[a[i]].first = true;
      samefirst[a[i]].second = i;
    }
  }
  ll ans = 0;
  if (end != -1) {
    ll sum = 0;
    ll first = samefirst[aend].second;
    for (int i = first; i < end; i++)
      sum += a[i];
    rep(i, first) ans += a[i];
    ll rest1 = n - first;
    ans += rest1 / (end - first) * sum;
    ll rest2 = rest1 % (end - first);
    for (int i = first; i < first + rest2; i++)
      ans += a[i];
  } else
    rep(i, n) ans += a[i];
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, x, m;
  cin >> n >> x >> m;
  ll ai, ai1;
  ai = x;
  ll ans = x;
  for (ll i = 0; i < n - 1; i++) {
    ai1 = ai % m * ai % m;
    ans += ai1;
    ai = ai1;
  }
  cout << ans << endl;
}