#P16868. [GKS 2021 #H] Dependent Events
[GKS 2021 #H] Dependent Events
Problem Description
There are events, numbered through . The probability of occurrence of each event depends upon the occurrence of exactly one other event called the parent event, except event , which is an independent event. In other words, for each event from to , values are given: denoting the parent event of event , denoting the probability of occurrence of event if its parent event occurs, and denoting the probability of occurrence of event if its parent event does not occur. For event , its probability of occurrence is given. There are queries that we want to answer. Each query consists of distinct events, and , and you need to find the probability that both events and have occurred.
Input Format
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains two integers and denoting the number of events and number of queries, respectively. lines follow. The -th line describes event . The first line contains a single integer denoting the probability of occurrence of event multiplied by . Each of the next lines consists of three integers , and denoting the parent event of event , the probability of occurrence of event if its parent event occurs multiplied by , and the probability of occurrence of event if its parent event does not occur multiplied by , respectively. Then, lines follow, describing the queries. Each of these lines contains two distinct integers and . For each query, find the probability that both events and occurred.
Output Format
For each test case, output one line containing Case #: , where is the test case number (starting from ) and is the sought probability computed for -th query modulo , which is defined precisely as follows. Represent the answer of -th query as an irreducible fraction . The number then must satisfy the modular equation , and be between and , inclusive. It can be shown that under the constraints of this problem such a number 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 #, for the first query, the probability that both events and occurred is given by (the probability that event occurred) (probability that event occurs given event occurred). Event would occur with probability . Given that event occurred, the probability that event occurs is . Therefore, the probability of occurrence of event given that event occurred is (probability of event occurring given than event occurred + probability of event occurring given that event did not occur). The probability that both events and occurred is . The answer can be converted into fraction of , and one can confirm that the satisfies the conditions mentioned in the output section as and is uniquely determined. For the second query, the probability that both events and occurred is .
For Sample Case #, for the first query, the probability that both events and occurred is given by (the probability that event occurred) (probability that event occurs given event occurred). As is the parent event of event , the probability of event occurring given event occurred is which is . Hence, the probability that both events and occurred is . Hence, the output will be . For the second query, the probability of occurrence of both events and is .
Limits
.
, for each from to .
and , for all .
, for each from to .
, for each from to .
.
Test Set
.
.
Test Set
For at most cases:
.
.
For the remaining cases:
.
.