Codeforces Round #428 (Div. 2) - D Winter is here
2017-08-13 22:36
507 查看
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
output
input
output
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>1的组合,他们的价值为gcd*个数。求总价值。
POINT:
遍历所有GCD=b,找出以b为gcd的数的个数x。那么它的价值为:b*(0*C(x,0)+1*C(x,1)+2*C(x,2)+……+x*C(x,x)).
可化为x*2^(x-1).怎么化的下面有两张图。
然后用容斥。
![](https://img-blog.csdn.net/20170813223350714?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfVHJlZWVlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20170813223443859?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfVHJlZWVlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码:
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>1的组合,他们的价值为gcd*个数。求总价值。
POINT:
遍历所有GCD=b,找出以b为gcd的数的个数x。那么它的价值为:b*(0*C(x,0)+1*C(x,1)+2*C(x,2)+……+x*C(x,x)).
可化为x*2^(x-1).怎么化的下面有两张图。
然后用容斥。
代码:
#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); }
相关文章推荐
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here 容斥
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式) +容斥
- Codeforces Round #428 (Div. 2) D. Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- CF——Codeforces Round #428 (Div. 2)D. Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- [Codeforces Round #428 DIV2D (CF839D)] Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(容斥,补题)