#P8702. [蓝桥杯 2019 国 B] 燃烧权杖
[蓝桥杯 2019 国 B] 燃烧权杖
Problem Description
Xiao C has recently become addicted to a game. Now, in the game, Xiao C has a hero with health ; the enemy also has a hero with health . In addition, there are soldiers with health values . Xiao C now plans to use a skill called “Burning Scepter”.
“Burning Scepter” will, each time, uniformly at random select one living character (hero or soldier), reduce its health by , and then if that character’s health is less than or equal to , the character dies and will no longer be selected by “Burning Scepter”. “Burning Scepter” repeats the above operation until any hero dies. Xiao C wants to know the probability that, after using “Burning Scepter”, the enemy hero dies (that is, Xiao C’s hero survives). To avoid precision errors, you only need to output the result modulo a prime , as described in the output format.
Input Format
The input contains multiple test cases.
The first line contains a positive integer , indicating the number of test cases.
Then follow test cases. For each test case, the first line contains four non-negative integers , , , , representing Xiao C’s hero’s health, the enemy hero’s health, the modulus, and the number of soldiers.
The second line contains positive integers , representing the health of each soldier.
Output Format
For each test case, output one line with one non-negative integer, representing the remainder of the answer modulo the prime .
It can be proven that the answer is always a rational number. Let the answer be (where and are coprime positive integers). If the number you output is , then you must ensure that is congruent to modulo ; that is, , where denotes the modular inverse of modulo .
6
1 10 101 0
100 1 101 0
50 30 4903 2
1 1
987 654 233 1
321
1000000000 999999999 233 3
1 2 3
1000000000 999999999 3 3
1 2 3
51
37
1035
118
117
2
Hint
【Sample Explanation】 For the first test case, the required probability is the probability that “Burning Scepter” reduces the enemy hero’s health by on the very first time, which is . modulo leaves a remainder of .
For the second test case, the answer is , and is congruent to modulo .
For the third test case, the answer is .
【Test Case Scale and Assumptions】
For of the test cases, .
For of the test cases, .
For of the test cases, .
For all test cases, , , and is a prime, .
Lanqiao Cup 2019 National Contest, Group B, Problem J.
Translated by ChatGPT 5