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

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> cir;
  int cur = x;
  vector<bool> vis(m);
  int start = -1;
  long long ans = 0;
  long long sum = 0;
  while (n > 0) {
    if (cur == 0) {
      cout << ans << '\\n';
      exit(0);
    }
    if (vis[cur]) {
      start = cur;
      break;
    }
    n -= 1;
    ans += cur;
    vis[cur] = true;
    cur = 1LL * cur * cur % m;
  }

  if (n == 0) {
    cout << ans << '\\n';
    exit(0);
  }

  for (int i = 0; i < m; ++i) {
    vis[i] = false;
  }
  while (!vis[start]) {
    cir.emplace_back(start);
    vis[start] = true;
    sum += start;
    start = 1LL * start * start % m;
  }

  int s = cir.size();
  ans += n / s * sum;
  for (int i = 0; i < n % s; ++i) {
    ans += cir[i];
  }

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

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> cir;
  int cur = x;
  vector<bool> vis(m);
  int start = -1;
  long long ans = 0;
  long long sum = 0;
  while (n > 0) {
    if (cur == 0) {
      cout << ans << '\\n';
      exit(0);
    }
    if (vis[cur]) {
      start = cur;
      break;
    }
    n -= 1;
    ans += cur;
    vis[cur] = true;
    cur = 1LL * cur * cur % m;
  }

  vis.assign(n, false);
  while (!vis[start]) {
    cir.emplace_back(start);
    vis[start] = true;
    sum += start;
    start = 1LL * start * start % m;
  }

  int s = cir.size();
  ans += n / s * sum;
  for (int i = 0; i < n % s; ++i) {
    ans += cir[i];
  }

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

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

#include <iostream>
#include <vector>
using namespace std;
using ulong = unsigned long;
using ull = unsigned long long;

ulong getRepeatPos(const vector<ull> &items) {
  const size_t itemSize = items.size();
  ull newestItem = items[itemSize - 1];
  for (ulong i = itemSize - 2;; i--) {
    if (newestItem == items[i]) {
      return i;
    }
    if (i == 0) {
      break;
    }
  }
  return itemSize;
}

ull getResult(ulong term, const uint initial, const uint mod) {
  vector<ull> items(1, initial);
  ull sum = initial;
  ull newestItem = initial;

  size_t itemSize;
  ulong repeatPosMin, repeatPosMax;
  for (ulong i = 1;; i++) {
    if (i >= term) {
      return sum;
    }
    items.push_back(newestItem * newestItem % mod);
    itemSize = items.size();
    newestItem = items[itemSize - 1];
    repeatPosMin = getRepeatPos(items);
    if (repeatPosMin < itemSize - 1) {
      repeatPosMax = itemSize - 1;
      break;
    }
    sum += newestItem;
  }

  const ulong repeatCount = repeatPosMax - repeatPosMin;
  ull repeatSum = 0;
  for (ulong i = repeatPosMin; i < repeatPosMax; i++) {
    repeatSum += items[i];
  }

  term -= repeatPosMax;
  sum += term / repeatCount * repeatSum;

  ulong surplus = term % repeatCount;
  if (surplus != 0) {
    for (ulong i = 0; i < surplus; i++) {
      sum += items[i + repeatPosMin];
    }
  }

  return sum;
}

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

  ull result = getResult(N, X, M);
  cout << result;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

unsigned long long getResult(const unsigned long term, const uint initial,
                             const uint mod) {
  unsigned long long item = initial;
  unsigned long long sum = initial;
  for (unsigned long i = 1; i < term; i++) {
    if (item != 1) {
      item *= item;
      if (item >= mod)
        item %= mod;
      if (item == 0)
        break;
    } else if (mod == 1) {
      break;
    }
    sum += item;
  }
  return sum;
}

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

  unsigned long long result = getResult(N, X, M);
  cout << result;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long sum[100008];
long long pos[100008];
bool visited[100008];

