E - Geometric Progression Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^iM で割った余りを求めてください。

制約

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

A X M

出力

答えを出力せよ。


入力例 1

3 4 7

出力例 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40 です。407 で割った余りは 5 であるため、5 を出力します。


入力例 2

8 10 9

出力例 2

0

入力例 3

1000000000 1000000000000 998244353

出力例 3

919667211

Score : 500 points

Problem Statement

Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.

Constraints

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

A X M

Output

Print the answer.


Sample Input 1

3 4 7

Sample Output 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.


Sample Input 2

8 10 9

Sample Output 2

0

Sample Input 3

1000000000 1000000000000 998244353

Sample Output 3

919667211