#P8100. [USACO22JAN] Counting Haybales P
[USACO22JAN] Counting Haybales P
Problem Description
As usual, the cow Bessie is causing trouble in Farmer John’s barn. FJ has () haybale stacks. For each , the -th stack contains () haybales. Bessie does not want any haybales to fall over, so the only operation she can perform is:
If the heights of two adjacent stacks differ by exactly , she can move the top haybale from the taller stack onto the shorter stack.
After performing the above operation a finite number of times, how many different height sequences can be obtained, modulo ? Two height sequences are considered the same if, for all , the -th stack has the same number of haybales in both sequences.
Input Format
The first line of input contains , the number of independent subtasks. You must answer all of them correctly to pass the entire test.
Each subtask contains and a sequence of heights. The input guarantees that the sum of over all subtasks does not exceed .
Output Format
Output lines. Each line should contain the answer for one subtask.
7
4
2 2 2 3
4
3 3 1 2
4
5 3 4 2
6
3 3 1 1 2 2
6
1 3 3 4 1 2
6
4 1 2 3 5 4
10
1 5 6 6 6 4 2 3 2 5
4
4
5
15
9
8
19
Hint
[Sample Explanation]
For the first subtask, the four possible height sequences are:
.
For the second subtask, the four possible height sequences are:
.
[Constraints]
- Testdata 1-3 satisfy .
- Testdata 4 satisfy that for all , .
- Testdata 5-7 satisfy that for all , .
- Testdata 8-10 satisfy that for all , , and .
- Testdata 11-13 satisfy .
- Testdata 14-17 satisfy .
- Testdata 18-21 have no additional constraints.
Problem by: Daniel Zhang.
Translated by ChatGPT 5