#P4457. [BJOI2018] 治疗之雨
[BJOI2018] 治疗之雨
Description
Problem update: Since many people reported difficulty understanding the statement, but to respect the original wording we will not make large changes. You may interpret “minimum” and “maximum” as lower bound and upper bound, similar to the Health in the background.
You now have numbers: the first is , with minimum and maximum ; the remaining are all infinite, with no minimum or maximum. You may perform any number of rounds. In each round, do the following:
- Uniformly at random choose one number among those that are not at their maximum (if none, do nothing), and add to it.
- Then perform this step times: uniformly at random choose one number among those that are not at their minimum (if none, do nothing), and subtract from it.
What is the expected number of rounds until the first number becomes its minimum .
Input Format
The input contains multiple test cases. The first line contains a positive integer , the number of test cases. The next lines each contain four non-negative integers , , , (as defined above), representing one query.
Output Format
Output lines, each with one integer, the answer for that query.
If the first number never becomes the minimum no matter how many rounds, output -1.
Otherwise, it can be proved that the answer is a rational number. Output the answer modulo , i.e., suppose the answer is (where and are coprime positive integers). You must output an integer such that and .
2
2 1 1 1
2 2 1 1
6
8
Hint
Constraints
- For of the testdata, , .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , , .
It is guaranteed that the case , does not exist (because the author judged it wrong).
It is guaranteed that the denominator of the answer is not a multiple of (because the author did not think of it).
Translated by ChatGPT 5
京公网安备 11011102002149号