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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Linq;
using CompLib.Util;
using System.Threading;

public class Program
{
    long N, X, M;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextLong();
        X = sc.NextInt();
        M = sc.NextInt();

        var table = new int[M];
        long tmp = X;
        int e = 1;
        while (true)
        {
            if (table[tmp] != 0) break;
            table[tmp] = e;
            // Console.WriteLine($"{tmp} {e}");
            tmp *= tmp;
            tmp %= M;
            e++;
        }

        // ループまで
        // t[tmp]-1回
        // 開始 t[tmp]
        // ループ e-t[tmp]
        int b = table[tmp] - 1;

        long f = table[tmp];
        int loop = e - table[tmp];

        // Console.WriteLine($"{ e} {tmp}");

        long ans = 0;

        long tmp2 = X;
        for (int i = 0; i < b && N >= 0; i++, N--)
        {
            ans += tmp2;
            tmp2 *= tmp2;
            tmp2 %= M;
        }
        // Console.WriteLine(ans);
        // Console.WriteLine(b);
        // Console.WriteLine(tmp2);
        if(N == 0)
        {
            Console.WriteLine(ans);
            return;
        }

        long l = 0;
        for (int i = 0; i < loop; i++)
        {
            l += tmp2;
            tmp2 *= tmp2;
            tmp2 %= M;
        }
        
        long d = N / loop;
        long m = N % loop;
        ans += d * l;
        for (int i = 0; i < m; i++)
        {
            ans += tmp2;
            tmp2 *= tmp2;
            tmp2 %= M;
        }

        Console.WriteLine(ans);
        // Console.WriteLine(l);
    }

    public static void Main(string[] args) => new Program().Solve();
    // public static void Main(string[] args) => new Thread(new Program().Solve, 1 << 27).Start();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}