#P4564. [CTSC2018] 假面
[CTSC2018] 假面
Description
Zhenzhen likes to play a game called DotA (Defense of the Algorithm). In this game, Zhenzhen controls his hero and fights alongside teammates against another team. Zhenzhen’s favorite hero in DotA is called “Faceless,” and this hero has skills:
- Lock: Cast on a specified enemy unit, dealing point of damage (reducing its health by ) with probability .
- Barrier: Cast on an area to trap all other units in that area so they cannot move. In the game, if a unit’s health drops to or below, that unit dies.
Zhenzhen is not very skilled at controlling Faceless, so he decides to practice diligently. There are enemy units (numbered from to ), and the enemy unit numbered has health points.
Zhenzhen has arranged a practice plan in which he will cast skills in order:
- For the Lock skill: Zhenzhen specifies an enemy unit and casts it on that unit. Since many factors determine the probability parameter , the value of may be different each time. In particular, if the specified enemy unit is already dead, then this skill has no effect.
- For the Barrier skill: Zhenzhen intends to cast it on specified enemy units, but since he is not good at casting this skill, he can hit exactly enemy unit. The probability of hitting each living enemy unit is equal (i.e., already dead enemy units have no effect). In particular, if all specified enemy units are already dead, then this skill also does not hit any unit.
Now, Lvlv, who is watching Zhenzhen practice, wants to know:
- For each Barrier skill that Zhenzhen casts, what is the probability that it hits each enemy, respectively.
- After all of Zhenzhen’s skills have been cast, what is the expected remaining health of each enemy unit.
To prevent precision errors, for all numerical outputs, please output their values modulo .
Since Barrier is Faceless’s ultimate skill, the number of times Zhenzhen casts it will not be too many. See Constraints.
Input Format
The -st line contains positive integer , the number of enemy units. The -nd line contains positive integers , representing the initial health of each enemy unit in order. The -rd line contains non-negative integer , the number of skills Zhenzhen will cast. The -th to the -rd lines each describe one skill; the -rd line describes the -th skill.
Each line begins with an integer , indicating the type of the skill. If , it indicates the Lock skill. This is followed by positive integers , meaning the target is , and the hit probability is . (Guaranteed that , .) If , it indicates the Barrier skill. This is followed by positive integer indicating the number of targets, then an additional numbers describing all the targets of this skill. (Guaranteed that all are pairwise distinct.) Within each line, if there are multiple numbers, separate them with a single space.
Output Format
Output lines (where is the number of Barrier skills):
For the first lines, corresponding to each Barrier skill in order: Output numbers. The -th number is the probability that the Barrier hits enemy unit .
On the -st line, output numbers. The -th number is the expected remaining health of enemy unit after all skills have been cast.
Within each line, if there are multiple numbers, separate them with a single space.
For all numerical values, output them modulo : that is, suppose the answer in reduced fraction form is with . Output the integer such that and . (It can be proven that such an integer is unique.)
3
1 2 3
6
0 2 1 1
1 1 2
0 2 1 1
0 3 1 1
1 1 2
1 3 1 2 3
1
0
499122177 0 499122177
1 0 2
3
1 1 1
4
0 2 1 2
1 2 1 2
0 3 2 3
1 3 1 2 3
249561089 748683265
804141285 887328314 305019108
1 499122177 332748118
Hint
Sample Explanation 1 Zhenzhen casts the following skills in order:
- Cast Lock on enemy unit : deals damage with probability . At this moment, enemy unit must have health remaining.
- Cast Barrier on enemy unit : (since enemy unit is still alive,) it must hit unit .
- Cast Lock on enemy unit : deals damage with probability .
- Cast Lock on enemy unit : deals damage with probability . Now the three enemy units’ health values must be respectively, and enemy unit must be dead.
- Cast Barrier on enemy unit : (since enemy unit is dead,) it must hit no unit.
- Cast Barrier on enemy units : the probabilities of hitting enemy units and are equal, i.e., each . Finally, the remaining health of the three enemy units must be .
Sample Explanation 2 Analysis for each Barrier skill:
- The -st Barrier (targets enemy units ):
- The probability that enemy unit is alive is , and enemy unit must be alive.
- If enemy unit is alive, then the probabilities of Barrier hitting and are equal, both ; if enemy unit is dead, then Barrier must hit enemy unit .
- Therefore: the probability of hitting enemy unit is $\frac{1}{2} \times 1 + \frac{1}{2} \times \frac{1}{2} = \frac{3}{4}$; the probability of hitting enemy unit is $\frac{1}{2} \times 0 + \frac{1}{2} \times \frac{1}{2} = \frac{1}{4}$.
- The -nd Barrier (targets enemy units ):
- The probabilities that the three enemy units are alive are , respectively.
- The probability that are all alive is ; the probability that only are alive is ; the probability that only are alive is ; the probability that only is alive is .
- Therefore: the probability of hitting enemy unit is $\frac{1}{6} \times \frac{1}{3} + (\frac{1}{3} + \frac{1}{6}) \times \frac{1}{2} + \frac{1}{3} \times 1 = \frac{23}{36}$; the probability of hitting enemy unit is $\frac{1}{6} \times \frac{1}{3} + \frac{1}{3} \times \frac{1}{2} = \frac{2}{9}$; the probability of hitting enemy unit is $\frac{1}{6} \times \frac{1}{3} + \frac{1}{6} \times \frac{1}{2} = \frac{5}{36}$. Finally, the expected remaining health of the three enemy units is .
Constraints
Let be the number of Barrier skills.
| Test point id | n = | Q = | C = | u, v | Other constraints |
|---|---|---|---|---|---|
| 1 | 5 | 21 | 6 | u < v | None |
| 2 | 60 | 199992 | 500 | All are equal | |
| 3 | 23 | 6 | All | ||
| 4 | 199994 | 500 | None | ||
| 5 | 199995 | ||||
| 6 | 199996 | 0 | |||
| 7 | 199997 | 500 | u = v | ||
| 8 | 200 | 199998 | 1000 | u < v | |
| 9 | 199999 | ||||
| 10 | 200000 |
For all test points, it is guaranteed that , , , .
Hint
The units digit of can help you quickly determine the test point id. The test point order may not correlate with difficulty.
Thanks to @和泉正宗 for providing the statement.
Translated by ChatGPT 5
京公网安备 11011102002149号