codeforces839D Winter is here 数论,莫比乌斯反演

D. Winter is here
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.

He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans.

Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7).

Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.

Input

The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers.

Output

Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).

Examples
input
3
3 3 1
output
12
input
4
2 3 4 6
output
39
Note

In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12

观察到求gcd和的这种题目我只会无脑的莫比乌斯反演。

先推公式吧

Σk*gcd(ai1,ai2,.....,aik) = Σd Σk(其中gcd(ai1,ai2,...,aik) = d)

我们转换为求个g(x) = Σk(其中gcd(ai1,ai2,...,aik) = x)

显然想到要用莫比乌斯反演了

令f(x) = Σg(d) 满足{x|d}

那么f(x) = Σk 满足{x|ai1,x|ai2,...,x|aik}

我们用num数组统计每个数字出现的次数

那么我求f(x)就可以直接统计出x的倍数出现了多少次,极为T

那么f(x)就相当于一个组合数公式的求和

f(x)  = 1*C(T,1) + 2*C(T,2) + ... +n*C(n,n) = (C(n-1,0) +C(n-1,1) +...+C(n-1,n-1) )*n

= n*2^(n-1)

那么g(x) = Σmu[d/x] * f(d) {满足d|x}

求出g(x)之后

答案就是Σd*g(d)

代码:

#include 
#include  
using namespace std;
typedef long long LL;
const LL MOD = 1e9+7;
int n;
const int MAXN = 1000007;
int aa[MAXN];
LL f[MAXN];
LL g[MAXN];
const int maxn = 1000007;  
int mu[maxn],a[maxn],prime[maxn],cnt[maxn],num[maxn];  
bool vis[maxn];  
int pnum;
void mobeius(int N)  
{  
    pnum=0;  
    vis[1]=mu[1]=1;  
    for(int i=2;i<=N;i++)  
    {  
        if(!vis[i])  
        {  
            mu[i]=-1;  
            prime[pnum++]=i;  
        }  
        for(int j=0;jN)break;  
            vis[i*prime[j]]=1;  
            if(i%prime[j]==0)  
            {  
                mu[i*prime[j]]=0;  
                break;
            }  
            mu[i*prime[j]]=-mu[i];  
        }  
    }  
}  
LL mod_pow(LL x,LL n){
	LL ans = 1;
	while(n){
		if(n & 1){
			ans = ans * x % MOD;
		}
		x = x * x % MOD;
		n >>= 1;
	}
	return ans % MOD;
}
void init(){
	LL sum = 0;
	for(int x = 2;x <= 1000000;x++){
		sum = 0;
		for(int j = x;j <= 1000000;j += x){
			sum += aa[j];
		}
		if(sum)
			f[x] = sum * mod_pow(2,sum-1) % MOD;
	}
	for(int x = 2;x <= 1000000;x++){
		for(int j = x;j <= 1000000;j += x){
			g[x] = (g[x] + mu[j/x] * f[j])%MOD;
		}
	}
	LL ans = 0;
	for(int x = 2;x <= 1000000;x++){
		ans = (ans + x * g[x])%MOD;
	}
	printf("%lld\n",ans);
}

int main(){
	mobeius(1000001);
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		int tmp;
		scanf("%d",&tmp);
		aa[tmp] ++;
	}
	init();
	
	return 0;
}







你可能感兴趣的:(ACM-ICPC训练题解,数论与组合数学系列,CODEFORCES训练记录)