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
题意:给你一个序列,找出gcd大于1的字序列,算出它们gcd*字序列的长度的和
解题思路:记录下每个数字出现的次数,从大到小枚举gcd,找出所有这个gcd的倍数的数有几个,sum[i]表示gcd为i的答案为多少,那么sum[i]=1*C(1,n)+2*C(2,n)+......+(n-1)*C(n-1,n)+n*C(n,n)-sum[2*i]-sum[3*i]-sum[4*i]......(设这个gcd的倍数的数有n个), 化简后可得sum[i]=n*2^(n-1)-sum[2*i]-sum[3*i]-sum[4*i]......最后将所有sum[i]*i相加即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n;
int a[200009];
LL cnt[1000009], sum[1000009];
LL qpow(LL x, LL y)
{
LL ans = 1;
while (y > 0)
{
if (y & 1) (ans *= x) %= mod;
y >>= 1;
(x *= x) %= mod;
}
return ans;
}
int main()
{
while (~scanf("%d", &n))
{
memset(cnt, 0, sizeof cnt);
memset(sum, 0, sizeof sum);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), cnt[a[i]]++;
LL ans = 0;
for (int i = 1000000; i > 1; i--)
{
LL k = 0;
for (int j = i; j <= 1000000; j += i)
{
(sum[i] -= sum[j]) %= mod;
k += cnt[j];
}
(sum[i] += k*qpow(2, k - 1) % mod) %= mod;
(sum[i] += mod) %= mod;
(ans += 1LL * i*sum[i] % mod) %= mod;
}
printf("%lld\n", ans);
}
return 0;
}