#P1146. Flipping Coins Puzzle

    ID: 13541 Type: Default 1000ms 256MiB

Flipping Coins Puzzle

Flipping Coins Puzzle

There are N coins placed in a row on the desktop, all initially showing heads. In one operation, you are allowed to flip exactly N-1 coins (i.e. flip all coins except one). Flipping a coin changes it from heads to tails and vice versa.

Your task is to find a shortest sequence of operations that turns all coins to tails. In each operation, specify the coin that is not flipped. Note that a coin gets flipped in an operation if and only if it is not the one omitted in that operation. After a sequence of operations, each coin’s total number of flips must be odd (since they start heads and need to become tails).

Let the total number of operations be x and the number of times coin i is omitted be oi. Since every operation omits exactly one coin, we have \(\sum_{i=1}^{N} o_i = x\). Also, coin i is flipped \(x - o_i\) times, and we require \(x-o_i \equiv 1 \pmod{2}\) for each coin. It can be shown that these conditions can be satisfied if and only if N is even. For an even N, a valid minimal sequence is to perform exactly N operations by leaving out coin 1 in the first operation, coin 2 in the second, and so on.

If N is odd, output -1 to indicate it is impossible.

inputFormat

The input consists of a single integer N (1 ≤ N ≤ 109), representing the number of coins.

outputFormat

If a valid sequence of operations exists, first output an integer representing the minimum number of operations. Then output that many lines, each containing one integer which is the index (1-indexed) of the coin that is not flipped in that operation. If no solution exists, output -1.

sample

2
2

1 2

</p>