AtCoder Beginner Contest 179
E
Sequence Sum
AtCoder
⌀ 1175
原题
← DF →
翻译中…

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}$.

翻译中…

样例

Input #1
6 2 1001
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$.
Input #2
1000 2 16
Output #2
6
The sequence $A$ begins $2,4,0,0,\ldots$ Therefore, the answer is $6$.
Input #3
10000000000 10 99959
Output #3
492443256176507
题目信息
题号
abc179_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。