int main() {
  long long n, m;
  long long x;
  cin >> n >> x >> m;
  long long res = 0, i = 2;
  long long prev = x;
  visited[x] = true;
  pos[x] = 1;
  sum[1] = x;

  if (n == 1)
    res = x;
  else {
    while (true) {
      long long cur = (prev * prev) % m;
      if (visited[cur]) {
        long long curr_pos = pos[cur];
        long long cycle_sum = sum[i - 1] - sum[curr_pos - 1];
        long long rem = n - (curr_pos - 1);
        long long cycle_size = i - curr_pos;
        long long d = rem / cycle_size;
        res += (sum[curr_pos - 1] + d * cycle_sum);
        long long excess = rem % cycle_size;
        if (excess)
          res += (sum[excess + curr_pos - 1] - sum[curr_pos - 1]);
        break;
      } else {
        visited[cur] = true;
        pos[cur] = i;
        sum[i] = sum[i - 1] + cur;
        ++i;
        prev = cur;
      }
      if (i > n) {
        res = sum[i - 1];
        break;
      }
    }
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

long long fun(long long x, long long m) { return (x * x) % m; }

int main() {
  long long n, m;
  long long x;
  cin >> n >> x >> m;
  long long res = 0;
  res = x;
  n--;
  while (n--) {
    long long d = fun(x, m);
    res += d;
    x = d;
  }
  cout << res << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  long long N;
  long long X, M, A;
  std::cin >> N >> X >> M;
  A = X;
  long long count = X;
  std::vector<long long> vec;
  vec.push_back(X);
  std::vector<long long>::iterator itr;
  long long a = 2;
  long long index = 0;
  for (; a <= N; a++) {
    X = (X * X) % M;
    count += X;
    itr = std::find(vec.begin(), vec.end(), X);

    if (itr != vec.end()) {
      index = std::distance(vec.begin(), itr);
      vec.push_back(X);
      break;
    }
    vec.push_back(X);
  }
  long long XXX = A;
  long long counttt = A;
  for (long long c = 2; c <= index + 1; c++) {
    XXX = (XXX * XXX) % M;
    counttt += XXX;
  }
  long long b = (N - a) % (a - index - 1);
  count = count + (count - counttt) * ((N - a - b) / (a - index - 1));
  for (long long c = 1; c <= b; c++) {
    X = (X * X) % M;
    count += X;
  }
  std::cout << count;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  long long N;
  long long X, M, A;
  std::cin >> N >> X >> M;
  A = X;
  long long count = X;
  std::vector<long long> vec;
  vec.push_back(X);
  std::vector<long long>::iterator itr;
  long long a = 2;
  long long index = 0;
  for (; a <= N; a++) {
    X = (X * X) % M;
    count += X;
    vec.push_back(X);
    itr = std::find(vec.begin(), vec.end(), X);
    if (itr == vec.end()) {
      index = std::distance(vec.begin(), itr);
      break;
    }
  }
  long long XXX = A;
  long long counttt = A;
  for (long long c = 2; c <= index + 1; c++) {
    XXX = (XXX * XXX) % M;
    counttt += XXX;
  }
  long long b = (N - a) % (a - index - 1);
  count = count + (count - counttt) * ((N - a - b) / (a - index - 1));
  for (long long c = 1; c <= b; c++) {
    X = (X * X) % M;
    count += X;
  }
  std::cout << count;
}
Problem p02550 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
#define rep(i, n) for (int i = 0; i < n; i++)
const ll mod = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  if (N <= M) {
    ll ans = X;
    rep(i, N - 1) {
      X = X * X % M;
      ans += X;
    }
    cout << ans << endl;
  } else {
    set<ll> check;
    check.insert(X);
    ll A = X;
    ll right = 1;
    rep(i, M) {
      A = A * A % M;
      right++;
      if (check.count(A)) {
        break;
      }
      check.insert(A);
      ;
    }
    ll left = 1;
    ll B = X;
    rep(i, M) {
      if (B == A) {
        break;
      }
      B = B * B % M;
      left++;
    }
    ll ans = 0;
    ll keep = X;
    rep(i, left - 1) {
      ans += keep;
      keep = keep * keep % M;
    }
    ll roopsum = 0;
    rep(i, right - left) {
      roopsum += keep;
      keep = keep * keep % M;
    }
    ans += roopsum * ((N - left + 1) / (right - left));
    rep(i, (N - left + 1) % (right - left)) {
      ans += keep;
      keep = keep * keep % M;
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
#define rep(i, n) for (int i = 0; i < n; i++)
const ll mod = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  if (N <= M) {
    ll ans = X;
    rep(i, N - 1) {
      X = X * X % M;
      ans += X;
    }
    cout << ans << endl;
  } else {
    vector<bool> check(N, false);
    check.at(X) = true;
    ll A = X;
    ll right = 1;
    rep(i, M) {
      A = A * A % M;
      right++;
      if (check.at(A)) {
        break;
      }
      check.at(A) = true;
    }
    ll left = 1;
    ll B = X;
    rep(i, M) {
      if (B == A) {
        break;
      }
      B = B * B % M;
      left++;
    }
    ll ans = 0;
    ll keep = X;
    rep(i, left - 1) {
      ans += keep;
      keep = keep * keep % M;
    }
    ll roopsum = 0;
    rep(i, right - left) {
      roopsum += keep;
      keep = keep * keep % M;
    }
    ans += roopsum * ((N - left + 1) / (right - left));
    rep(i, (N - left + 1) % (right - left)) {
      ans += keep;
      keep = keep * keep % M;
    }
    cout << ans << endl;
  }
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define mii map<int, int>
#define pii pair<int, int>
#define pq priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
int32_t main() {
  int n, x, m, a;
  cin >> n >> x >> m;
  vector<int> v;
  a = x % m;
  int ans = 0;
  rep(i, 0, n) {
    if (find(v.begin(), v.end(), a) != v.end())
      break;
    ans += a;
    v.pb(a);
    a = (a * a) % m;
    if (i == n - 1) {
      cout << ans;
      return 0;
    }
  }
  int p = find(v.begin(), v.end(), a) - v.begin();
  int count = 0, s = v.size();
  ans = 0;
  rep(i, 0, p) {
    ans += v[i];
    n--;
  }
  a = 0;
  rep(i, p, s) a = a + v[i];
  ans += a * (n / (s - p));
  count = n % (s - p);
  int i = p;
  while (count--)
    ans += v[i++];
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define mii map<int, int>
#define pii pair<int, int>
#define pq priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
int32_t main() {
  int n, x, m, a;
  cin >> n >> x >> m;
  vector<int> v;
  a = x % m;
  rep(i, 0, n) {
    if (find(v.begin(), v.end(), a) != v.end())
      break;
    v.pb(a);
    a = (a * a) % m;
  }
  int p = find(v.begin(), v.end(), a) - v.begin();
  int ans = 0, count = 0, s = v.size();
  rep(i, 0, p) {
    ans += v[i];
    n--;
  }
  a = 0;
  rep(i, p, s) a = a + v[i];
  ans += a * (n / (s - p));
  count = n % (s - p);
  int i = p;
  while (count--)
    ans += v[i++];
  cout << ans;
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long n, x, m;
struct node {
  long long num, v;
} f[100010];
long long l, r;
long long sum, ans;
bool cnt[100010];
int main() {
  scanf("%lld%lld%lld", &n, &x, &m);
  f[1].v = x;
  cnt[x] = 1;
  f[x].num = 1;
  l = 1, r = n;
  for (long long i = 2; i <= n; i++) {
    f[i].v = (f[i - 1].v * f[i - 1].v) % m;
    if (cnt[f[i].v]) {
      l = f[f[i].v].num;
      r = i - 1;
      break;
    }
    cnt[f[i].v] = 1;
    f[f[i].v].num = i;
  }
  for (long long i = 1; i < l; i++) {
    ans += f[i].v;
  }
  for (long long i = l; i <= r; i++) {
    sum += f[i].v;
  }
  ans += ((n - l + 1) / (r - l + 1)) * sum;
  long long x = (n - l + 1) % (r - l + 1);
  for (long long i = l; i < l + x; i++) {
    ans += f[i].v;
  }
  printf("%lld", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n, x, m;
struct node {
  long long num, v;
} f[100010];
long long l, r;
long long sum, ans;
int main() {
  scanf("%lld%lld%lld", &n, &x, &m);
  f[1].v = x;
  f[1].num = 1;
  l = 1, r = n;
  for (long long i = 2; i <= n; i++) {
    if (f[i].v) {
      l = f[i].num;
      r = i - 1;
    }
    f[i].v = f[i - 1].v * f[i - 1].v % m;
    f[i].num = i;
  }
  for (long long i = l; i <= r; i++) {
    sum += f[i].v;
  }
  ans += (n / (r - l + 1)) * sum;
  long long x = n % (r - l + 1);
  for (long long i = l; i < l + x; i++) {
    ans += f[i].v;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
int INF = 1e9;
ll LINF = 1e18;

int main() {
  ll n, m, x;
  cin >> n >> x >> m;
  vector<int> res(m, -1), a;
  int len = 0;
  ll tot = 0;
  while (res[x] == -1) {
    a.push_back(x);
    res[x] = len;
    tot += x;
    x = (x * x) % m;
    len++;
  }
  int peri = len - res[x];
  ll s = 0;
  for (int i = res[x]; i < len; i++)
    s += a[i];
  ll ans = 0;
  if (n <= len)
    rep(i, n) ans += a[i];
  else {
    ans += tot;
    n -= len;
    ans += s * (n / peri);
    n %= peri;
    rep(i, n) ans += a[res[x] + i];
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
int INF = 1e9;
ll LINF = 1e18;

int main() {
  ll n;
  int x, m;
  cin >> n >> x >> m;
  vector<int> res(100005);
  ll temp = x;
  ll len = 1;
  bool flag = false;
  while (len <= n) {
    temp %= m;
    // cout << temp << endl;
    if (res[temp] != 0) {
      flag = true;
      break;
    }
    res[temp] = len;
    temp *= temp;
    len++;
  }
  int peri = 0;
  if (flag)
    peri = len - res[temp];
  ll pans = 0;
  ll sans = 0;
  rep(i, m) {
    if (res[i] >= res[temp])
      pans += i;
    else if (res[i] > 0)
      sans += i;
  }
  if (!flag) {
    cout << sans + pans << endl;
    return 0;
  }
  // cout << pans << endl;
  pans *= (n - len + 1) / peri;
  ll r = (n - len + 1) % peri;
  ll gans = 0;
  rep(i, r) {
    temp %= m;
    gans += temp;
    temp *= temp;
  }
  cout << sans + pans + gans << endl;

  // rep(i,20) if(res[i]!=0) cout << len << endl;
  //  temp = x;
  //  rep(i,n%len){
  //  	temp %= m;
  //  	ans += temp;
  //  	temp *= temp;
  //  }
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long

ll N, X, M;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> X >> M;
  ll curr = X;
  vector<ll> path;
  path.push_back(curr);
  set<ll> vis;
  vis.insert(curr);
  ll hf, result = curr, req = N - 1;
  for (int i = 2; i <= N; i++) {
    curr = (curr * curr) % M;
    if (vis.find(curr) != vis.end()) {
      hf = curr;
      break;
    }
    result += curr;
    req -= 1;
    path.push_back(curr);
    vis.insert(curr);
  }
  ll ix = find(path.begin(), path.end(), hf) - path.begin();
  ll sz = path.size() - ix;
  ll score = accumulate(path.begin() + ix, path.end(), 0ll);
  if (req) {
    result += score * (req / sz);
    ll rem = (req % sz), iy = ix;
    while (rem--) {
      result += path[iy];
      iy += 1;
    }
  }
  cout << result << "\\n";
}

// 6 1 2 6 1 2

Error: -8

Fixed submission:

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

#define ll long long

ll N, X, M;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> X >> M;
  ll curr = X;
  vector<ll> path;
  path.push_back(curr);
  set<ll> vis;
  vis.insert(curr);
  ll hf, result = curr, req = N;
  for (int i = 2; i <= N; i++) {
    curr = (curr * curr) % M;
    result += curr;
    req -= 1;
    if (vis.find(curr) != vis.end()) {
      hf = curr;
      break;
    }
    path.push_back(curr);
    vis.insert(curr);
  }
  ll ix = find(path.begin(), path.end(), hf) - path.begin();
  ll sz = path.size() - ix;
  ll score = accumulate(path.begin() + ix, path.end(), 0ll);
  // cout << ix << " " << path.size() << "\\n";
  result += score * (req / sz);
  ll rem = req % sz, iy = ix;
  while (rem--)
    result += path[iy++];
  cout << result << "\\n";
}

// 6 1 2 1 2
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int main(void) {
  ll i, j, num, x, m, ans, a = 0, b = 0, k = 0;
  cin >> num >> x >> m;
  if (m == 1) {
    printf("0\\n");
    return 0;
  }
  vector<ll> d(m, 0), s(m, 0);
  rep(i, num) {
    k++;
    a += x;
    if (d[x]) {
      k -= s[x];
      b = a - d[x];
      a = d[x];
      int c = (num - s[x]) % k;
      k = (num - s[x]) / k;
      rep(j, c) {
        x *= x;
        x %= m;
        a += x;
      }
      break;
    }
    d[x] = a;
    s[x] = k;
    x *= x;
    x %= m;
  }
  if (i == num)
    ans = a;
  else
    ans = a + b * k;
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int main(void) {
  ll i, j, num, x, m, ans, a = 0, b = 0, k = 0;
  cin >> num >> x >> m;
  vector<ll> d(m, 0), s(m, 0);
  rep(i, num) {
    k++;
    a += x;
    if (d[x]) {
      k -= s[x];
      b = a - d[x];
      a = d[x];
      int c = (num - s[x]) % k;
      k = (num - s[x]) / k;
      rep(j, c) {
        x *= x;
        x %= m;
        a += x;
      }
      break;
    }
    d[x] = a;
    s[x] = k;
    x *= x;
    x %= m;
  }
  if (i == num)
    ans = a;
  else
    ans = a + b * k;
  cout << ans << "\\n";
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back

vll rem(1e5 + 5, 0), vec(1e5 + 5, 0);

int main() {
  ll n, x, m, first = 0, period = 0;
  cin >> n >> x >> m;
  ll sum = 0, ans = 0;

  for (ll i = 1; i <= m - 1; i++) {
    rem[i] = x;
    if (vec[x] != 0) {
      first = vec[x];
      period = i - first;
    }
    vec[x] = i;
    x *= x;
    x %= m;
  }
  if (first != 0) {
    for (ll i = first; i <= first + period - 1; i++)
      sum += rem[i];
  } else {
    if (m == 1) {
      ans = 0;
    } else {
      for (ll i = 1; i <= m - 1; i++)
        sum += rem[i];
      ans += sum * (n / (m - 1));
      n %= (m - 1);
      for (ll i = 1; i <= n; i++)
        ans += rem[i];
    }
    cout << ans << endl;
    return 0;
  }

  if (n <= first) {
    for (ll i = 1; i <= n; i++)
      ans += rem[i];
  } else {
    for (ll i = 1; i <= first - 1; i++)
      ans += rem[i];
    n -= first - 1;
    ans += sum * (n / period);
    n %= period;
    for (ll i = first; i <= first + n - 1; i++)
      ans += rem[i];
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back

vll rem(1e5 + 5, 0), vec(1e5 + 5, 0);

int main() {
  ll n, x, m, first = 0, period = 0;
  cin >> n >> x >> m;
  ll sum = 0, ans = 0;

  for (ll i = 1; i <= m - 1; i++) {
    rem[i] = x;
    if (vec[x] != 0) {
      first = vec[x];
      period = i - first;
    }
    vec[x] = i;
    x *= x;
    x %= m;
  }
  if (first != 0) {
    for (ll i = first; i <= first + period - 1; i++)
      sum += rem[i];
  } else {
    for (ll i = 1; i <= m - 1; i++)
      sum += rem[i];
    ans += sum * (n / (m - 1));
    n %= (m - 1);
    // for(ll i=1;i<=n;i++) ans+=rem[i];
    cout << ans << endl;
    return 0;
  }

  if (n <= first) {
    for (ll i = 1; i <= n; i++)
      ans += rem[i];
  } else {
    for (ll i = 1; i <= first - 1; i++)
      ans += rem[i];
    n -= first - 1;
    ans += sum * (n / period);
    n %= period;
    for (ll i = first; i <= first + n - 1; i++)
      ans += rem[i];
  }

  cout << ans << endl;
}