Joining up a fence
Statement
A village has N workshops numbered 1 through N. Each workshop holds several planks, and one workshop holds at most M planks. The planks may differ in length.
The villagers want to walk round the workshops, gather planks and build a single line of fence. The planks are taken from workshop 1 through to workshop N in order and joined together in the order they were taken. At most one plank can be taken from one workshop, and a workshop may be walked past without taking anything.
One workshop may hold several planks of the same length. Even when the lengths are the same these are different planks, so which plank was taken makes it a different way.
Write a program that calculates how many ways there are to take planks so that the total length of the fence is exactly H. The number of ways can grow very large, so print the remainder of the number of ways divided by 10,007.
For example, suppose N=3, M=3 and H=5, and the lengths of the planks each workshop holds are as in the table below.
| Workshop | 1 | 2 | 3 |
|---|---|---|---|
| Lengths of the planks held | 2, 1 | 1 | 3, 2 |
Writing out every way of reaching a total length of exactly 5 gives the table below.
| Way | Workshop 1 | Workshop 2 | Workshop 3 | Total length |
|---|---|---|---|---|
| 1 | length 2 | nothing taken | length 3 | 5 |
| 2 | length 2 | length 1 | length 2 | 5 |
| 3 | length 1 | length 1 | length 3 | 5 |
So the answer in this example is 3.
It is worth pointing out that planks of the same length are counted apart. Suppose N=2, M=2 and H=3, workshop 1 holds two planks of length 1, and workshop 2 holds one plank of length 2.
| Workshop | 1 | 2 |
|---|---|---|
| Lengths of the planks held | 1, 1 | 2 |
Two planks can be taken from workshop 1 and both are of length 1, but which one was taken is different, so there are 2 ways. The fence joined together looks the same in both, a length of 1 lying next to a length of 2, but the plank taken is different, so they are counted apart.
There are also cases where the total length does not reach H even after every workshop has been walked past. That happens when the planks held are all short, so that taking every one of them still falls short of H. There is no way at all then, so 0 is printed.
Input and Output Examples
Example 1
Input
3 3 5 2 1 1 3 2
Output
3
Example 2
Input
2 2 3 1 1 2
Output
2
Example 3
Input
5 5 10 4 3 5 1 4 5 2 5 5 1 3 1
Output
37
Input
First the number of workshops N, the largest number of planks one workshop can hold M, and the length of the fence to build H are given on the first line, separated by spaces. N, M and H are all natural numbers from 1 to 50.
Then N lines follow, giving the lengths of the planks held by workshop 1 through workshop N in order. One line holds the lengths of the planks that workshop holds, separated by spaces, and the number of planks is from 1 to M. Each plank length is a natural number from 1 to 50.
Output
Print on one line the remainder, divided by 10,007, of the number of ways to take planks so that the total length of the fence is exactly H. If a fence of length exactly H cannot be built, print 0.
Requirements
- At most one plank is taken from one workshop
- A workshop may be walked past without taking a plank
- Planks of the same length are counted as different planks and told apart
- A total length shorter than H or longer than H is not counted
- Print the answer as the remainder divided by 10,007
- 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 15 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 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 2 3 1 1 2
2
3 3 5 2 1 1 3 2
3
5 5 10 4 3 5 1 4 5 2 5 5 1 3 1
37
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
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.