#P15921. [TOPC 2023] Exponentiation

    ID: 29377 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学2023Special Judge矩阵加速ICPC台湾

[TOPC 2023] Exponentiation

题目描述

Exponentiation is a mathematical operation that involves raising a base number to a certain exponent to obtain a result. In the expression ana^n, where aa is the base and nn is the exponent, it means multiplying aa by itself nn times. The result of this operation is called the exponentiation of aa to the nn-th power. For examples, 23=2×2×2=82^3 = 2 \times 2 \times 2 = 8 and 52=5×5=255^2 = 5 \times 5 = 25. In these examples, 2 is the base, 3 is the exponent in the first case, and 5 is the base, and 2 is the exponent in the second case. Exponentiation is a fundamental operation in mathematics and is commonly used in various contexts, such as solving equations, and cryptography.

In many cryptographic algorithms, particularly those based on number theory like RSA (Rivest-Shamir-Adleman) and Diffie-Hellman, modular exponentiation is a fundamental operation. Modular exponentiation involves raising a base to an exponent modulo a modulus. This operation is computationally intensive but relatively easy to perform, even for very large numbers.

Let x+1x=αx + \frac{1}{x} = \alpha where α\alpha is a positive integer. Please write a program to compute xβ+1xβmodmx^\beta + \frac{1}{x^\beta} \mod m for given positive integers β\beta and mm.

输入格式

The input has only one line, and it contains three space-separated positive integers α\alpha, β\beta and mm.

输出格式

Output xβ+1xβmodmx^\beta + \frac{1}{x^\beta} \mod m. If there are multiple solutions, you may output any of them in the range from 00 to m1m - 1.

1 2 3
2
5 4 321
206
3 3 333
18
8 8 888

626

提示

Note

xx can be a complex number. For example, xx is either 1+3i2\frac{1+\sqrt{3}i}{2} or 13i2\frac{1-\sqrt{3}i}{2} if α=1\alpha = 1. However, xβ+1xβx^\beta + \frac{1}{x^\beta} is always an integer in this problem.

α\alpha, β\beta and mm are positive integers less than 2642^{64}. You may assume xβ+1xβx^\beta + \frac{1}{x^\beta} is an integer.