Box
Time limit5sMemory limit128 MB
Count left-packed orderings of boxes with total width at most W so no unpacked box still fits the leftover space.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
There are boxes of widths and one big box of width . Write a program that counts the ways to put the boxes into the big box.
The conditions are as follows.
- The widths of the boxes put into the big box must not add up to more than .
- Boxes go in one at a time from the left end of the big box, and no gap may be left between two boxes. Empty space may remain at the right end of the big box, but no box that fits into that space may still be left out.
- If a box sits in position in one way and in a different position in another way, the two ways count as different.
- Two boxes of the same width cannot be told apart.
Input
The first line contains the number of test cases ().
The first line of each test case contains () and (). The second line contains the box widths . ()
Output
For each test case, print one line in the form Case x: y, where is the test case number starting from 1 and is the number of ways modulo .
Hint
For , and widths , these six ways are possible. Each line lists the widths of the boxes in the order they went in, starting from the left.
- 1 2
- 1 3
- 2 1
- 2 3
- 3 1
- 3 2
Putting in a single box, so 1, 2, or 3 on its own, breaks condition 2. A box that fits into the empty space on the right is still left out.