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=1nj=1nLCM(Ai,Aj)\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
這題也許算比較經典?
其實就是要枚舉值域來求。
記值域上界爲 NNcntxcnt_xxx 出現的次數

i=1nj=1nLCM(Ai,Aj)=i=1Nj=1NLCM(i,j)cnticntj=i=1Nj=1Nijgcd(i,j)cnticntj\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=gcd(i,j)d = gcd(i,j),然後枚舉 dd 的倍數,有
i=1Nj=1Nijgcd(i,j)cnticntj=d=1Ni=1Ndj=1Ndijd[gcd(i,j)==1]cnticntj=d=1Ndi=1Ndj=1Ndijcntidcntjdxi,xjμ(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)
然後枚舉 xx,有
ans=d=1Ndx=1Ndμ(x)i=1Ndxj=1Ndxixcntidxjxcntjdx=d=1Ndx=1Ndμ(x)x2(i=1Ndxicntidx)2ans = \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=1Nxicntixh(x)=\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{x}\right\rfloor}}i *cnt_{ix}即可
複雜度是 O(nlogn)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;
}
發佈了85 篇原創文章 · 獲贊 80 · 訪問量 4019
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章