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.
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.
Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).
3
3 3 1
12
4
2 3 4 6
39
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
const LL N = 1000000;
const LL p = 1e9+7;
LL num[N+1];
LL pos[N+1];
LL f[N+1];
void init()
{
memset(f,0,sizeof f);
pos[0]=1;
for(LL i=1;i<=N;i++)
{
pos[i]=pos[i-1]*2%p;
}
}
int main()
{
init();
LL n;
scanf("%lld",&n);
for(LL i=1;i<=n;i++)
{
LL a;scanf("%lld",&a);
num[a]++;
}
LL ans=0;
for(LL i=N;i>=2;i--)
{
LL x=0;
for(LL j=i;j<=N;j+=i)
{
x+=num[j];
}
if(x==0) continue;
f[i]=x*pos[x-1]%p;
for(LL j=i*2;j<=N;j+=i)
{
f[i]-=f[j];
f[i]=(f[i]+p)%p;
}
ans=(ans+i*f[i])%p;
}
printf("%lld\n",ans);
}

本文介绍了一个算法问题,模拟《权力的游戏》中约翰·雪诺评估其军队实力的方式。任务是通过计算所有士兵组合的加权总和来确定军队的整体实力,其中每个组合的权重由士兵数量与最大公约数的乘积决定。
1343

被折叠的 条评论
为什么被折叠?



