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:
<munderover> i = 1 n </munderover> <munderover> j = 1 n </munderover> L C M ( A i , A j ) \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}LCM(A_i,A_j) i=1nj=1nLCM(Ai,Aj)
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 N c n t x cnt_x cntx x x x 出现的次数

<munderover> i = 1 n </munderover> <munderover> j = 1 n </munderover> L C M ( A i , A j ) = <munderover> i = 1 N </munderover> <munderover> j = 1 N </munderover> L C M ( i , j ) c n t i c n t j = <munderover> i = 1 N </munderover> <munderover> j = 1 N </munderover> 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 i=1nj=1nLCM(Ai,Aj)=i=1Nj=1NLCM(i,j)cnticntj=i=1Nj=1Ngcd(i,j)ijcnticntj
然后枚举 d = g c d ( i , j ) d = gcd(i,j) d=gcd(i,j),然后枚举 d d d 的倍数,有
<munderover> i = 1 N </munderover> <munderover> j = 1 N </munderover> i j g c d ( i , j ) c n t i c n t j = <munderover> d = 1 N </munderover> <munderover> i = 1 <mstyle mathsize="0&#46;5em"> N d </mstyle> </munderover> <munderover> j = 1 <mstyle mathsize="0&#46;5em"> N d </mstyle> </munderover> i j d [ g c d ( i , j ) = = 1 ] c n t i c n t j = <munderover> d = 1 N </munderover> d <munderover> i = 1 <mstyle mathsize="0&#46;5em"> N d </mstyle> </munderover> <munderover> j = 1 <mstyle mathsize="0&#46;5em"> N d </mstyle> </munderover> i j c n t i d c n t j d <munder> x i , x j </munder> μ ( 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) i=1Nj=1Ngcd(i,j)ijcnticntj=d=1Ni=1dNj=1dNijd[gcd(i,j)==1]cnticntj=d=1Ndi=1dNj=1dNijcntidcntjdxi,xjμ(x)
然后枚举 x x x,有
a n s = <munderover> d = 1 N </munderover> d <munderover> x = 1 <mstyle mathsize="0&#46;5em"> N d </mstyle> </munderover> μ ( x ) <munderover> i = 1 <mstyle mathsize="0&#46;5em"> N d x </mstyle> </munderover> <munderover> j = 1 <mstyle mathsize="0&#46;5em"> N d x </mstyle> </munderover> i x c n t i d x j x c n t j d x = <munderover> d = 1 N </munderover> d <munderover> x = 1 <mstyle mathsize="0&#46;5em"> N d </mstyle> </munderover> μ ( x ) x 2 ( <munderover> i = 1 <mstyle mathsize="0&#46;5em"> N d x </mstyle> </munderover> 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 ans=d=1Ndx=1dNμ(x)i=1dxNj=1dxNixcntidxjxcntjdx=d=1Ndx=1dNμ(x)x2(i=1dxNicntidx)2

推式子推到这里做法已经出来了。
预处理
h ( x ) = <munderover> i = 1 <mstyle mathsize="0&#46;5em"> N x </mstyle> </munderover> i c n t i x h(x)=\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{N}{x}\right\rfloor}}i *cnt_{ix} h(x)=i=1xNicntix即可
复杂度是 O ( n l o g n ) 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;
}