eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Encoded Coordinates

Encoded Coordinates

You have been monitoring a terrorist cell that is planning a big attack somewhere close. The terrorists have been sending encoded coordinates to each other, and you suspect that the attack will happen at one of the locations described by these coordinates. You have intercepted the relevant communications for a series of locations you want to investigate. Every location consists of a separate x and y coordinate, each being a non-negative integer smaller than some prime number \textbf{P}. Both coordinates are encoded separately using the same process. A source has given you information on the process of decoding a single coordinate. The input of this process consists of five values: \textbf{A}, \textbf{B}, \textbf{C}, \textbf{K} and \textbf{N}. Central to the decoding process is a collection of three functions, defined in terms of each other and the input value \textbf{K}: \textbf{F(n + 1) = G(n) + H(n)} \textbf{G(n + 1) = K*F(n) + H(n- 1)} \textbf{H(n + 1) = F(n) + K*G(n)} The values \textbf{A}, \textbf{B} and \textbf{C} are used to initialize these functions as follows: \textbf{F(1) = A G(1) = B H(1) = C} The decoded coordinate is the value of \textbf{F(N) mod P}. As you might have noticed, there is some crucial information missing: you need the value of \textbf{H(0)} to calculate \textbf{G(2)}. While you do not have this value, you do know that the \textbf{x} and \textbf{y} coordinate use the same value for \textbf{H(0)}. Luckily, you have managed to already obtain the x coordinates for the locations. Your colleagues have suggested that this does not give you enough information to calculate the y coordinates, but you want to prove them wrong. \InputFile On the first line one positive number: the number of test cases, at most \textbf{100}. After that per test case: \begin{itemize} \item one line with the prime number \textbf{P} (\textbf{2} ≤ \textbf{P} ≤ \textbf{19997}). \item one line with five space-separated integers \textbf{A_x}, \textbf{B_x}, \textbf{C_x}, \textbf{K_x} and \textbf{N_x} (\textbf{0} ≤ \textbf{A_x}, \textbf{B_x}, \textbf{C_x}, \textbf{K_x} < \textbf{P} and \textbf{1} ≤ \textbf{N_x} ≤ \textbf{10^9}): the parameters for the \textbf{x} coordinate. \item one line with five space-separated integers \textbf{A_y}, \textbf{B_y}, \textbf{C_y}, \textbf{K_y} and \textbf{N_y} (\textbf{0} ≤ \textbf{A_y}, \textbf{B_y}, \textbf{C_y}, \textbf{K_y} < \textbf{P} and \textbf{1} ≤ \textbf{N_y} ≤ \textbf{10^9}): the parameters for the y coordinate. \item one line with one integer \textbf{x} (\textbf{0} ≤ \textbf{x} < \textbf{P}): the \textbf{x} coordinate. \end{itemize} \OutputFile Per test case: \begin{itemize} \item one line with one integer: the calculated y coordinate. If your colleagues are right, and there is not exactly one valid y coordinate, print "\textbf{UNKNOWN}" on a single line instead. \end{itemize}
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
2
11
2 3 5 7 4
4 6 8 9 7
5
7
1 6 2 5 1
3 4 2 5 3
1
Выходные данные #1
7
UNKNOWN
Источник The 2013 Benelux Algorithm Programming Contest, BAPC 2013