#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 xx; the enemy also has a hero with health yy. In addition, there are kk soldiers with health values a1,a2,,aka_1,a_2,\cdots,a_k. 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 1010, and then if that character’s health is less than or equal to 00, 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 pp, as described in the output format.

Input Format

The input contains multiple test cases.

The first line contains a positive integer TT, indicating the number of test cases.

Then follow TT test cases. For each test case, the first line contains four non-negative integers xx, yy, pp, kk, representing Xiao C’s hero’s health, the enemy hero’s health, the modulus, and the number of soldiers.

The second line contains kk positive integers a1,a2,,aka_1,a_2,\cdots,a_k, 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 pp.

It can be proven that the answer is always a rational number. Let the answer be a/ba/b (where aa and bb are coprime positive integers). If the number you output is xx, then you must ensure that aa is congruent to b×xb\times x modulo pp; that is, xa×b1(modp)x \equiv a\times b^{-1} \pmod p, where b1b^{-1} denotes the modular inverse of bb modulo pp.

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 1010 on the very first time, which is 1/21/2. 2×512 \times 51 modulo 101101 leaves a remainder of 11.

For the second test case, the answer is 1023/10241023/1024, and 1024×371024 \times 37 is congruent to 10231023 modulo 101101.

For the third test case, the answer is 99/12899/128.

【Test Case Scale and Assumptions】

For 10%10\% of the test cases, x,y,a1,,ak10x, y, a_1,\cdots, a_k \le 10.

For 20%20\% of the test cases, x,y,a1,,ak100x, y, a_1,\cdots, a_k \le 100.

For 50%50\% of the test cases, x,y,a1,,ak1000x, y, a_1,\cdots, a_k \le 1000.

For all test cases, 1T1001\leq T\leq 100, 1x,y,a1,,ak1091 \le x, y, a_1,\cdots, a_k \le 10^9, 3p100003 \le p \le 10000 and pp is a prime, 0k100 \le k \le 10.

Lanqiao Cup 2019 National Contest, Group B, Problem J.

Translated by ChatGPT 5