Codeforces Round #428 (Div. 2) D

来源:互联网 发布:c语言绝对值函数fabs 编辑:程序博客网 时间:2024/04/19 00:09
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
33 3 1
output
12
input
42 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



容斥原理

考虑设g[d]为以d的倍数为gcd的集合的贡献(即所有的集合的大小之和)

那么显然有g[d] = Σi* C(i , cnt[d])     (1 <= i <= cnt[d])

其中cnt[d]为含有d这个因子的数的个数

然后设一个f[d]为以d为gcd的集合的贡献

那么有f[d] = g[d] - f[2d] - f[3d] - f[4d] - .......

最后答案为Σd * f[d]



代码:
#include<cstdio>#include<algorithm>#include<set>#include<cstdlib>#include<vector>#include<cmath>using namespace std;typedef long long LL;const int maxn = 1001000;const LL crz = 1e9 + 7;int n;LL ans,cnt[maxn],maxd,a[maxn],f[maxn],bin[maxn];inline LL getint(){LL ret = 0;char c = getchar();while (c < '0' || '9' < c) c = getchar();while ('0' <= c && c <= '9')ret = ret * 10 + c - '0',c = getchar();return ret;}int main(){n = getint();for (int i = 1; i <= n; i++) maxd = max(maxd,a[i] = getint());bin[0] = 1;for (int i = 1; i <= n; i++) cnt[a[i]]++;for (int i = 1; i <= maxd; i++) bin[i] = (bin[i - 1] * 2) % crz;for (int i = 2; i <= maxd; i++)for (int j = 2 * i; j <= maxd; j += i)cnt[i] += cnt[j];for (int i = maxd; i >= 2; i--){if (cnt > 0) f[i] = (cnt[i] * bin[cnt[i] - 1]) % crz;for (int j = i * 2; j <= maxd; j += i)f[i] = (f[i] - f[j] + crz) % crz;}for (LL d = 2; d <= maxd; d++)ans = (ans + f[d] * d) % crz;printf("%I64d",ans);return 0;}


原创粉丝点击