#P7491. 「Stoi2031」蒲公英的约定(vol.2)

「Stoi2031」蒲公英的约定(vol.2)

Description

Qinghe is playing a game. They have nn bunches of dandelions, and the ii-th bunch has sis_i flowers.

These dandelions have a magical property: for a given constant σ(0,1)\sigma \in (0,1), for xx flowers, σx\lfloor \sigma x \rfloor flowers are split off as one group, and the remaining xσxx-\lfloor \sigma x \rfloor flowers continue to be split in the same way, until the split-off group contains no dandelions, i.e. σx=0\lfloor \sigma x \rfloor=0. They call this phenomenon "renxing" (stubbornness).

Now each bunch of dandelions has this "renxing" property. The rules are as follows:

Starting from Qing, the two players take turns to make a travel move. One travel move means choosing one bunch of dandelions and blowing away some flowers from the first group of that bunch. You must blow away at least one flower, and at most the size of the first group; that is, you cannot blow away any dandelions outside the first group.

When the first group becomes empty, the next group becomes the first group. If a bunch has only one group of dandelions left, then this bunch can no longer be chosen. When no bunch can be chosen, the game ends, and the player whose turn it is loses.

They want to know: if, on Qing’s first travel, she chooses uniformly at random one bunch of dandelions that can be operated on, and then uniformly at random chooses the number of flowers to blow away, and after that both players play optimally, what is the value of Qing’s winning probability xmod20190816170251x \bmod{20190816170251}?

The difference from vol.1 is that after a bunch of dandelions is partially blown away, it will be regrouped.

Input Format

The first line contains two positive integers id,nid,n, where idid denotes the Subtask number.

The second line contains nn positive integers; the ii-th one is sis_i.

If id>3id>3, the third line contains two positive integers p,qp,q, meaning σ=pq\sigma=\dfrac{p}{q}.

Output Format

Output one integer per line, representing Qing’s winning probability xmod20190816170251x \bmod{20190816170251}.

4 3
1 1 1
1 6

0

6 3
1 7 3
1 3

15143112127689

Hint

Brief statement:

There are nn bunches of dandelions; the ii-th bunch has sis_i flowers. Given a real number σ\sigma, two players move alternately. Each move can choose one bunch of dandelions, and choose an integer c(0,σs]c \in (0,\sigma s], then blow away cc flowers from that bunch, where ss is the number of flowers in that bunch before the move. You must blow away at least one flower. If a player cannot move, that player loses.

Find the value of the first player’s winning probability xmod20190816170251x \bmod{20190816170251}, under the process where on the first move the first player uniformly at random chooses any operable bunch of dandelions and then uniformly at random chooses the number of flowers to blow away, and afterwards both players use optimal strategies.

Sample explanation:

For sample 11, Qing cannot make any move, so the winning probability is 00.

For sample 22, Qing can choose the 22-nd bunch and, among two possible moves, choose to blow away 22 flowers to become 1,5,31,5,3; or choose the 33-rd bunch and choose the only possible move to become 1,7,21,7,2. The 11-st bunch cannot be chosen. The overall winning probability is 12+12=34\dfrac{\frac{1}{2}+1}{2}=\dfrac{3}{4}.

Constraints:

This problem uses bundled testdata. The score and limits of each Subtask are as follows.

Subtask No. nn \le sis_i \le σ\sigma restriction Score
11 3×1053 \times 10^5 101010^{10} σ=2+13\sigma=\dfrac{\sqrt{2}+1}{3} 1010
22 σ=3+15\sigma=\dfrac{\sqrt{3}+1}{5}
33 σ=512\sigma=\dfrac{\sqrt{5}-1}{2}
44 100100 11 None 33
55 100100 σ=12\sigma=\dfrac{1}{2} 77
66 10610^6 None 1313
77 3×1053 \times 10^5 101010^{10} σ12\sigma \ge \dfrac{1}{2} 4747

For 100%100\% of the data, $1 \le n \le 3 \times 10^5, 1 \le s_i \le 10^{10}, 1 \le p<q \le 10^9$.

The input size of this problem is large. You may use the fast input template from the contest statement to speed up reading.

Translated by ChatGPT 5