Codeforces Round #428 (Div. 2) D. Winter is here 容斥

D. Winter is here

題目鏈接:

http://codeforces.com/contest/839/problem/Dc++

Description

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.spa

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.rest

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).code

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.ip

Input

The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army.ci

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers.element

Output

Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).get

Sample Input

3
3 3 1input

Sample Output

12it

Hint

題意

讓你考慮全部gcd大於1的集合。這個集合的貢獻是gcd乘上集合的大小。
問你總的貢獻是多少。

題解:

令cnt[i]表示因子含有i的數的個數
\(f(i)=1*C(cnt[i],1)+2*C(cnt[i],2)+...+cnt[i]*C(cnt[i],cnt[i])\)
那麼ans[i]=f(i)*2^(cnt[i]-1)-f(2i)-f(3i)-....
ans[i]表示gcd爲i的答案

代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
const int mod = 1e9+7;

long long p[maxn],w[maxn];
long long cnt[maxn];
long long a[maxn];
int n;
int main(){
    p[0]=1;
    for(int i=1;i<maxn;i++)p[i]=p[i-1]*2ll%mod;
    for(int i=1;i<maxn;i++)w[i]=i;
    for(int i=2;i<maxn;i++){
        for(int j=i+i;j<maxn;j+=i){
            w[j]-=w[i];
        }
    }
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        cnt[a[i]]++;
    }
    long long ans = 0;
    for(int i=2;i<maxn;i++){
        long long tmp = 0;
        for(int j=i;j<maxn;j+=i){
            tmp+=cnt[j];
        }
        ans=(ans+(tmp*w[i]%mod)*p[tmp-1]%mod)%mod;
    }
    cout<<ans<<endl;
}
相關文章
相關標籤/搜索