Magic Bitstrings

Time limit1sMemory limit128 MB

Summary
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 44 is one less than the prime 55.

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 pp. Now build a square matrix of bits with p−1p-1 rows: row mm (for 1≤m≤p−11 \le m \le p-1) lists every mm-th bit of the cyclic string, starting from the mm-th bit. That is, the kk-th entry of row mm is the bit at cyclic position (k⋅m) mod p(k \cdot m) \bmod p.

For the string 1001 (so p=5p = 5) the matrix is:

rowbits
every 1st bit1001
every 2nd bit0110
every 3rd bit0110
every 4th bit1001

The matrix has as many rows as the length of the original bitstring. Because the extended string has prime length pp, 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 p≤100000p \le 100000. The last line contains a single 0, which must not be processed.

Output

For each prime pp from the input, print one line: the lexicographically smallest non-constant magic bitstring of length p−1p-1, if one exists; otherwise print Impossible.

Examples3

  1. Example 1

    Input
    5
    3
    17
    47
    2
    79
    0
    
    Expected output
    0110
    01
    0010111001110100
    0000100001101010001101100100111010100111101111
    Impossible
    001001100001011010000001001111001110101010100011000011011111101001011110011011
    
  2. Example 2

    Input
    2
    0
    
    Expected output
    Impossible
    
  3. Example 3

    Input
    3
    0
    
    Expected output
    01