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



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

const bool ready = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
  return true;
}();

const double PI = acos(-1);
using ll = long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for (int i = 0; i < int(n); i++)

#define cini(i)                                                                \\
  int i;                                                                       \\
  cin >> i;
#define cins(s)                                                                \\
  string s;                                                                    \\
  cin >> s;
#define cind(d)                                                                \\
  double d;                                                                    \\
  cin >> d;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  fori(n) { cin >> a[i]; }
#define cinas(s, n)                                                            \\
  vs s(n);                                                                     \\
  fori(n) { cin >> s[i]; }
#define cinad(a, n)                                                            \\
  vd a(n);                                                                     \\
  fori(n) { cin >> a[i]; }

using pii = pair<int, int>;
using pdd = pair<double, double>;
using vd = vector<double>;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;

#define endl "\\n"

int step(int a, int m) { return (a * a) % m; }

/* At less than m elements the seq repeats. */
void solve() {
  cini(n);
  cini(x);
  cini(m);

  vi first(m, -1);
  vi pre(2 * m, 0);

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (first[x] >= 0) {
      int loopsize = i - first[x];
      int loopsum = pre[i - 1] - pre[first[x] - 1];

      int loops = (n - i) / loopsize;
      ans += loops * loopsum;
      i += loops * loopsize;
      for (int j = 0; j < (int)first.size(); j++)
        first[j] = -1;
    }

    if (i < (int)pre.size()) {
      pre[i] = pre[i - 1] + x;
      first[x] = i;
    }

    ans += x;
    x = step(x, m);
  }
  cout << ans << endl;
}

signed main() { solve(); }


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;

int main() {
  ll i, j, n, k;
  cin >> n >> k;
  vector<ll> l(n), r(n);
  for (i = 0; i < n; i++)
    cin >> l[i] >> r[i];
  vector<ll> dp(n + 2, 0), dpsum(n + 2, 0);
  dp[1] = 1;
  dpsum[1] = 1;
  ll mod = 998244353;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < k; j++) {
      ll li = i - r[j];
      ll ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1ll);
      dp[i] += dpsum[ri] - dpsum[li - 1];
      dp[i] = (dp[i] + mod) % mod;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
  }
  cout << dp[n];
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (decltype(beg) i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (decltype(beg) i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--)
#define REP(n)                                                                 \\
  for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++)

using namespace std;

struct N {
  int64_t c;
  uint64_t sum;
};

