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: Wrong Answer)

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL (1LL<<62) // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
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; }
using namespace std;

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

    // 累積和と出現位置
    vi sum(1, 0), pos(m+1, -1);
    sum[0] = x, pos[x] = 0;

    for(ll i=1; i<n; i++){
        x = (x*x)%m;
        sum.push_back(sum[i-1] + x);
        // これまでに出現していない場合
        if(pos[x] == -1) pos[x] = i;
        // これまでに出現している場合はループ長を検出
        else{
            // 1ループ当たりの長さとその区間和
            ll loopSize = i-pos[x];
            ll loopSum = sum[i] - sum[pos[x]];

            // 最後までに何回ループが含まれるかと端数分の処理
            ll loopTotal = ((n-(i-loopSize))/loopSize)*loopSum;
            ll fraction = (pos[x]-1 < 0 ? sum[pos[x]] : sum[pos[x]-1+(n-(i-loopSize))%loopSize]-sum[pos[x]-1]);
            cout << (pos[x]-1 < 0 ? 0 : sum[pos[x]-1])+loopTotal+fraction << endl;
            return 0;
        }
    }
    // ループがなかった場合
    cout << sum[n-1] << endl;
}