Wavelet Compression

Time limit1sMemory limit128 MB

Summary
Given a signal transformed by recursive sum/difference wavelet steps, reconstruct the original samples for each test case.
Level

Easy3 of 10

Topics
Divide and conquer, Simulation, Implementation
Solved
No attempts yet

Problem

The discrete wavelet transform is a popular tool for signal compression. Your task is to decompress a one-dimensional signal (a list of integers) that was compressed by the simple wavelet transform described below.

To see how this simple wavelet transform works, suppose we have a list containing an even number of integers. We compute the sum and the difference of each pair of consecutive samples, producing a list of sums and a list of differences, each half the original length. Formally, if the original samples are

a(1), ..., a(n)

then for i=1,…,n/2i = 1, \dots, n/2 the ii-th sum s(i)s(i) and difference d(i)d(i) are

s(i)=a(2i−1)+a(2i)s(i) = a(2i-1) + a(2i)

d(i)=a(2i−1)−a(2i)d(i) = a(2i-1) - a(2i)

The transformed signal is formed by listing all the sums first, followed by all the differences. For instance, if the input signal is

5, 2, 3, 2, 5, 7, 9, 6

then the sums and differences are

s(i) = 7, 5, 12, 15
d(i) = 3, 1, -2, 3

so the transformed signal is

7, 5, 12, 15, 3, 1, -2, 3

The same process is then applied recursively to the first half of the transformed signal (the sums), treating those sums as a new input signal, and it continues until the signal being transformed has length 1. For the example above, the final transformed signal is

39, -15, 2, -3, 3, 1, -2, 3

The length of the original signal is a power of 2, and every original sample is an integer between 0 and 255 inclusive. Given a transformed signal, recover the original samples.

Input

The input consists of several test cases. Each test case is given on its own line: it begins with an integer NN (1≤N≤2561 \le N \le 256, a power of 2) giving the number of samples, followed by the NN transformed samples. The input ends with a line containing N=0N = 0, which is not processed.

Output

For each test case, print the original samples on a single line, separated by single spaces.

Examples3

  1. Example 1

    Input
    8 39 -15 2 -3 3 1 -2 3
    4 10 -4 -1 -1
    0
    
    Expected output
    5 2 3 2 5 7 9 6
    1 2 3 4
    
  2. Example 2

    Input
    1 42
    0
    
    Expected output
    42
    
  3. Example 3

    Input
    2 255 -255
    0
    
    Expected output
    0 255