Encoded Coordinates
Time limit5sMemory limit128 MB
Given two linear recurrences sharing one missing initial value and the first output, find the second output or report UNKNOWN when it is not unique.
- Level
Medium7 of 10
- Topics
- Matrix, Number theory
- Solved
- No attempts yet
Problem
Sangkeun was tapping a terrorist's phone when he learned that a large attack had been announced. The terrorists send encoded coordinates to each other, and the attack will happen at one of them. Sangkeun managed to intercept a coordinate.
A coordinate splits into an x value and a y value, and both are non-negative integers smaller than a prime . The x value and the y value are encoded separately, by the same procedure.
Encoding one value needs , , , , . The procedure is written with three functions.
, , are the initial values of the functions.
The value becomes .
One very important piece of information is missing from the procedure above. Computing needs , and nothing reveals . All that is known about it is that the same is used when computing x and when computing y. is also a non-negative integer smaller than .
Sangkeun happened to get the x value without decoding it. Write a program that recovers from x and then computes y.
Input
The first line contains the number of test cases, which is at most 100.
Each test case takes four lines. The first line contains a prime (). The second line contains , , , , , the values used to encode the x value (, ). The third line contains , , , , , the values used to encode the y value (, ). The fourth line contains the intercepted x value ().
The intercepted x value really came out of the encoding, so at least one produces it.
Output
For each test case, print the y value on its own line. If several produce the given x value and they do not all lead to the same y value, print UNKNOWN instead of the y value.