#P6624. [省选联考 2020 A 卷] 作业题
[省选联考 2020 A 卷] 作业题
Description
Little W has just learned about spanning trees in a discrete mathematics class: a spanning tree of an undirected graph is a subset of the edge set with size , and the subgraph generated by is connected in .
While doing today’s homework, Little W got stuck on the following problem:
Given an undirected graph with vertices and edges (both vertices and edges are numbered starting from ), it is guaranteed that there are no multiple edges and no self-loops in the graph. Each edge has a positive integer weight . For a spanning tree of , define the value of as: the greatest common divisor of the weights of the edges in multiplied by the sum of these weights, that is:
$$val(T)=\left(\sum\limits_{i=1}^{n-1} w_{e_i}\right) \times \gcd(w_{e_1},w_{e_2},\dots,w_{e_{n-1}})$$where are the indices of the edges included in .
Little W needs to find the sum of the values of all spanning trees of . Since the answer may be very large, you only need to output it modulo .
Input Format
The first line contains two positive integers , representing the number of vertices and edges of .
The next lines each contain three positive integers . The -th line describes an undirected edge connecting vertex and vertex , with weight .
Output Format
Output a single integer in one line, representing the answer modulo .
3 3
1 2 4
2 3 6
1 3 12
192
Hint
[Sample Explanation 1]
There are three spanning trees in :
, with value .
, with value .
, with value .
The total sum is .
[Constraints]
of the testdata satisfies: .
Another of the testdata satisfies: .
Another of the testdata satisfies: all are equal.
Another of the testdata satisfies: all are prime numbers.
of the testdata satisfies: $1\leq n\leq 30, 1\leq m \leq \frac {n(n-1)}{2}, 1\leq w_i \leq 152501$。
Translated by ChatGPT 5
京公网安备 11011102002149号