Factor N into its two primes to recover phi(N), then use modular inverse and fast exponentiation to decrypt C and print M.
Medium5Number theoryMathNo attempts yetTime limit2sMemory limit512 MBRSA is one of the most widely used public key encryption algorithms. Its basic operation is described below.
Pick two distinct odd primes P and Q and compute N=PQ. Next compute the totient φ(N)=(P−1)(Q−1) and pick an integer E with 1<E<φ(N) and gcd(φ(N),E)=1. Finally compute D, the multiplicative inverse of E modulo φ(N), that is the integer D with DE≡1(modφ(N)).
This gives the public key, formed by the two integers N and E, and the secret key, formed by the two integers N and D.
To encrypt a message M with 0<M<N, compute C=MEmodN, and C is the encrypted message. To decrypt it and recover the original message, compute M=CDmodN. Decryption needs the secret key, and the public key alone is not enough. The expression x≡1(mody) used above means that the remainder of x divided by y is 1.
In this problem you break RSA. Write a program that recovers the original message M from the public key N, E and the ciphertext C alone.
The single line of input contains three integers N, E, C separated by spaces, where 15≤N≤109, 1≤E<N and 1≤C<N.
N and E form the RSA public key described above, and C is a message encrypted with that public key. N is a product of two distinct odd primes, and gcd(φ(N),E)=1 always holds.
Print a single line containing the original message M, where 1≤M<N.