#P8580. [CoE R5] 罚球
[CoE R5] 罚球
Problem Description
There are people playing a free throw game. The rules are as follows:
- Each person is numbered . At the beginning, player takes a free throw. After that, the next player who has not been eliminated takes a free throw. In particular, after player comes player .
- If the shooter does not hit the backboard, they are eliminated immediately.
- If the shooter hits the backboard but does not score, then if the previous player scored, this shooter will be eliminated; otherwise, they will not be eliminated.
- The game ends when there is only one person left.
Note that the first player will not be eliminated if they hit the backboard but do not score.
Among these people, for player , the probability of not hitting the backboard is , and the probability of hitting the backboard but not scoring is . Find the expected total number of free throws taken by all players when the game ends.
Input Format
The first line contains two integers , representing the number of players and the subtask index.
The next lines each contain two integers , and it is guaranteed that .
Output Format
Output one line: the expected total number of free throws taken by all players. If the game will never end, output . Otherwise, output the value modulo .
2 8
200 400
200 400
888921
7 8
321 637
321 637
321 637
321 637
321 637
321 637
321 637
818968
6 10
338 270
229 413
132 133
141 173
157 686
616 250
315860
8 10
338 270
229 413
132 133
141 173
157 686
616 250
0 0
0 0
-1
Hint
About modulo
If you do not know how to take modulo of rational numbers, see here.
Sample Explanation
Input :
For everyone, the probability of not hitting the backboard is , and the probability of hitting the backboard but not scoring is . The expected number of free throws is .
The calculation is as follows (black means eliminated, red means no score but not eliminated, blue means scored):
$$\dfrac{1}{5}+\red{\dfrac{2}{5}}\times(\dfrac{1}{5}+\red{\dfrac{2}{5}}\times(...)+\blue{\dfrac{2}{5}}\times(...))+\blue{\dfrac{2}{5}}\times(\dfrac{3}{5}+\blue{\dfrac{2}{5}}\times(...))=\dfrac{25}{9}$$Input :
For everyone, the probability of not hitting the backboard is , and the probability of hitting the backboard but not scoring is . The expected number of free throws is .
Constraints
This problem uses bundled tests.
Test point properties: | | Property | Score | | :----------: | :----------: | :----------: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | are both fixed values, and the answer is not | | | | | | | | | | | | No special property | |
For of the testdata, , .
Subtask of this problem is scored in two parts, corresponding to .
It is guaranteed that there is no case where the denominator is a multiple of .
Translated by ChatGPT 5