#P5472. [NOI2019] 斗主地
[NOI2019] 斗主地
Description
Xiao S is playing a game called “Dou Dizhu” with Xiao F.
Poor Xiao S finds that he cannot beat Xiao F at playing cards, so he wants to do something during the shuffling stage.
A deck has cards, numbered from top to bottom as . The score of the card numbered is . In this problem, has exactly two possibilities: or .
The shuffling method is similar to what we do in daily life. We define it formally as follows: the shuffling stage consists of rounds, performed in order. In round :
- Xiao S takes the top cards from the deck. Then the deck is split into two piles: the first pile is the top cards, and the second pile is the remaining cards, and the relative order within each pile remains unchanged. In particular, when or , one pile is empty.
- Next, merge the two piles to produce a new third pile. When the first pile has cards left and the second pile has cards left, with probability take the bottom card of the first pile and put it on the top of the new third pile; with probability take the bottom card of the second pile and put it on the top of the new third pile.
- Repeat step until both piles are empty. This completes one round of shuffling.
Because the shuffling process is random, Xiao S cannot know exactly which card is at a certain position. But after these rounds of shuffling, Xiao S wants to ask what the expected score of the card at a certain position is. Xiao S will ask you such questions.
Input Format
The first line contains three positive integers , representing the number of cards, the number of shuffling rounds, and the type of . When , . When , .
The next line contains integers, representing .
The next line contains a positive integer , the number of queries from Xiao S. The next lines each contain a positive integer , meaning Xiao S wants to know the expected score of the card at the -th position from top to bottom.
It is guaranteed that .
Output Format
Output lines. Each line contains one integer, which is the answer modulo .
That is, suppose the answer in lowest terms is , where and are coprime. Output an integer such that and . It can be proven that such an integer is unique.
4 1 1
3
1
1
249561090
Hint
More Samples
You can obtain more samples through the additional files.
Sample 2
See landlords/landlords2.in and landlords/landlords2.ans in the additional files.
Sample 3
See landlords/landlords3.in and landlords/landlords3.ans in the additional files.
Explanation for Sample Input/Output 1
- With probability , the final result from top to bottom is .
- With probability , the final result from top to bottom is .
- With probability , the final result from top to bottom is .
- With probability , the final result from top to bottom is .
So in the end, with probability the first position is , and with probability the first position is . Therefore, the expected score of the first position is .
To help you understand the shuffling process more intuitively, we draw below the process where the result is .

Constraints and Notes
For all test points, it is guaranteed that , , , .
The specific limits for each test point are shown in the table below:
| Test Point | Other Properties | |||
|---|---|---|---|---|
| None | ||||
| All are the same | ||||
| None | ||||
Please note that we do not guarantee .
Hint
Here we give the definition of the expectation of a discrete random variable :
Suppose the possible values of are , and are the probabilities that takes the corresponding values. Then the expectation of is:
Translated by ChatGPT 5
京公网安备 11011102002149号