Under construction

Still being built. Things may change

RSA

Time limit2sMemory limit512 MB

Summary
Factor N into its two primes to recover phi(N), then use modular inverse and fast exponentiation to decrypt C and print M.
Level

Medium5 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

RSA is one of the most widely used public key encryption algorithms. Its basic operation is described below.

Pick two distinct odd primes PP and QQ and compute N=PQN = PQ. Next compute the totient φ(N)=(P−1)(Q−1)\varphi(N) = (P - 1)(Q - 1) and pick an integer EE with 1<E<φ(N)1 < E < \varphi(N) and gcd⁡(φ(N),E)=1\gcd(\varphi(N), E) = 1. Finally compute DD, the multiplicative inverse of EE modulo φ(N)\varphi(N), that is the integer DD with DE≡1(modφ(N))DE \equiv 1 \pmod{\varphi(N)}.

This gives the public key, formed by the two integers NN and EE, and the secret key, formed by the two integers NN and DD.

To encrypt a message MM with 0<M<N0 < M < N, compute C=ME mod NC = M^E \bmod N, and CC is the encrypted message. To decrypt it and recover the original message, compute M=CD mod NM = C^D \bmod N. Decryption needs the secret key, and the public key alone is not enough. The expression x≡1(mody)x \equiv 1 \pmod{y} used above means that the remainder of xx divided by yy is 1.

In this problem you break RSA. Write a program that recovers the original message MM from the public key NN, EE and the ciphertext CC alone.

Input

The single line of input contains three integers NN, EE, CC separated by spaces, where 15≤N≤10915 \le N \le 10^9, 1≤E<N1 \le E < N and 1≤C<N1 \le C < N.

NN and EE form the RSA public key described above, and CC is a message encrypted with that public key. NN is a product of two distinct odd primes, and gcd⁡(φ(N),E)=1\gcd(\varphi(N), E) = 1 always holds.

Output

Print a single line containing the original message MM, where 1≤M<N1 \le M < N.

Examples2

  1. Example 1

    Input
    1073 71 436
    
    Expected output
    726
    
  2. Example 2

    Input
    91 43 19
    
    Expected output
    33