Gym 101992D The Millennium Prize Problems

description

Did you hear about the Millennium Prize Problems? They are seven problems in mathematics that were stated by the Clay Mathematics Institute on May 24, 2000. A correct solution for any of these problems results in a million dollar prize being awarded by the institute to the discoverer(s).

Soliman, one of the ECPC judges came up with a new mathematics problem and claimed that it should be the eighth Millennium Prize Problem as he is pretty sure no one could solve it. Here’s the problem:

Given an array A of length N (the array can have duplicate values). What is the sum of all least common multiple (LCM) values for every pair of the array? More formally you need to calculate the following:
i = 1 n j = 1 n L C M ( A i , A j ) \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}LCM(A_i,A_j)
Tefa, one of the ECPC judges suggested that we can add it in the problem set to see if someone can come up with a solution, so can you do it?

Input

The first line of the input contains a single integer T the number of test cases, each test case consists of one line containing N+1 space-separated integers, the first integer is the number N the size of the array and the remaining integers are the elements of the array A, where 1≤N≤105 and 1≤Ai≤105.

Output

For each test case output the answer to the problem modulo 109+7.

Example

Input

2
4 2 6 12 15
5 2 3 7 11 12

Output

335
861

分析

好久没做莫比乌斯函数题了,来一发hhh
这题也许算比较经典?
其实就是要枚举值域来求。
记值域上界为 N N c n t x cnt_x x x 出现的次数

i = 1 n j = 1 n L C M ( A i , A j ) = i = 1 N j = 1 N L C M ( i , j ) c n t i c n t j = i = 1 N j = 1 N i j g c d ( i , j ) c n t i c n t j \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}LCM(A_i,A_j) = \sum\limits_{i=1}^{N}\sum\limits_{j=1}^{N}LCM(i,j)*cnt_i*cnt_j=\sum\limits_{i=1}^{N}\sum\limits_{j=1}^{N}\dfrac{i*j}{gcd(i,j)}*cnt_i*cnt_j
然后枚举 d = g c d ( i , j ) d = gcd(i,j) ,然后枚举 d d 的倍数,有
i = 1 N j = 1 N i j g c d ( i , j ) c n t i c n t j = d = 1 N i = 1 N d j = 1 N d i j d [ g c d ( i , j ) = = 1 ] c n t i c n t j = d = 1 N d i = 1 N d j = 1 N d i j c n t i d c n t j d x i , x j μ ( x ) \sum\limits_{i=1}^{N}\sum\limits_{j=1}^{N}\dfrac{i*j}{gcd(i,j)}*cnt_i*cnt_j=\sum\limits_{d=1}^{N}\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{N}{d}\right\rfloor}}i*j*d*[gcd(i,j)==1]*cnt_i*cnt_j=\sum\limits_{d=1}^{N}d\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{N}{d}\right\rfloor}}i*j*cnt_{id}*cnt_{jd}\sum\limits_{x|i,x|j}\mu(x)
然后枚举 x x ,有
a n s = d = 1 N d x = 1 N d μ ( x ) i = 1 N d x j = 1 N d x i x c n t i d x j x c n t j d x = d = 1 N d x = 1 N d μ ( x ) x 2 ( i = 1 N d x i c n t i d x ) 2 ans = \sum\limits_{d=1}^{N}d\sum\limits_{x=1}^{{\tiny\left\lfloor\dfrac{N}{d}\right\rfloor}}\mu(x)\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{dx}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{N}{dx}\right\rfloor}}i*x*cnt_{idx}*j*x*cnt_{jdx}=\sum\limits_{d=1}^{N}d\sum\limits_{x=1}^{{\tiny\left\lfloor\dfrac{N}{d}\right\rfloor}}\mu(x)*x^2(\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{dx}\right\rfloor}}i*cnt_{idx})^2

推式子推到这里做法已经出来了。
预处理
h ( x ) = i = 1 N x i c n t i x h(x)=\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{x}\right\rfloor}}i *cnt_{ix} 即可
复杂度是 O ( n l o g n ) O(nlogn)

代码如下

#include <bits/stdc++.h>
#define LL long long
#define N 100005
using namespace std;
const int mod = 1e9 + 7;
int cnt[N], p[N], mu[N], x[N], g[N], s[N], ret, maxn = N - 5;
LL t, z = 1;
int main(){
	freopen("lcm.in", "r", stdin);
	int i, j, d, n, m, T, tmp, ans;
	mu[1] = 1;
	for(i = 2; i <= maxn; i++){
		if(!x[i]) p[++ret] = x[i] = i, mu[i] = -1;
		for(j = 1; j <= ret; j++){
			t = z * i * p[j];
			if(t > maxn) break;
			x[t] = p[j];
			if(i % p[j] == 0) break;
			mu[t] = -mu[i];
		}
	}
	scanf("%d", &T);
	while(T--){
		ans = 0;
		memset(cnt, 0, sizeof(cnt));
		memset(g, 0, sizeof(g));
		memset(s, 0, sizeof(s));
		scanf("%d", &n);
		for(i = 1; i <= n; i++){
			scanf("%d", &j);
			cnt[j]++;
		}
		n = maxn;
		//for(i = 1; i <= n; i++) printf("%d %d=====\n", i, cnt[i]);
		for(i = 1; i <= n; i++){
			for(j = 1; j <= n / i; j++){
				g[i] = (g[i] + z * cnt[i * j] * j % mod) % mod;
			}
			g[i] = z * g[i] * g[i] % mod;
			//printf("%d %d\n", i, g[i]);
		}
		for(d = 1; d <= n; d++){
			tmp = 0;
			for(i = 1; i <= n / d; i++){
				tmp = (tmp + z * i * i % mod * mu[i] * g[(d * i)] % mod) % mod;	
			}
			tmp = (tmp + mod) % mod;
			ans = (ans + z * d * tmp % mod) % mod;
		}
		printf("%d\n", ans);
	}
	return 0;
}
原创文章 100 获赞 103 访问量 7558

猜你喜欢

转载自blog.csdn.net/iamhpp/article/details/104007274