N and M (8)
Time limit1sMemory limit512 MB
Given N distinct natural numbers and a length M, print all non-decreasing sequences of length M drawn from the numbers, in lexicographic order.
- Level
Medium4 of 10
- Topics
- Backtracking, Sorting, Recursion
- Solved
- No attempts yet
Problem
You are given N natural numbers and a natural number M. Write a program that finds every sequence of length satisfying all of the conditions below. The N natural numbers are all different.
- a sequence of M numbers chosen from the N natural numbers
- the same number may be chosen more than once.
- the chosen sequence must be non-decreasing.
- a sequence of length is non-decreasing if it satisfies .
Input
The first line contains and . ()
The second line contains numbers. Every number in the input is a natural number less than or equal to 10,000.
Output
Print one sequence satisfying the conditions per line. Do not print the same sequence more than once, and separate the numbers of each sequence with a space.
Print the sequences in increasing lexicographic order.