0159. Strange Counter

Input file name: counter.in
Output file name: counter.out
Time limit: 2 s
Memory limit: 32 megabytes

The new secret counter invented in one theoretical computer science lab is the great breakthrough in the computer microschemes design. This counter consists of N registers numbered from 0 to N-1, each of which contains 0, 1 or 2. If the number in the i-th register is Ai, the number stored in the counter is Σi=0N-1Ai 2i.

One can see that the same number can be stored in the counter in several different ways. For example, the number 5 can be stored in a 3-register counter as (1, 0, 1) or as (0, 2, 1).

The main feature of the counter is that it can add numbers that are powers of two to the number stored in the counter, only changing the value of a small number of registers. Namely, the scientists of the lab developed the scheme that allowed adding such number changing no more than four registers!

Unfortunately after the recent experiments in the neighbouring physics lab, involving the creation of the artificial black hole, the theoretical computer science laboratory was accidently destroyed. However, the supercomputer project that the counter was designed for is still on, so you were asked to reinvent the counter.

Input file

The first line of the input file contains N – the number of registers in the counter (1 ≤ N ≤ 1,000). Initially all registers contain zeroes. The second line contains M – the number of additions you have to make (1 ≤ M ≤ 10,000). The third line contains M integer numbers ranging from 0 to N-1. Number i means that you must add 2i to the number in the counter. There sum of all numbers added to the register does not exceed 2N - 1.

Output file

Output file must contain M lines. Each line must contain the changes made adding the corresponding number to the counter.

The first number in each line must be K – the number of registers changed (1 ≤ K ≤ 4). K pairs must follow – for each changed register first output its number and then the new value.

Examples:

counter.incounter.out
5 6 0 0 0 1 0 0 1 0 1 1 0 2 2 0 1 1 1 1 1 2 1 0 2 3 0 1 1 1 2 1


Source: Petrozavodsk Summer 2003. Final Contest, Saturday, August 30
Author: Andrew Stankevich

Discuss       Submit a solution



Printable version