RNG
Time limit1sMemory limit128 MB
Given y and a, b, c, n, find all x in [0, 2^n) with a x^2 + b x + c = y mod 2^n, printing x only when exactly one solution exists.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
Hyunsu is making a new adventure game featuring pirates and monkeys. To drive its many random elements, the game uses a random number generator, RNG for short.
Given the previous random number , the RNG produces the next number by
where , , , and are integers.
While debugging, Hyunsu sees the current random number in the debug console and wants to recover the number that was generated immediately before it.
Given the current number and the constants , , , , find the previous number () satisfying the equation above. If exactly one such exists, print it; if none exists or two or more exist, print No unique solution.
Input
The first line contains the number of test cases .
Each test case is given on one line as five space-separated integers , , , , . (, )
Output
For each test case, print the previous number satisfying the equation, one per line.
If such an is not unique (there is none, or there are two or more), print No unique solution instead.