#P16281. 「MierOI R1」Beyond
「MierOI R1」Beyond
Problem Description
One day, Little M learned an algorithm called "fractional programming", which is used to solve the "fractional programming problem".
:::info[Fractional Programming Problem]{open}
There are items. The -th item has a value of and a weight of . Both and are positive integers.
Select items from the items, let the selected items be , and maximize the value of
$$\frac{a_{i_1}+a_{i_2}+\dots+a_{i_k}}{b_{i_1}+b_{i_2}+\dots+b_{i_k}}$$:::
Coincidentally, Little M had also solved a problem called sale. On a whim, he came up with a problem to test you.
Given , and the value sequence of the items, find the number of possible weight sequences of the items satisfying , such that the following greedy strategy exactly finds the optimal solution to the "fractional programming problem":
- Sort the items in descending order of their values . For items with the same value, sort them in ascending order of their indices . Select the top items after sorting.
The answer should be modulo .
Input Format
This problem contains multiple test cases.
The first line of the input contains a positive integer , indicating the number of test cases.
Then, the test cases follow sequentially. For each test case:
- The first line contains two positive integers .
- The second line contains positive integers .
Output Format
For each test case, output a single line containing a non-negative integer, representing the number of weight sequences satisfying the condition. The answer should be modulo .
2
2 1
10 9
6 3
10 10 8 6 6 5
3
15
5
16 1
256 225 196 169 144 121 100 81 64 49 36 25 16 9 4 1
16 2
256 225 196 169 144 121 100 81 64 49 36 25 16 9 4 1
16 15
256 225 196 169 144 121 100 81 64 49 36 25 16 9 4 1
16 14
256 225 196 169 144 121 100 81 64 49 36 25 16 9 4 1
16 9
256 225 196 169 144 121 100 81 64 49 36 25 16 9 4 1
34816
18176
32996
16887
3171
Hint
Explanation for Sample #1
For the first test case, the weight sequences satisfying the condition are . When , the greedy solution is , and the optimal solution is , so the greedy strategy fails to find the optimal solution.
Data Range
This problem uses bundled subtasks.
For all test cases, it is guaranteed that , , and .
::cute-table{tuack}
| Subtask | Score | ||
|---|---|---|---|
| ^ | |||
| ^ | |||