#P12807. [AMPPZ 2019] Fantastic compression

[AMPPZ 2019] Fantastic compression

Description

Franek had one job: to memorize a permutation P P of the sequence (1,2,,n) (1, 2, \ldots, n) . This, however, proved too boring. Instead, he compressed the numbers in a new, fantastic way he devised: he took a small integer k k and memorized only the sums of all connected k k -length fragments of P P . In other words, Franek now has a sequence S=(S1,S2,,Snk+1) S = (S_1, S_2, \ldots, S_{n-k+1}) , where:

  • S1=P1+P2++Pk S_1 = P_1 + P_2 + \ldots + P_k ,
  • S2=P2+P3++Pk+1 S_2 = P_2 + P_3 + \ldots + P_{k+1} ,
  • \ldots
  • Snk+1=Pnk+1+Pnk+2++Pn S_{n-k+1} = P_{n-k+1} + P_{n-k+2} + \ldots + P_n .

The method swiftly proved not-so-fantastic, though. First, Franek discovered, to his horror, that sometimes there are several permutations which all compress to the same sequence. Also, he is not sure anymore if he remembered the compressed sequence correctly – the initial permutation may now be lost forever!

Given a compressed sequence S S , help Franek find all permutations P P which correspond to S S .

Input Format

The first line of input contains the number of test cases z z (1z1000 1 \le z \le 1000 ). The test cases follow, each one in the following format:

The first line of a test case contains the length of the permutation n n and the small integer k k chosen by Franek (2n25000 2 \le n \le 25000 ; 2kmin(n,6) 2 \le k \le \min(n, 6) ). The second line contains nk+1 n - k + 1 integers: the elements of the compressed sequence S S (1Si1000000 1 \le S_i \le 1\,000\,000 ).

The total length of permutations in all test cases does not exceed 250,000.

Output Format

For every test case, output first the number c c of permutations that correspond to the given sequence S S . In the next c c lines, output these permutations in lexicographic order. Every permutation should be given as n n integers in a single line, separated by spaces.

Assume that for the given tests, c c is never greater than 1000.

2
5 3
8 10 12
5 3
10 10 10
2
1 2 5 3 4
2 1 5 4 3
0