Modular Reverse Engineering
Time limit2sMemory limit512 MB
Given v, x, and prime m, find the smallest p with a matching q so that p/q lies in [x, x+1) and p/q equals v modulo m.
- Level
Medium7 of 10
- Topics
- Number theory, Math, Binary search, Implementation
- Solved
- No attempts yet
Problem
Sometimes a competitive programming problem whose output is a rational number asks you to output the quantity instead, for some prime modulus . This quantity is hard to debug when your program gives the wrong answer, because computing by hand is difficult and the value can be very large even for small rationals. For example, .
To debug your solution quickly, you want to write a program that recovers and from . The possible values of and are not unique, but one piece of information narrows them down: , read as an ordinary rational number, lies in the range for some integer .
Given the three values , , and , find the minimum possible and the corresponding such that () and .
Input
The input is a single line with three space-separated integers , , and , where , , , and is prime.
Output
Print the minimal integer and the corresponding such that , , and . If several such pairs exist, print the pair with the minimum value of . If no such and exist, print a single instead.