Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)

转载出处:http://blog.csdn.net/jaihk662/article/details/77161436


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


题意:

如果一个子序列的GCD为1,那么这个子序列的价值为0,否则子序列价值为子序列长度*子序列GCD

给出n个数,求这n个数所有子序列的价值和


题解:

考虑暴力每一个GCD,求出这个GCD对答案的贡献

如果枚举到x,n个数中有sum[x]个数是x的倍数,那么x对答案的贡献就是


但这可能会算重复,因为由全是x倍数组成的子序列它的GCD不一定是x啊

所以sum[x]还要减掉所有的sum[y],其中y为x的倍数

那么令val[x]为x对答案的贡献,就有


所以枚举GCD时要倒过来枚举

最后求一波和就是答案


[cpp]  view plain  copy
 print ?
  1. #include<stdio.h>  
  2. #define mod 1000000007  
  3. #define LL long long  
  4. LL cnt[1000005], sum[1000005];  
  5. LL Pow(LL a, LL b)  
  6. {  
  7.     LL now;  
  8.     now = 1;  
  9.     while(b)  
  10.     {  
  11.         if(b%2)  
  12.             now = now*a%mod;  
  13.         a = a*a%mod;  
  14.         b /= 2;  
  15.     }  
  16.     return now;  
  17. }  
  18. int main(void)  
  19. {  
  20.     LL ans, i, j, n, x;  
  21.     scanf("%lld", &n);  
  22.     for(i=1;i<=n;i++)  
  23.     {  
  24.         scanf("%lld", &x);  
  25.         cnt[x]++;  
  26.     }  
  27.     ans = 0;  
  28.     for(i=1000000;i>=2;i--)  
  29.     {  
  30.         x = 0;  
  31.         for(j=i;j<=1000000;j+=i)  
  32.         {  
  33.             sum[i] -= sum[j];  
  34.             x += cnt[j];  
  35.         }  
  36.         sum[i] += x*Pow(2, x-1)%mod;  
  37.         ans = ((ans+sum[i]*i)%mod+mod)%mod;  
  38.     }  
  39.     printf("%lld\n", ans);  
  40.     return 0;  
  41. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值