D. Winter is here
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
【解题报告】
题意,n个数字,从中选出一些最大公约数大于1的子集,计算所有满足的子集的子集大小与对应最大公约数的积的和。
考虑枚举 gcd, f(d)=∑k 表示gcd为d的所有子集的长度和, 设 F(d)=∑f(n) 要求 d|n 。
如有cnt个数是d的倍数,易知 F(d)=1*C(cnt,1)+2*(cnt,2)+…..+cnt*C(cnt,cnt) = cnt* ( C(cnt-1,0)+C(cnt-1,1)+…..+C(cnt-1,cnt-1) ) = cnt*2^(cnt-1) 。 所以我们可以轻松地求出F(d) 。
根据莫比乌斯反演, f(d) = ∑ u(n/d) F(n) 要求 d|n 。 答案就是 ans += d*f(d) 要求 2<=d<=max 。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000010
#define mod 1000000007
#define ll long long
int n,a[N],cnt[N],vis[N];
int mu[N];
ll fpow(ll a,int b)
{
ll ans=1;
for(;b;a=a*a%mod,b>>=1) if(b&1) ans=ans*a%mod;
return ans;
}
void mobius(int mn)
{
mu[1]=1;
for(int i=1;i<=mn;++i)
for(int j=i+i;j<=mn;j+=i)
mu[j]-=mu[i];
}
int main()
{
scanf("%d",&n);
int mx=0;
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
++vis[a[i]];
mx=max(mx,a[i]);
}
for(int i=1;i<=mx;++i)
for(int j=i;j<=mx;j+=i)
cnt[i]+=vis[j];
mobius(mx);
ll ans=0;
for(int i=2;i<=mx;++i)
if(cnt[i])
{
ll tmp=0;
for(int j=i;j<=mx;j+=i)
if(mu[j/i]&&cnt[j])
{
(tmp+=(1LL*mu[j/i]*cnt[j]%mod)*fpow(2,cnt[j]-1)%mod)%=mod;
}
(ans+=1LL*i*tmp%mod)%=mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}