N and M (12)
InterviewTime limit2sMemory limit512 MB
Given N numbers and a length M, list all non-decreasing length-M sequences drawn from the numbers with repetition, in lexicographic order.
- Level
Medium4 of 10
- Topics
- Backtracking, Sorting, Recursion, Implementation
- Solved
- No attempts yet
Problem
You are given natural numbers and a natural number . Write a program that finds every sequence of length satisfying all of the conditions below.
- The sequence is made by choosing of the given numbers.
- You may choose the same number more than once.
- The sequence is non-decreasing. A sequence of length is non-decreasing when holds.
Input
The first line contains and . ()
The second line contains numbers. Every number given in the input is a natural number no greater than 10,000.
Output
Print one sequence that satisfies the conditions per line. Do not print the same sequence more than once, and separate the numbers of a sequence with a space.
Print the sequences in increasing lexicographic order.