Code Sequence (Large)
Time limit5sMemory limit512 MB
Given consecutive terms of a binary-additive sequence with unknown coefficients, output the next term or UNKNOWN if it is not forced.
- Level
Hard8 of 10
- Topics
- Math, Bit manipulation, Number theory
- Solved
- No attempts yet
Problem
A sequence is generated from a secret code, and you have to work out its next term. The code was built as follows.
First, for each from to , a number between and inclusive was chosen.
Then, for every integer from to inclusive:
- Write in binary.
- Collect for every bit that is set in that binary representation. For , bits 0 and 2 are set, so and are collected.
- Add the collected values, divide by , and let the remainder be .
You are given several consecutive terms of . You do not know where in the sequence they start, though you do know that at least one more term follows them, and you do not know which were chosen.
Print the term that comes right after the given ones. If the input does not pin it down to a single value, print UNKNOWN.
Input
The first line contains the number of test cases .
Each test case takes two lines.
- The first line contains , the number of known terms.
- The second line contains the known terms, separated by single spaces. Each term is between and .
Limits
- Every test case is a consecutive block of some sequence the procedure above can produce, and at least one more term follows the last given one. That is, there are and an index with such that the given terms are in this order.
Output
For each test case print one line in the form Case #: , where is the test case number starting from 1 and is the next term. Write UNKNOWN in place of when the next term is not determined.
Notes
In the first test case of the sample input, could have been 1, 2 and 4, with the given terms starting at . Read that way, is unknown, so the next term could be anything and the answer is UNKNOWN.
In the second test case neither the full list of nor the starting index can be recovered. Even so, whenever 1, 10, 11, 200 occur consecutively in this order, the next value is always 201.