int64_t solve() {

  uint64_t n, x, m;
  cin >> n >> x >> m;

  vector<N> ns(m + 100, {-1, 0});
  uint64_t sum = 0;
  int64_t c = 0;
  bool has_loop = false;

  REP(n) {
    if (ns[x].c != -1) {
      has_loop = true;
      break;
    }
    ns[x].c = c;
    ns[x].sum = sum;

    c++;
    sum += x;
    x = x * x % m;
  }
  if (!has_loop) {
    return sum;
  }

  uint64_t ans = sum;
  uint64_t r = n - c;
  uint64_t rr = (c - ns[x].c);
  // cout << ans << " " << r << endl;
  // cout << (sum - ns[x].sum) << " " << (r / (c - ns[x].c)) << endl;
  ans += (sum - ns[x].sum) * (r / rr);
  r %= rr;
  REP(r) {
    ans += x;
    x = (x * x);
    x %= m;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);
  cout << solve() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (decltype(beg) i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (decltype(beg) i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--)
#define REP(n)                                                                 \\
  for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++)

using namespace std;

struct N {
  int64_t c;
  uint64_t sum;
};

int64_t solve() {

  uint64_t n, x, m;
  cin >> n >> x >> m;

  uint64_t sum = 0;

  REP(n) {
    sum += x;
    x = (x * x) % m;
  }
  return sum;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);
  cout << solve() << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;
const ll lnf = 1ll << 60;

ll val[60][100010];
ll to[60][100010];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  rep(j, m) {
    val[0][j] = j;
    to[0][j] = (ll)j * j % m;
  }
  rep(i, 60 - 1) rep(j, m) {
    val[i + 1][j] = val[i][j] + val[i][to[i][j]];
    to[i + 1][j] = to[i][to[i][j]];
  }
  ll next = x, ans = 0;
  for (int i = 60 - 1; i >= 0; i--) {
    if ((n >> i) & 1) {
      ans += val[i][next];
      next = to[i][next];
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;
const ll lnf = 1ll << 60;

ll val[60][100010];
ll to[60][100010];

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  rep(j, m) {
    val[0][j] = j;
    to[0][j] = j * j % m;
  }
  rep(i, 60 - 1) rep(j, m) {
    val[i + 1][j] = val[i][j] + val[i][to[i][j]];
    to[i + 1][j] = to[i][to[i][j]];
  }
  ll next = x, ans = 0;
  for (ll i = 60 - 1; i >= 0; i--) {
    if ((n >> i) & 1) {
      ans += val[i][next];
      next = to[i][next];
    }
  }
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (ll)(n); ++i)
#define rrep(i, j, n) for (int i = (int)(n)-1; j <= i; --i)

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
  os << "{";
  for (size_t i = 0; i < a.size(); ++i)
    os << (i > 0 ? "," : "") << a[i];
  return os << "}";
}

[[maybe_unused]] constexpr ll MOD = 1000000007;
[[maybe_unused]] constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  rep(i, 0, n) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      rep(j, 0, one_cycle.size()) if (one_cycle[j] == now) {
        start = j;
        break;
      }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = (int)one_cycle.size() - start;
  rep(i, 0, start) s1 += one_cycle[i];
  rep(i, start, one_cycle.size()) s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  rep(i, 0, (n - start) % loop) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)(n); ++i)

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

  ll y = x;
  vector<ll> used(m + 1);
  ll start = 0, end = 0;
  rep(i, 0, n) {
    used[x]++;
    end = x;
    x = x * x % m;
    if (used[x] == 1) {
      start = x;
      break;
    }
  }
  ll ans = 0;
  ll now = y;
  while (n > 0 && now != start) {
    ans += now;
    now = now * now % m;
    --n;
  }
  ll loop = 1;
  ll loop_sum = now;
  now = now * now % m;
  ll cnt = 1000000;
  while (cnt > 0 && now != start) {
    loop_sum += now;
    now = now * now % m;
    ++loop;
    --cnt;
  }

  if (n <= 0 || x == 0) {
    cout << ans << endl;
    return 0;
  }
  ans += n / loop * loop_sum;
  n %= loop;
  rep(i, 0, n) {
    ans += x;
    x = x * x % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < n; ++i)

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

  ll y = x;
  vector<ll> used(m + 1);
  ll start = 0, end = 0;
  rep(i, 0, n) {
    used[x]++;
    end = x;
    x = x * x % m;
    if (used[x] == 1) {
      start = x;
      break;
    }
  }
  ll ans = 0;
  ll now = y;
  while (n > 0 && now != start) {
    ans += now;
    now = now * now % m;
    --n;
  }
  ll loop = 1;
  ll loop_sum = now;
  now = now * now % m;
  ll cnt = 1000000;
  while (cnt > 0 && now != start) {
    loop_sum += now;
    now = now * now % m;
    ++loop;
    --cnt;
  }

  if (n <= 0 || x == 0) {
    cout << ans << endl;
    return 0;
  }
  ans += n / loop * loop_sum;
  n %= loop;
  rep(i, 0, n) {
    ans += x;
    x = x * x % m;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < n; ++i)

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

  ll y = x;
  vector<int> used(m + 1);
  ll start = 0, end = 0;
  rep(i, 0, n) {
    end = x;
    x = x * x % m;
    if (used[x] == 1) {
      start = x;
      break;
    }
    used[x]++;
  }
  ll ans = 0;
  ll now = y;
  while (now != start) {
    ans += now;
    now = now * now % m;
    --n;
  }
  ll loop = 1;
  ll loop_sum = now;
  now = now * now % m;
  while (now != start) {
    loop_sum += now;
    now = now * now % m;
    ++loop;
  }

  if (n <= 0 || x == 0) {
    cout << ans << endl;
    return 0;
  }
  ans += n / loop * loop_sum;
  n %= loop;
  rep(i, 0, n) {
    ans += x;
    x = x * x % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (n); ++i)

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  rep(i, 0, n) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      rep(j, 0, one_cycle.size()) if (one_cycle[j] == now) {
        start = j;
        break;
      }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = one_cycle.size() - start;
  rep(i, 0, start) s1 += one_cycle[i];
  rep(i, start, one_cycle.size()) s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  rep(i, 0, (n - start) % loop) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; (s) <= i; i--)

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  rep(i, 0, n) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      rep(j, 0, one_cycle.size()) if (one_cycle[j] == now) {
        start = j;
        break;
      }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = one_cycle.size() - start;
  rep(i, 0, start) s1 += one_cycle[i];
  rep(i, start, one_cycle.size()) s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  rep(i, 0, (n - start) % loop) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  for (int i = 0; i < n; ++i) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      for (int j = 0; j < (int)one_cycle.size(); ++j)
        if (one_cycle[j] == now) {
          start = j;
          break;
        }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = one_cycle.size() - start;
  for (int i = 0; i < start; ++i)
    s1 += one_cycle[i];
  for (int i = start; i < one_cycle.size(); ++i)
    s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  for (int i = 0; i < (n - start) % loop; ++i) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)(n); ++i)
#define rrep(i, j, n) for (int i = (int)(n)-1; j <= i; --i)

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
  os << "{";
  for (size_t i = 0; i < a.size(); ++i)
    os << (i > 0 ? "," : "") << a[i];
  return os << "}";
}

[[maybe_unused]] constexpr ll MOD = 1000000007;
[[maybe_unused]] constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> used(m * 2);
  vector<ll> one_cycle;

  int start = 0;
  ll now = x;
  rep(i, 0, n) {
    used[now] = true;
    one_cycle.push_back(now);
    now = now * now % m;
    if (used[now]) {
      rep(j, 0, one_cycle.size()) if (one_cycle[j] == now) {
        start = j;
        break;
      }
      break;
    }
  }

  ll s1 = 0, s2 = 0, loop = one_cycle.size() - start;
  rep(i, 0, start) s1 += one_cycle[i];
  rep(i, start, one_cycle.size()) s2 += one_cycle[i];
  ll ans = s1 + (n - start) / loop * s2;
  rep(i, 0, (n - start) % loop) {
    ans += now;
    now = now * now % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;

ll solve(ll n, ll x, ll m) {
  vector<int> isexist(m, 0);
  ll cur = x;
  int loopstart;
  int loopstepnum = -1;
  int initstep;
  for (int i = 1; i <= m + 10; i++) {
    if (isexist[cur] != 0) {
      loopstart = cur;
      initstep = isexist[cur] - 1;
      loopstepnum = i - isexist[cur];
      break;
    } else {
      isexist[cur] = i;
      cur = cur * cur;
      cur %= m;
    }
  }

  if (loopstepnum != -1) {
    vector<ll> loopsum(loopstepnum + 1, 0);
    {
      ll curnum = loopstart;
      for (int i = 1; i <= loopstepnum; i++) {
        loopsum[i] = loopsum[i - 1] + curnum;
        curnum = (curnum * curnum) % m;
      }
    }

    ll ans = 0;
    ll cur2 = x;
    for (int i = 0; i < min((ll)initstep, n); i++) {
      ans += cur2;
      cur2 = (cur2 * cur2) % m;
    }

    ans += loopsum[loopstepnum] * ((n - initstep) / loopstepnum);
    int laststep = (n - initstep) % loopstepnum;
    ans += loopsum[laststep];
    return ans;
  } else {
    return 0;
  }
}

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

  cout << solve(n, x, m) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;

ll solve(ll n, ll x, ll m) {
  vector<int> isexist(m, 0);
  ll cur = x;
  int loopstart;
  int loopstepnum;
  int initstep;
  for (int i = 1; i <= m; i++) {
    if (isexist[cur] != 0) {
      loopstart = cur;
      initstep = isexist[cur] - 1;
      loopstepnum = i - isexist[cur];
      break;
    } else {
      isexist[cur] = i;
      cur = cur * cur;
      cur %= m;
    }
  }

  vector<ll> loopsum(loopstepnum + 1, 0);
  {
    ll curnum = loopstart;
    for (int i = 1; i <= loopstepnum; i++) {
      loopsum[i] = loopsum[i - 1] + curnum;
      curnum = (curnum * curnum) % m;
    }
  }

  ll ans = 0;
  ll cur2 = x;
  for (int i = 0; i < min((ll)initstep, n); i++) {
    ans += cur2;
    cur2 = (cur2 * cur2) % m;
  }

  ans += loopsum[loopstepnum] * ((n - initstep) / loopstepnum);
  int laststep = (n - initstep) % loopstepnum;
  if (laststep > 0)
    ans += loopsum[laststep];
  return ans;
}

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

  cout << solve(n, x, m) << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> inline T read() {
  T x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -f;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c - 48);
    c = getchar();
  }
  return x * f;
}

