【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here
给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和。
设sum(i)为i的倍数的数的个数,可以通过容斥算出来。
具体看这个吧:http://blog.csdn.net/jaihk662/article/details/77161436。
注意1*C(n,1)+2*C(n,2)+...+n*C(n,n)=n*2^(n-1)。
#include<cstdio>
using namespace std;
typedef long long ll;
#define MOD 1000000007ll
int n;
int a[200005],cnt[1000005];
ll sum[1000005],ans,pw[1000005];
int main(){
pw[0]=1;
for(int i=1;i<=1000000;++i){
pw[i]=(pw[i-1]*2ll)%MOD;
}
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
++cnt[a[i]];
}
for(int i=1000000;i>=2;--i){
int all=cnt[i];
for(int j=i*2;j<=1000000;j+=i){
sum[i]=(sum[i]+MOD-sum[j])%MOD;
all+=cnt[j];
}
sum[i]=(sum[i]+((ll)all*pw[all-1])%MOD)%MOD;
ans=(ans+((ll)i*sum[i])%MOD)%MOD;
}
printf("%I64d\n",ans);
return 0;
}
【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here的更多相关文章
- Codeforces Round #428 (Div. 2) D. Winter is here 容斥
D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...
- CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- CodeForces 839B - Game of the Rows | Codeforces Round #428 (Div. 2)
血崩- - /* CodeForces 839B - Game of the Rows [ 贪心,分类讨论] | Codeforces Round #428 (Div. 2) 注意 2 7 2 2 2 ...
- Codeforces Round #428 (Div. 2) 题解
题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...
- Codeforces Round #428 (Div. 2)E. Mother of Dragons
http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...
- 【Codeforces Round #428 (Div. 2) B】Game of the Rows
[Link]:http://codeforces.com/contest/839/problem/B [Description] 给你n排的如题目所示的位置; 同一排中(1,2) 算相邻; (3,4) ...
- 【Codeforces Round #428 (Div. 2) C】Journey
[Link]:http://codeforces.com/contest/839/problem/C [Description] 给一棵树,每当你到一个点x的时候,你进入x的另外一每一个出度的概率都是 ...
- Codeforces Round #428 (Div. 2)A,B,C
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- 取(m堆)石子游戏 HDU2176(Nim博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 题目: Problem Description m堆石子,两人轮流取.只能在1堆中取.取完者胜. ...
- winform Textbox像百度一下实现下拉显示
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- perl 在win下输出中文乱码问题
use utf8; my $name = '你好'; binmode(STDOUT, ":encoding(gbk)"); print $name,"\n"; ...
- CMD命令行下载文件
远程执行sct的另一种姿势 cscript /b C:\Windows\System32\Printing_Admin_Scripts\zh-CN\pubprn.vbs 127.0.0.1 scrip ...
- SSH 登录失败:Host key verification failed 的处理方法
原因就是你之前已经登录过这个服务器了然后改系统啥的了.导致目标主机 key 值不正确.直接把本机的key文件删除即可 sudo rm /home/yourname/.ssh/known_hosts
- 通过编译函数库来学习GCC【转】
转自:http://blog.csdn.net/u012365926/article/details/51446295 基本概念 什么是库 在windows平台和linux平台下都大量存在着库. 本质 ...
- Linux 入门记录:二、Linux 文件系统基本结构
一.树状目录结构 Linux 文件系统是一个倒置的单根树状结构.文件系统的根为"/":文件名严格区分大小写:路径使用"/"分割(Windows 中使用" ...
- 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...
- centos7下opencv的安装
os:centos7 opencv:opencv3.0.0 for linux reference:http://www.cnblogs.com/xixixing/p/6096057.html det ...
- 关于aspxgridview里面过长内容只显示的一部分的处理方案
protected void g_Message_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventAr ...