Modular Inverse
InterviewTime limit2sMemory limit1024 MB
Given a prime m and a number a with 1 at most a less than m, find b in [1, m) with a times b leaving remainder 1 modulo m.
- Level
Easy3 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
The day before yesterday, Roma learned a new math operation: taking the remainder modulo , written . For example, . Here means with , where all numbers are integers.
Yesterday Roma started multiplying numbers and noticed that sometimes , even when neither number is 1. He found this interesting and named it: and are called mutual inverses modulo .
This morning Roma looked at prime numbers . He proved that for every with , there is exactly one such that . However, he does not know how to find from and . Help him!
Input
A single line contains the numbers and (). The number is prime.
Output
Print the number such that . It must also satisfy .