#P7142. [THUPC 2021 初赛] 密集子图

[THUPC 2021 初赛] 密集子图

Description

One day, the wizard Xiao L saw a complete directed graph.

All edges in the graph have length 11, and all edges are white.

Now Xiao L wants to cast magic on this graph. Each directed edge independently has a certain probability of turning black.

Xiao L considers a graph to be "dense" if and only if, when traveling using only black edges, the shortest path length from node 11 to every other node is at most kk (in particular, if two nodes are not connected, the shortest path length between them is considered ++ \infty).

Xiao L wants to know: what is the probability that this complete directed graph is "dense"? Please output this probability modulo 998,244,353998,244,353.

Input Format

The first line contains two positive integers nn (2n12)2 \le n \le 12) and kk (1kn11 \le k \le n - 1).
In the next n×(n1)n \times (n - 1) lines, each line contains four positive integers x,y,p,qx, y, p, q, meaning that the probability that the directed edge from node xx to node yy turns black is pq\frac{p}{q}. It is guaranteed that 1xn1 \le x \le n, 1yn1 \le y \le n, xyx \ne y, 0pq<998,244,3530 \le p \le q < 998,244,353, q>0q > 0, and each valid (x,y)(x, y) appears exactly once.

Output Format

Output one integer in a single line, representing the answer.

3 1
1 2 1 2
2 1 1 2
1 3 1 3
3 1 2 3
2 3 3 4
3 2 2 5

166374059

Hint

[Sample Explanation #1]

This complete directed graph is "dense" if and only if both the directed edge from node 11 to node 22 and the directed edge from node 11 to node 33 turn black. The probability of this happening is =12×13=16= \frac{1}{2} \times \frac{1}{3} = \frac{1}{6}, and $\frac{1}{6} \bmod 998,244,353 = 6^{998,244,351} \bmod 998,244,353 = 166,374,059$.

[Source]

From the preliminary round of the 2021 Tsinghua University Student Programming Contest and Intercollegiate Invitational (THUPC2021).

Solutions and other resources can be found at https://github.com/THUSAAC/THUPC2021-pre.

Translated by ChatGPT 5