#define lint long long int
#define ulint unsigned lint
#define readint read<int>()
#define readlint read<lint>()
const int inf = 1e9 + 1e7, MAXN = 1e5 + 1e1;

lint dp[MAXN], t[MAXN], tmp = 0, ans = 0, n, x, y, m, cnt = 0;

int main(void) {

  // freopen("testdata.in","r",stdin);
  // freopen("testdata.out","w",stdout);

  scanf("%lld%lld%lld", &n, &x, &m);
  t[x] = 1, tmp = x, dp[x] = x, y = x * x % m, ++cnt;
  while (!t[y] && cnt < n) {
    t[y] = cnt + 1, tmp += y, dp[y] = dp[x] + y, x = y, y = x * x % m, ++cnt;
  }
  ans = tmp;
  if (cnt == n) {
    printf("%lld\\n", ans);
    return 0;
  }
  n -= cnt, ans += (n / (cnt - t[y] + 1)) * (tmp - dp[y] + y),
      n %= (cnt - t[y] + 1), cnt = 0;
  while (cnt < n) {
    ++cnt, ans += y, x = y, y = x * x % m;
  }
  printf("%lld\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

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

template <typename T> inline T read() {
  T x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -f;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c - 48);
    c = getchar();
  }
  return x * f;
}

#define lint long long int
#define ulint unsigned lint
#define readint read<int>()
#define readlint read<lint>()
const int inf = 1e9 + 1e7, MAXN = 1e5 + 1e1;

lint dp[MAXN], t[MAXN], tmp = 0, ans = 0, n, x, y;
int m, cnt = 0;

int main(void) {

  // freopen("testdata.in","r",stdin);
  // freopen("testdata.out","w",stdout);

  scanf("%lld%lld%d", &n, &x, &m);
  t[x] = 1, tmp = x, dp[x] = x, y = x * x % m, ++cnt;
  while (!dp[y] && cnt < n) {
    t[y] = cnt + 1, tmp += y, dp[y] = dp[x] + y, x = y, y = x * x % m, ++cnt;
  }
  ans = tmp;
  if (cnt == n) {
    printf("%lld\\n", ans);
    return 0;
  }
  n -= cnt, ans += (n / (cnt - t[y] + 1)) * (tmp - dp[y] + y),
      n %= (cnt - t[y] + 1), cnt = 0;
  while (cnt < n) {
    ++cnt, ans += y, x = y, y = x * x % m;
  }
  printf("%lld\\n", ans);

  return 0;
}