Packing fruit
Statement
A fruit shop has N kinds of fruit, numbered 1 through N. Every kind is piled up in plenty, so you can pick up as many pieces as you like.
A customer packs M pieces of fruit in all into one basket. The same kind of fruit may be packed more than once. Once the fruit is in the basket there is no way to tell which order it was picked up in, and only the kinds and the number of each kind are left. So if the kinds and the counts packed are the same, it counts as the same case, whatever order the fruit was picked up in.
Write a program that prints every way the customer can pack the fruit, in lexicographic order. Each case writes the numbers of the fruit in the basket from the smallest number upward. The same number may appear several times.
For example, suppose M=2 pieces are packed from N=3 kinds of fruit. There are 6 cases in all, and listed in lexicographic order they are the ones in the table below.
| Case | First number written | Second number written |
|---|---|---|
| Case 1 | 1 | 1 |
| Case 2 | 1 | 2 |
| Case 3 | 1 | 3 |
| Case 4 | 2 | 2 |
| Case 5 | 2 | 3 |
| Case 6 | 3 | 3 |
Case 1 shows that the same kind may be packed twice, so 1 1 is in the table. On the other hand 2 1 is not in the table. Packing one piece of fruit 1 and one piece of fruit 2 was already written once as the 1 2 of case 2, and the order the fruit was picked up in is not taken into account.
One more kind brings more cases. With the same M=2 and N=4 there are 10 cases that can be packed, as the table below shows.
| Case | First number written | Second number written |
|---|---|---|
| Case 1 | 1 | 1 |
| Case 2 | 1 | 2 |
| Case 3 | 1 | 3 |
| Case 4 | 1 | 4 |
| Case 5 | 2 | 2 |
| Case 6 | 2 | 3 |
| Case 7 | 2 | 4 |
| Case 8 | 3 | 3 |
| Case 9 | 3 | 4 |
| Case 10 | 4 | 4 |
In the tables the numbers of each case never get smaller as you read from left to right. The same number may carry on, but a number smaller than the one before it may not come after it. This is the rule that writes a case with the same kinds and counts only once.
Input and Output Examples
Example 1
Input
3 2
Output
1 1 1 2 1 3 2 2 2 3 3 3
Example 2
Input
1 1
Output
1
Example 3
Input
2 2
Output
1 1 1 2 2 2
Input
First the number of kinds of fruit N in the shop is given as a natural number. N is a natural number from 1 to 8.
Next, on the same line, the number of pieces of fruit M to pack into the basket is given, separated by a space. M is a natural number from 1 to 8, and it is not greater than N, the number of kinds of fruit.
So the input is one line, and that line holds two natural numbers separated by a single space.
Output
Print every way the fruit can be packed, in lexicographic order, one case per line. A line writes the numbers of the M pieces of fruit in the basket from the smallest number upward. The same number may appear several times. That leaves one space after the last number on a line as well. Also, after every case has been printed, When a line holds more than one value, the values are separated by a single space.
Requirements
- The same kind of fruit may be packed more than once
- Cases with the same kinds and the same counts are the same case, so print each case only once
- Write the numbers of each case from the smallest upward, so a number smaller than the one before it never comes after it
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 14 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 14 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 14 cases to solve this.
1 1
1
2 2
1 1 1 2 2 2
3 2
1 1 1 2 1 3 2 2 2 3 3 3
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.