Codeforces 839D Winter is here【容斥原理+数学公式】

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


题目大意:


让你计算所有的子序列价值,一个序列(a1,a2,a3..............ak)的Gcd如果大于1,那么其价值贡献度为K*Gcd.


思路:


很套路的这类题,和Bestcoder这个题有异曲同工之妙:Hdu 5212


①首先我们知道,如果直接暴力去按照代码去求解的话,时间复杂度是很爆炸的。所以我们考虑优化。


②我们知道,对于一个因子数x来讲,计算其贡献的时候,我们可以找到x这个数的所有的倍数的个数tot,那么任意从中取出几个数来,其gcd的结果都一定是x的倍数(x,2x,3x,4x,5x......................................),那么我们可以枚举因字数,然后找到其倍数的个数,整体时间复杂度为O(nlogn);


③显然,如果我们统计过了x的倍数的个数tot,使得统计答案之后,如果我们再遇到了2x这个因子,也这样计算的话,会出现重复。

那么我们就要考虑去重问题,相对而言,我们可以设定f(x)表示因子x所贡献的价值,那么显然:f(x)=Val-f(2x)-f(3x)-f(4x)-......................................


④这里Val显然=x*【C(tot,1)+2*C(tot,2)+3*C(tot,3)+4*C(tot,4)+.............+tot*C(tot,tot)】。我们可以推出后边一长串式子得到公式:



⑤那么我们过程维护一下这个减法即可,我们可以从大到小枚举因子数,然后计算之前将其倍数的答案减去即可。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
#define mod 1000000007
ll a[3035000];
ll cnt[3035000];
ll F[3035000];
ll kuaisumi(ll a,ll b)
{
    if(b<0)return 0ll;
    a%=mod;
    ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=ans*a;
            ans%=mod;
        }
        a=a*a;
        a%=mod;
        b/=2;
    }
    return ans;
}
int main()
{
    ll n;
    while(~scanf("%I64d",&n))
    {
        memset(cnt,0,sizeof(cnt));
        memset(F,0,sizeof(F));
        for(ll i=1; i<=n; i++)scanf("%I64d",&a[i]);
        for(ll i=1; i<=n; i++)
        {
            for(ll j=1; j*j<=a[i]; j++)
            {
                if(a[i]%j!=0)continue;
                cnt[j]++;
                if(j*j!=a[i])cnt[a[i]/j]++;
            }
        }
        ll ans=0;
        for(ll i=1000000; i>=2; i--)
        {
            if(cnt[i]==0)continue;
            F[i]=cnt[i]*kuaisumi(2,cnt[i]-1)%mod;
            for(ll j=i*2;j<=1000000;j+=i)
            {
                F[i]-=F[j];
                F[i]=(F[i]%mod+mod)%mod;
            }
            ll val=i;
            ans+=(val*F[i]%mod+mod)%mod;
        }
        printf("%I64d\n",(ans%mod+mod)%mod);
    }
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值