Magic Bitstrings
Time limit1sMemory limit128 MB
Given a prime p, output the lexicographically smallest non-constant magic bitstring of length p-1, where each row of the modular index matrix must equal the string or its complement.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Array, Implementation
- Solved
- No attempts yet
Problem
A bitstring whose length is one less than a prime may be magic. For example, 1001 is such a string, since its length is one less than the prime .
To reveal the magic, append a single placeholder symbol x (which is not a bit) to the string, then treat the result as a cyclic string of length . Now build a square matrix of bits with rows: row (for ) lists every -th bit of the cyclic string, starting from the -th bit. That is, the -th entry of row is the bit at cyclic position .
For the string 1001 (so ) the matrix is:
The matrix has as many rows as the length of the original bitstring. Because the extended string has prime length , the appended x is never selected.
A bitstring is magic if every row of its matrix equals either the original bitstring or its bitwise complement. In the example every row is 1001 or its complement 0110, so 1001 is magic.
Input
Each line of input (except the last) contains a prime number . The last line contains a single 0, which must not be processed.
Output
For each prime from the input, print one line: the lexicographically smallest non-constant magic bitstring of length , if one exists; otherwise print Impossible.