TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Homework to primary school children often becomes primary home engagement for
parents. In order to lessen the burden of parents a software firm proposes to develop a
package that includes solutions to problems on Arithmetic, which are commonly given as
homework. As a programmer of the software firm you are required to write a program to solve
such a problem.


The problem is to add triplets of integers from a given set S of n (≤30) distinct positive
integers and determine all K subsets {a b c d e f} of six distinct elements in S so that the sum
of three elements in the subset, say (a+b+c), is equal to the sum (d+e+f) of the other three.


For example, given the set S={1001 2001 3001 4001 5001 6001 7001} there exists
exactly three subsets (K=3): {1001 2001 3001 4001 5001 7001}, {1001 2001 3001 5001 6001
7001} and {1001 3001 4001 5001 6001 7001} each one of which satisfies the stated
condition.

Input Format

Input may contain multiple test cases. Each test case contains two lines.


The first line gives two integers: n, the total number of elements in S and I, an indicator that indicates the output format. The indicator I, is either 0 or 1.


The second line gives n elements of S in an arbitrary order.

Input terminates with an input 0 as the first input for a test case.

Output Format

For each test case, print output in either of two formats.
If the indicator I is 0 then print only the total number K of subsets in one line.


Otherwise the line is followed by K more lines each containing elements of a subset.


The elements of each subset appear in ascending order and the subsets appear in lexicographic order.

Sample Input 1

6 0 
50 22 87 180 65 115 
7 1 
1001 2001 3001 4001 5001 6001 7001 
8 0 
12 20 35 38 46 23 18 58 
0

Sample Output 1

0
3
1001 2001 3001 4001 5001 7001
1001 2001 3001 5001 6001 7001
1001 3001 4001 5001 6001 7001
4

Hints

Problem Source

Migrated from old NTUJ.

ICPC Kanpur, 2008

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 10000 65536 2048