Exponentiation
InterviewTime limit3sMemory limit1024 MB
Given alpha, beta, and m, compute x^beta + 1/x^beta mod m, where x + 1/x = alpha and all inputs are below 2^64.
- Level
Medium5 of 10
- Topics
- Math, Matrix, Number theory
- Solved
- No attempts yet
Problem
Exponentiation is a basic mathematical operation that raises a base number to an exponent to obtain a result. In the expression , is the base and is the exponent, meaning is multiplied by itself times. The result is called the -th power of . For example, and . In the first case, the base is and the exponent is . In the second case, the base is and the exponent is . Exponentiation is used in many areas, such as solving equations and cryptography.
In many cryptographic algorithms based on number theory, such as RSA (Rivest-Shamir-Adleman) and Diffie-Hellman, modular exponentiation is a basic operation. Modular exponentiation raises a base to an exponent and then takes the remainder modulo a modulus. It is computationally heavy, but it can be carried out fairly quickly even for very large numbers.
Let , where is a positive integer. Write a program that computes for given positive integers and .
Input
The input is one line with three space-separated positive integers , , and . Each of , , and is less than .
Output
Output . You may assume that is an integer. If there are multiple solutions, you may output any of them in the range from to .
Hint
can be a complex number. For example, if , then is either or . However, is always an integer in this problem.