Problem:
Score : 500 points

Problem Statement

Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).
Find \displaystyle{\sum_{i=1}^N A_i}.

Constraints

1 \leq N \leq 10^{10}

0 \leq X < M \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X M

Output

Print \displaystyle{\sum_{i=1}^N A_i}.

Sample Input 1

6 2 1001

Sample Output 1

1369

The sequence A begins 2,4,16,256,471,620,\ldots Therefore, the answer is 2+4+16+256+471+620=1369.

Sample Input 2

1000 2 16

Sample Output 2

6

The sequence A begins 2,4,0,0,\ldots Therefore, the answer is 6.

Sample Input 3

10000000000 10 99959

Sample Output 3

492443256176507

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include"bits/stdc++.h"
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using ll=long long; 
using ld=long double; 
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int mod1=1e9+7; 
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x+FIXED_RANDOM);
    }
};
int main()
{
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  ll n,x,m; cin>>n>>x>>m; unordered_map<int,int,custom_hash>g; 
  ll ans=0; 
  vector<int>v; int i1=-1,i2=-1; 
  rep(i,1,1000000)
  {
    if(g[x]!=0){ i1=g[x]; i2=i-1;  break; } g[x]=i; v.emplace_back(x); x=(x*1ll*x)%m; 
  }
  assert(i1!=-1); 
  ll sum=0; rep(i,i1-1,i2-1) sum+=v[i];      
  if(i1!=1){ rep(i,0,i1-2){ ans+=v[i]; } n-=(i1-1); } 
  ll rem=n%(i2-i1+1); 
  if(rem!=0)
  {
    int c=0; int idx=i1-1; 
    while(c!=rem)
    {
      ans+=v[idx]; idx++; c++; 
    }
  }
  ans+=(n/(i2-i1+1))*1ll*sum; cout<<ans<<"\n"; 
}