Language: C++

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout << fixed << setprecision(20);
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
typedef long long ll;
using namespace std;
ll N, X, M;
int S[100005];

int main() {
  INCANT;
  cin >> N >> X >> M;
  ll idx=0ll;
  ll cur = X;
  ll start, sz;
  vector<ll> A;
  S[X] = 0;
  A.push_back(X);
  ll ret=X;
  while(idx<min(M+1, N)) {
    ll n = pow(cur,2ll);
    n%=M;
    if (n==0ll) {
      cur=0ll;
      break;
    }
    if (S[n]>0) {
      start = S[n];
      sz = idx - S[n]+1;
      break;
    }
    idx++;
    S[n]=idx;
    A.push_back(n);
    ret+=n;
    cur=n;
  }
  if (cur==0 || idx==N-1) {
    cout << ret << endl;
    return 0;
  }
  ret = 0ll;
  ll tmp=0ll;
  rep(i, start) tmp+=A[i];
  rep(i, sz) ret+=A[start+i];
  
  ll cir = (N-start)/sz;
  ret*=cir;
  ret+=tmp;
  rep(i, (N-start)%sz) ret+=A[start+i];
  cout << ret << "\n";
  return 0;
}#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout << fixed << setprecision(20);
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
typedef long long ll;
using namespace std;
ll N, X, M;
int S[100005];

int main() {
  INCANT;
  cin >> N >> X >> M;
  ll idx=0ll;
  ll cur = X;
  ll start, sz;
  vector<ll> A;
  S[X] = 0;
  A.push_back(X);
  ll ret=X;
  while(idx<min(M+1, N)) {
    ll n = pow(cur,2ll);
    n%=M;
    if (n==0ll) {
      cur=0ll;
      break;
    }
    if (S[n]>0) {
      start = S[n];
      sz = idx - S[n]+1;
      break;
    }
    idx++;
    S[n]=idx;
    A.push_back(n);
    ret+=n;
    cur=n;
  }
  if (cur==0 || idx==N-1) {
    cout << ret << endl;
    return 0;
  }
  ret = 0ll;
  ll tmp=0ll;
  rep(i, start) tmp+=A[i];
  rep(i, sz) ret+=A[start+i];
  
  ll cir = (N-start)/sz;
  ret*=cir;
  ret+=tmp;
  rep(i, (N-start)%sz) ret+=A[start+i];
  cout << ret << "\n";
  return 0;
}

Compiler output:
a.cc:65:2: error: stray '#' in program
   65 | }#include <algorithm>
      |  ^
a.cc:65:3: error: 'include' does not name a type
   65 | }#include <algorithm>
      |   ^~~~~~~
a.cc:83:4: error: redefinition of 'll N'
   83 | ll N, X, M;
      |    ^
a.cc:19:4: note: 'll N' previously declared here
   19 | ll N, X, M;
      |    ^
a.cc:83:7: error: redefinition of 'll X'
   83 | ll N, X, M;
      |       ^
a.cc:19:7: note: 'll X' previously declared here
   19 | ll N, X, M;
      |       ^
a.cc:83:10: error: redefinition of 'll M'
   83 | ll N, X, M;
      |          ^
a.cc:19:10: note: 'll M' previously declared here
   19 | ll N, X, M;
      |          ^
a.cc:84:5: error: redefinition of 'int S [100005]'
   84 | int S[100005];
      |     ^
a.cc:20:5: note: 'int S [100005]' previously declared here
   20 | int S[100005];
      |     ^
a.cc:86:5: error: redefinition of 'int main()'
   86 | int main() {
      |     ^~~~
a.cc:22:5: note: 'int main()' previously defined here
   22 | int main() {
      |     ^~~~