All problems

Problem 730

Combination Sum III

Medium · 20 pts · Recursion, Backtracking

Problem

Find every combination of exactly K different numbers from 1 to 9 whose sum is exactly N. Each number may be used at most once in a combination. Write each combination in increasing order and list them in lexicographic order, after printing how many there are. Backtrack over the numbers 1 to 9 in increasing order, stopping a branch as soon as it has K numbers or its sum exceeds N.

Input

A single line with K and N.

Output

Print the number of combinations, then each combination on its own line in lexicographic order.

Constraints

1 <= K <= 9; 1 <= N <= 60

Example input

3 9

Example output

3
1 2 6
1 3 5
2 3 4

Explanation

For K = 3 and N = 9, the combinations are 1 2 6, 1 3 5 and 2 3 4. No other three different digits add up to 9, so there are 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Combination Sum II Next: Permutation Sequence