#P12807. [AMPPZ 2019] Fantastic compression
[AMPPZ 2019] Fantastic compression
Description
Franek had one job: to memorize a permutation of the sequence . This, however, proved too boring. Instead, he compressed the numbers in a new, fantastic way he devised: he took a small integer and memorized only the sums of all connected -length fragments of . In other words, Franek now has a sequence , where:
- ,
- ,
- .
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 , help Franek find all permutations which correspond to .
Input Format
The first line of input contains the number of test cases (). The test cases follow, each one in the following format:
The first line of a test case contains the length of the permutation and the small integer chosen by Franek (; ). The second line contains integers: the elements of the compressed sequence ().
The total length of permutations in all test cases does not exceed 250,000.
Output Format
For every test case, output first the number of permutations that correspond to the given sequence . In the next lines, output these permutations in lexicographic order. Every permutation should be given as integers in a single line, separated by spaces.
Assume that for the given tests, 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
京公网安备 11011102002149号