luogu#P4457. [BJOI2018] 治疗之雨
[BJOI2018] 治疗之雨
Background
(Players who have not played Hearthstone can skip this paragraph.) Today we discuss the interaction between “Healing Rain” (restore Health, randomly distributed among all friendly characters) and “Shadowstrike Armor” (whenever a character is healed, deal damage to a random enemy). Suppose you have minions on your side with infinite remaining Health and infinite missing Health (i.e., no upper or lower bounds), and your opponent has copies of Shadowstrike Armor on the board. Your hero has current Health and maximum Health . Now you cast a Healing Rain that can restore infinitely many Health (instead of ). What is the expected total amount of Health restored by Healing Rain before your hero dies (Health drops to ; due to Healing Rain’s targeting rules, the hero will no longer be healed afterward).
Note: If the background conflicts with the problem statement, follow the problem statement.
Below is the formal description of the problem.
Problem 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