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 Kotlin (1.3.71) (judge verdict: Accepted)

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val br = BufferedReader(InputStreamReader(System.`in`))
val out = PrintWriter(System.out)

fun main() {
    br.use { out.use {
        solve()
        out.flush()
    } }
}

fun solve() {
//    var pre = 2
//    var aaaaaa = 0
////    out.println("1:" +pre)
//    out.println(pre)
//    for (i in 1..12) {
//        aaaaaa += pre
//        val tmp = pre * pre % 1001
//
//
//        out.println("${i}:" + aaaaaa)
//        out.println(tmp)
//        pre = tmp
//    }
////    out.println(aaaaaa)
//    out.println()

    val (n, x, m) = readLongList()

    val set = mutableSetOf<Long>()
    var cur = x
    while (set.contains(cur) == false) {
        set.add(cur)
        cur = (cur % m) * (cur * m) % m
    }

    val a = set.max()!!
    var loopFrom = 1L
    val set2 = mutableSetOf<Long>()
    var cur2 = x
    while (set2.contains(cur2) == false) {
        set2.add(cur2)
        cur2 = (cur2 % m) * (cur2 % m) % m
        if (cur2 == a) break
        loopFrom++
    }
    loopFrom++

    var firstSum = 0L
    cur2 = x
    repeat(loopFrom.toInt()) {
        firstSum += cur2
        val tmp = (cur2 % m) * (cur2 % m) % m
        cur2 = tmp
    }


    var loopSum = 0L
    var loopCount = 0L
    set2.clear()
    val first = cur2
    while (set2.contains(cur2) == false) {
        loopCount++
        set2.add(cur2)
        val tmp = (cur2 % m)* (cur2 % m) % m
        loopSum += tmp
        cur2 = tmp
    }
//    out.println()
//    out.println(loopFrom)
//    out.println(firstSum)
//
//    out.println(loopCount)
//    out.println(loopSum)

    if (n <= loopFrom) {
        var ans = 0L
        var pre = x
        repeat(n.toInt()) {
            ans += pre
            pre = (pre % m) * (pre % m) % m
        }
        out.println(ans)
        return
    } else {
        var ans = firstSum

        val remain = max(0, n - loopFrom)
        val aaa = remain / loopCount
        val modRemain = remain % loopCount

        ans += aaa * loopSum

        var pre = first
        repeat(modRemain.toInt()) {
            ans += pre
            pre = (pre%m) * (pre%m) % m
        }
        out.println(ans)
        return
    }
}

fun readInteger() = Integer.parseInt(br.readLine())
fun readLong() = java.lang.Long.parseLong(br.readLine())
fun readStringList() = br.readLine()!!.split(' ')
fun readIntegerList() = readStringList().map(Integer::parseInt)
fun readLongList() = readStringList().map(java.lang.Long::parseLong)
fun readDoubleList() = readStringList().map(java.lang.Double::parseDouble)