#P51178. [NOI2019] Landdrol
[NOI2019] Landdrol
Description
Alice and Bob are playing card games, but Alice found that she can never win Bob, so she plans to cheat in the shuffling session.
There are cards in total, numbered from to from top to bottom. The card with index has a score of . In this problem, or always holds.
The whole shuffling session can be devided into rounds. During the -th round:
- Alice will take cards from the top, then all the cards are divided into 2 piles: the first contains cards from the top, the second contains the remaining cards.
- In particular, when or , one of the piles will be empty.
- Then Alice will merge the two piles in the following way. When there are cards left in the first pile and in the second, she will take the card at the bottom of the first pile with the probability of , and put it on the top of the new pile; otherwise she will take the card at the bottom of the second pile and put it on the top of the new pile.
Since the shuffling session is random, Alice can't know which card it is at a certain position. But Alice wants to know the expected score of the card at a certain position after rounds of shuffling.
Input
The first line contains integers . When , , otherwise .
The second line contains integers .
The third line contains a single integer — the number of queries.
Each of the next lines contains a single integer .
Output
You should print lines, the -th line contains the expected score of the -th card from top to bottom.
You should print the answer module , that is, if the answer is , where , you should print an integer such that and . One can prove that such is unique.
Sample 1
4 1 1
3
1
1
249561090
After shuffling, the cards will become:
- from top to bottom with the probability of
- from top to bottom with the probability of
- from top to bottom with the probability of
- from top to bottom with the probability of
So the expected score of the first place is .
The picture below illustrates the whole process to the answer :
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.
Limits And Hints
Specific constraints of each cases are listed below:
Subtask # | Other constraints | |||
---|---|---|---|---|
No | ||||
All are equal | ||||
No | ||||
WARNING: THERE IS NO GUARANTEE that .