#P16868. [GKS 2021 #H] Dependent Events

    ID: 19195 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>数学倍增2021最近公共祖先 LCA概率论Google Kick Start

[GKS 2021 #H] Dependent Events

Problem Description

There are NN events, numbered 11 through NN. The probability of occurrence of each event depends upon the occurrence of exactly one other event called the parent event, except event 11, which is an independent event. In other words, for each event from 22 to NN, 33 values are given: PiP_i denoting the parent event of event ii, AiA_i denoting the probability of occurrence of event ii if its parent event occurs, and BiB_i denoting the probability of occurrence of event ii if its parent event does not occur. For event 11, its probability of occurrence KK is given. There are QQ queries that we want to answer. Each query consists of 22 distinct events, uju_j and vjv_j, and you need to find the probability that both events uju_j and vjv_j have occurred.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains two integers NN and QQ denoting the number of events and number of queries, respectively. NN lines follow. The ii-th line describes event ii. The first line contains a single integer KK denoting the probability of occurrence of event 11 multiplied by 10610^6. Each of the next N1N-1 lines consists of three integers PiP_i, AiA_i and BiB_i denoting the parent event of event ii, the probability of occurrence of event ii if its parent event occurs multiplied by 10610^6, and the probability of occurrence of event ii if its parent event does not occur multiplied by 10610^6, respectively. Then, QQ lines follow, describing the queries. Each of these lines contains two distinct integers uju_j and vjv_j. For each query, find the probability that both events uju_j and vjv_j occurred.

Output Format

For each test case, output one line containing Case #xx: R1R_1 R2R_2 R3R_3 \cdots RQR_Q, where xx is the test case number (starting from 11) and RjR_j is the sought probability computed for jj-th query modulo 109+710^9 + 7, which is defined precisely as follows. Represent the answer of jj-th query as an irreducible fraction pq\frac{p}{q}. The number RjR_j then must satisfy the modular equation Rj×qp(mod(109+7))R_j \times q \equiv p \pmod{(10^9 + 7)}, and be between 00 and 109+610^9 + 6, inclusive. It can be shown that under the constraints of this problem such a number RjR_j always exists and is uniquely determined.

2
5 2
200000
1 400000 300000
2 500000 200000
1 800000 100000
4 200000 400000
1 5
3 5
4 2
300000
1 100000 100000
2 300000 400000
3 500000 600000
1 2
2 4
Case #1: 136000001 556640004
Case #2: 710000005 849000006

Hint

For Sample Case #11, for the first query, the probability that both events 11 and 55 occurred is given by (the probability that event 11 occurred) ×\times (probability that event 55 occurs given event 11 occurred). Event 11 would occur with probability 0.20.2. Given that event 11 occurred, the probability that event 44 occurs is 0.80.8. Therefore, the probability of occurrence of event 55 given that event 11 occurred is 0.2×0.8+0.4×0.2=0.240.2 \times 0.8 + 0.4 \times 0.2 = 0.24 (probability of event 55 occurring given than event 44 occurred + probability of event 55 occurring given that event 44 did not occur). The probability that both events 11 and 55 occurred is 0.2×0.24=0.0480.2 \times 0.24 = 0.048. The answer 0.0480.048 can be converted into fraction of 6125\frac{6}{125}, and one can confirm that the 136000001136000001 satisfies the conditions mentioned in the output section as 136000001×1256(mod(109+7))136000001 \times 125 \equiv 6 \pmod{(10^9 + 7)} and is uniquely determined. For the second query, the probability that both events 55 and 33 occurred is 0.103520.10352.

For Sample Case #22, for the first query, the probability that both events 11 and 22 occurred is given by (the probability that event 11 occurred) ×\times (probability that event 22 occurs given event 11 occurred). As 11 is the parent event of event 22, the probability of event 22 occurring given event 11 occurred is A2A_2 which is 0.10.1. Hence, the probability that both events 11 and 22 occurred is 0.3×0.10.3 \times 0.1. Hence, the output will be 3×102mod(109+7)=7100000053 \times 10^{-2} \bmod (10^9 + 7) = 710000005. For the second query, the probability of occurrence of both events 22 and 44 is 0.0570.057.

Limits

1T1001 \le T \le 100.

1Pi<i1 \le P_i < i, for each ii from 22 to NN.

1uj,vjN1 \le u_j, v_j \le N and ujvju_j \ne v_j, for all jj.

0Ai1060 \le A_i \le 10^6, for each ii from 22 to NN.

0Bi1060 \le B_i \le 10^6, for each ii from 22 to NN.

0K1060 \le K \le 10^6.

Test Set 11

2N10002 \le N \le 1000.

1Q10001 \le Q \le 1000.

Test Set 22

For at most 55 cases:

2N2×1052 \le N \le 2 \times 10^5.

1Q2×1051 \le Q \le 2 \times 10^5.

For the remaining cases:

2N10002 \le N \le 1000.

1Q10001 \le Q \le 1000.