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

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

int64_t solve() {
  int64_t N, X, M;
  cin >> N >> X >> M;
  int64_t ans = 0;
  map<int64_t, int> mp;
  vector<int64_t> A(M + 1);
  int L = -1;
  rep(i, min(N, M)) {
    A[i] = X;
    if (mp.find(X) != mp.end())
      L = i - mp[X];
    mp[X] = i;
    ans += X;
    X = X * X % M;
  }
  if (N <= M || L == -1)
    return ans;
  N -= M;
  int64_t roop = N / L;
  int64_t move = N % L;
  vector<int64_t> dp(L + 1);
  rep(i, L) dp[i + 1] = A[M - L + i];
  rep(i, L) dp[i + 1] += dp[i];
  ans += dp[L] * roop + dp[move];
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << solve() << '\\n';
}


Error: 0

Fixed submission:

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

int64_t solve() {
  int64_t N, X, M;
  cin >> N >> X >> M;
  int64_t ans = 0;
  map<int64_t, int> mp;
  vector<int64_t> A(M + 1);
  int L = 0;
  rep(i, min(N, M)) {
    A[i] = X;
    if (mp.find(X) != mp.end())
      L = i - mp[X];
    mp[X] = i;
    ans += X;
    X = X * X % M;
  }
  if (N <= M)
    return ans;
  N -= M;
  int64_t roop = N / L;
  int64_t move = N % L;
  vector<int64_t> dp(L + 1);
  rep(i, L) dp[i + 1] = A[M - L + i];
  rep(i, L) dp[i + 1] += dp[i];
  ans += dp[L] * roop + dp[move];
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << solve() << '\\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 srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
using vi = vector<int>;
#define pb push_back
int main() {
  ll n;
  cin >> n;
  int x, m;
  scanf("%d%d", &x, &m);
  map<int, int> mp;
  vi a;
  int cnt = 0;
  ll tot = 0;
  while (mp.count(x) == 0) {
    a.pb(x);
    tot += x;
    mp[x] = cnt;
    ++cnt;
    x = (ll)x * x % m;
  }
  if (n <= cnt) {
    ll ans = 0;
    rep(i, n) ans += a[i];
    cout << ans << endl;
    return 0;
  }
  ll c = 0;
  int s = cnt - mp[x];
  srep(i, mp[x], cnt) c += a[i];
  ll ans = tot;
  n -= cnt;
  ans += n / s * c;
  n %= s;
  int si = mp[x];
  rep(i, n) ans += a[si + i];
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n;
  cin >> n;
  int x, m;
  cin >> x >> m;
  map<int, int> h;
  vector<int> a;
  int cnt = 0;
  ll res = 0;
  while (h.count(x) == 0) {
    a.push_back(x);
    res += x;
    h[x] = res;
    ++cnt;
    x = (ll)x * x % m;
  }
  if (n <= cnt) {
    ll val = 0;
    for (int i = 0; i < n; i++)
      val += a[i];
    cout << val << endl;
    return 0;
  }
  ll c = 0;
  int s = cnt - h[x];
  for (int i = h[x]; i < cnt; i++)
    c += a[i];
  ll ans = res;
  n -= cnt;
  ans += n / s * c;
  n %= s;
  int k = h[x];
  for (int i = 0; i < n; i++)
    ans += a[k + i];
  cout << ans << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  ll check[110000] = {};
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, m + 10) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  ll check[110000] = {};
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, n) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
ll n, x, m;
int cnt, cnt2;
int ha[100005];
map<int, int> pre;
int main() {
  cin >> n >> x >> m;
  ll now = x;
  pre[x] = 1;
  ha[1] = x;
  for (int i = 2; i <= 2 * m + 1; i++) {
    now = now * now % m;
    if (pre.find(now) != pre.end()) {
      cnt = i - 1;
      cnt2 = pre[now] - 1;
      break;
    } else
      pre[now] = i, ha[i] = now;
  }
  ll ans = 0;
  for (int i = 1; i <= min(n, (ll)cnt2); i++)
    ans += ha[i];
  ll ans2 = 0;
  for (int i = 1; i <= ((n - cnt2) < 0 ? 0 : cnt - cnt2); i++)
    ans2 += ha[i + cnt2];
  ans2 = ans2 * (max(0ll, (n - cnt2) / (cnt - cnt2)));
  for (int i = 1; i <= max(0ll, (n - cnt2) % (cnt - cnt2)); i++)
    ans += ha[i + cnt2];
  cout << ans + ans2 << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
ll n, x, m;
int cnt, cnt2;
int ha[100005];
map<int, int> pre;
int main() {
  cin >> n >> x >> m;
  ll now = x;
  pre[x] = 1;
  ha[1] = x;
  for (int i = 2; i <= min(m, n); i++) {
    now = now * now % m;
    if (pre.find(now) != pre.end()) {
      cnt = i - 1;
      cnt2 = pre[now] - 1;
      break;
    } else
      pre[now] = i, ha[i] = now;
  }
  ll ans = 0;
  for (int i = 1; i <= cnt2; i++)
    ans += ha[i];
  ll ans2 = 0;
  for (int i = 1; i <= min(n - cnt2, (ll)cnt - cnt2); i++)
    ans2 += ha[i + cnt2];
  ans2 = ans2 * (max(0ll, (n - cnt2) / (cnt - cnt2)));
  for (int i = 1; i <= max(0ll, (n - cnt2) % (cnt - cnt2)); i++)
    ans += ha[i + cnt2];
  cout << ans + ans2 << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, x, m;
  cin >> n >> x >> m;

  VL cx(100100);
  cx[0] = x;
  rep(i, 100100) cx[i + 1] = f(cx[i], m);
  // for(auto x :cx) cerr<<x<<" "; dbg();

  ll ans = 0;
  if (n < 100010) {
    rep(i, n) ans += cx[i];
  } else {

    map<ll, int> mp;
    int st = 0, ss = 0;
    rep(i, 100999) {
      if (mp.count(cx[i])) {
        st = mp[cx[i]];
        ss = i;
        break;
      }
      mp[cx[i]] = i;
    }
    ll lp = ss - st;
    ll su = (n - st) / lp;
    ll sm = 0;
    rep2(i, st, ss) sm += cx[i];
    ans = su * sm;
    ll am = (n - st) % lp;
    rep(i, st) ans += cx[i];
    rep2(i, st, st + am) ans += cx[i];
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, x, m;
  cin >> n >> x >> m;

  VL cx(100100);
  cx[0] = x;
  rep(i, 100100) cx[i + 1] = f(cx[i], m);
  // for(auto x :cx) cerr<<x<<" "; dbg();

  map<ll, int> mp;
  int st = 0, ss = 0;
  rep(i, m) {
    if (mp.count(cx[i])) {
      st = mp[cx[i]];
      ss = i;
      break;
    }
    mp[cx[i]] = i;
  }

  ll ans = 0;
  if (n < 100000) {
    rep(i, n) ans += cx[i];
  } else {

    ll lp = ss - st;
    ll su = n / lp;
    ll sm = 0;
    rep2(i, st, ss) sm += cx[i];
    ans = su * sm;
    ll am = n % lp;
    rep(i, st) ans += cx[i];
    rep2(i, st, am) ans += cx[i];
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  ll x;
  cin >> x;
  ll m;
  cin >> m;
  ll c = 1;
  vll d(m + 10);
  ll count = 0;
  ll sum = 0;
  vll s(m + 10);
  while (true) {
    if (count != 0)
      c = c * c % m;
    else
      c = x % m;
    if (d[c] != 0)
      break;
    count++;
    d[c] = count;
    sum = sum + c;
    s[count] = sum;
    if (count == n) {
      cout << sum << endl;
      return 0;
    }
  }
  ll ans = 0;
  ll sum2 = s[count] - s[d[c] - 1];
  ans = sum + sum2 * ((n - count) / (count - d[c] + 1));
  ans += s[d[c] - 1 + (n - count) % (count - d[c] + 1)] - s[d[c] - 1];
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  ll x;
  cin >> x;
  ll m;
  cin >> m;
  ll c = 1;
  vll d(m + 10);
  ll count = 0;
  ll sum = 0;
  vll s(m + 10);
  while (true) {
    if (count != 0)
      c = c * c % m;
    else
      c = x % m;
    if (d[c] != 0)
      break;
    count++;
    d[c] = count;
    sum = sum + c;
    s[count] = sum;
  }
  ll ans = 0;
  ll sum2 = s[count] - s[d[c] - 1];
  ans = sum + sum2 * ((n - count) / (count - d[c] + 1));
  ans += s[d[c] - 1 + (n - count) % (count - d[c] + 1)] - s[d[c] - 1];
  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
/*
struct Edge
{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
*/
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, X, M;
  cin >> N >> X >> M;

  vector<int> ord(M, -1);
  vector<ll> rireki, syu;
  ll res = 0;
  for (int n = 0; n < N; n++) {
    if (ord[X] != -1) {
      int p = ord[X];
      for (int i = p; i < n; i++) {
        syu.push_back(rireki[i]);
      }
      break;
    }
    ord[X] = n;
    rireki.push_back(X);
    res += X;
    X = (X * X) % M;
    cerr << X << endl;
  }
  N -= rireki.size();
  if (N == 0) {
    cout << res << endl;
    return 0;
  }

  vector<ll> sum(syu.size() + 1, 0);
  for (int i = 0; i < syu.size(); i++)
    sum[i + 1] = sum[i] + syu[i];

  ll q = N / syu.size();
  ll r = N % syu.size();
  res += sum[syu.size()] * q + sum[r];
  cout << res << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
/*
struct Edge
{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
*/
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M, X;
  cin >> N >> M >> X;

  vector<int> ord(M, -1);
  vector<ll> rireki, syu;
  ll res = 0;
  for (int n = 0; n < N; n++) {
    if (ord[X] != -1) {
      int p = ord[X];
      for (int i = p; i < n; i++) {
        syu.push_back(rireki[i]);
      }
      break;
    }
    ord[X] = n;
    rireki.push_back(X);
    res += X;
    X = (X * X) % M;
  }
  N -= rireki.size();

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

  vector<ll> sum(syu.size() + 1, 0);
  for (int i = 0; i < syu.size(); i++)
    sum[i + 1] = sum[i] + syu[i];

  ll q = N / syu.size();
  ll r = N % syu.size();
  res += sum[syu.size()] * q + sum[r];
  cout << res << endl;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\\n'
typedef pair<int, int> P;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> flag(1000000, -1);
  flag[x] = 0;
  vector<ll> dp(1000000);
  dp[0] = x;
  ll l = 0, r = 0;
  rep(i, m) {
    dp[i + 1] = (dp[i] * dp[i]) % m;
    if (flag[dp[i + 1]] >= 0) {
      l = flag[dp[i + 1]];
      r = i + 1;
    }
    flag[dp[i + 1]] = i + 1;
  }
  vector<ll> sum = dp;

  rep(i, m) { sum[i + 1] += sum[i]; }

  if (n < l) {
    cout << sum[n - 1] << endl;
  } else {
    ll ans = 0;
    if (l > 0) {
      ans += sum[l - 1];
      n -= l;
    }
    ans += (sum[r] - sum[l]) * (n / (r - l));
    if (l > 0) {
      ans += sum[l + n % (r - l) - 1] - sum[l - 1];
    } else {
      ans += sum[l + n % (r - l) - 1];
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\\n'
typedef pair<int, int> P;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> flag(1000000, -1);
  flag[x] = 0;
  vector<ll> dp(1000000 + 1);
  dp[0] = x;
  ll l = 1e9, r = 1e9;
  rep(i, n) {
    dp[i + 1] = (dp[i] * dp[i]) % m;
    if (flag[dp[i + 1]] >= 0) {
      l = flag[dp[i + 1]];
      r = i + 1;
      break;
    }
    flag[dp[i + 1]] = i + 1;
  }
  vector<ll> sum = dp;

  rep(i, m) { sum[i + 1] += sum[i]; }

  if (n <= r) {
    cout << sum[n - 1] << endl;
  } else {
    ll ans = 0;
    if (l > 0) {
      ans += sum[l - 1];
      n -= l;
    }
    ans += (sum[r] - sum[l]) * (n / (r - l));
    if (l > 0) {
      ans += sum[l + n % (r - l) - 1] - sum[l - 1];
    } else {
      ans += sum[l + n % (r - l) - 1];
    }
    cout << ans << endl;
  }
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll N, X, M, S;
  cin >> N >> X >> M;
  ll m[M], n[M + 1];
  S = X;
  for (ll i = 0; i < M; i++)
    m[i] = -1;
  m[X] = 1;
  n[1] = X;
  for (ll i = 2; i <= N; i++) {
    X = (X * X) % M;
    if (m[X] != -1) {
      ll y = (N - i + 1) / (i - m[X]);
      if (y > 0) {
        ll o = 0;
        for (ll j = m[X]; j < i; j++)
          o += n[j];
        S += o * y;
      }
      ll l = (N - i + 1) % (i - m[X]);
      for (ll j = m[X]; j < m[X] + l; j++)
        S += n[j];
      cout << S;
      return 0;
    }
    m[X] = i;
    n[i] = X;
    S += X;
  }
  cout << S;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll N, X, M, S;
  cin >> N >> X >> M;
  ll m[M + 1], n[M + 1];
  S = X;
  m[X] = 1;
  n[1] = X;
  for (ll i = 2; i <= N; i++) {
    X = (X * X) % M;
    if (m[X]) {
      ll y = (N - i + 1) / (i - m[X]);
      if (y > 0) {
        ll o = 0;
        for (ll j = m[X]; j < i; j++)
          o += n[j];
        S += o * y;
      }
      ll l = (N - i + 1) % (i - m[X]);
      for (ll j = m[X]; j < m[X] + l; j++)
        S += n[j];
      cout << S;
      return 0;
    }
    m[X] = i;
    n[i] = X;
    S += X;
  }
  cout << S;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (long long(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll tmp = x;
  vector<bool> flag(100010, false);
  vl vec(100010, 0);
  vec[0] = tmp;
  flag[tmp] = true;

  ll total_len = 0;
  ll l_start = 0;

  reps(i, 1, 100010) {
    tmp = (tmp * tmp) % m;
    vec[i] = tmp;
    if (!flag[tmp])
      flag[tmp] = true;
    else {
      total_len = i;
      break;
    }
  }

  rep(i, 100010) {
    if (vec[i] == vec[total_len]) {
      l_start = i;
      break;
    }
  }

  ll len = total_len - l_start;
  ll ans = 0;
  ll lsum = 0;

  rep(i, l_start) { ans += vec[i]; }
  reps(i, l_start, total_len) { lsum = (lsum + vec[i]); }

  ll lcnt = (n - l_start) / len;
  ans += lcnt * lsum;
  ll tmp2 = (n - l_start) % len;

  reps(i, l_start, l_start + tmp2) { ans += vec[i]; }

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll tmp = x;
  vector<bool> flag(100010, false);
  vl vec(100010, 0);
  vec[0] = tmp;
  flag[tmp] = true;

  ll total_len = 0;
  ll l_start = 0;

  reps(i, 1, 100010) {
    tmp = (tmp * tmp) % m;
    vec[i] = tmp;
    if (!flag[tmp])
      flag[tmp] = true;
    else {
      total_len = i;
      break;
    }
  }

  reps(i, 1, 100010) {
    if (vec[i] == vec[total_len]) {
      l_start = i;
      break;
    }
  }

  ll len = total_len - l_start;
  ll ans = 0;
  ll lsum = 0;

  rep(i, l_start) { ans += vec[i]; }
  reps(i, l_start, total_len) { lsum = (lsum + vec[i]); }

  ll lcnt = (n - l_start) / len;
  ans += lcnt * lsum;
  ll tmp2 = (n - l_start) % len;

  reps(i, l_start, l_start + tmp2) { ans += vec[i]; }

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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define pb push_back
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()

typedef pair<int, int> P;

const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//------------------------------------------------------//

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vec<int> id(m, -1);
  vec<int> a;
  int len = 0;
  ll tot = 0;
  while (id[x] == -1) {
    a.pb(x);
    id[x] = len;
    len++;
    tot += x;
    x = (x * x) % m;
  }

  int c = len - id[x];
  ll s = 0;
  repc(i, id[x], len - 1) 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>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repab(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define pb push_back
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
#define endl "\\n"

typedef pair<int, int> P;

const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//--//--//--//--//--//--//--//--//--//--//--//

int n, k;
int s[20];
ll dp[200001];
ll ans = 0;

int main() {
  cin >> n >> k;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    s[2 * i] = l;
    s[2 * i + 1] = r;
  }

  rep(i, n) { dp[i] = 0; }

  dp[1] = 1;
  rep(p, n) {
    rep(i, k) {
      for (int j = s[2 * i]; j <= s[2 * i + 1]; j++) {
        if (i + 1 + j <= n) {
          dp[i + 1 + j] += dp[i + 1];
        }
      }
    }
  }

  cout << dp[n] % 998244353 << endl;

  return 0;
}