#P3790. 文艺数学题

    ID: 2721 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>洛谷原创O2优化枚举,暴力剪枝生成树洛谷月赛

文艺数学题

Description

Xiao Y has an undirected graph with NN vertices and MM edges (possibly with multiple edges and self-loops), and each edge has a weight ww.

You need to compute the sum of the greatest common divisors of the edge weights over all spanning trees; see the sample for details.

Since the answer may be large, take it modulo 10000000071000000007.

Input Format

The first line contains two positive integers N,MN, M, denoting the number of vertices and the number of edges.

The next MM lines each contain three positive integers u,v,wu, v, w, denoting an edge connecting uu and vv with weight ww.

Output Format

Output a single integer: the answer modulo 10000000071000000007.

4 5  
1 2 12  
1 3 9  
2 4 6  
3 4 8  
1 4 4  
15

Hint

Explanation of Sample 11

Obviously, this graph has 88 different spanning trees.

Let gcd(x,y,z)\gcd(x,y,z) denote the greatest common divisor of x,y,zx, y, z. Then the answer is

$\gcd(12,6,8)+\gcd(6,8,9)+\gcd(8,9,12)+\gcd(9,12,6)+\gcd(9,4,6)+\gcd(12,4,8)+\gcd(12,4,9)+\gcd(6,4,8)$

=2+1+1+3+1+4+1+2=2+1+1+3+1+4+1+2

=15=15.

Constraints

For 20%20\% of the testdata, N10,M20N\le 10, M\le 20;

For another 10%10\% of the testdata, N12,M24N\le 12, M\le 24;

For another 20%20\% of the testdata, N60,M3000N\le 60, M\le 3000, and it satisfies ui+1=viu_i+1=v_i;

For another 20%20\% of the testdata, N40,M1000,W1000N\le 40, M\le 1000, W\le 1000;

For another 15%15\% of the testdata, N50,M2000N\le 50, M\le 2000;

For 100%100\% of the testdata, N60,M3000,W1000000N\le 60, M\le 3000, W\le 1000000.

Translated by ChatGPT 5