【容斥原理】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 ...
随机推荐
- bzoj 2440 dfs序
首先我们可以做一遍dfs,用一个队列记录每个点进出的顺序,当每个点访问的时候que[tot++]=x,记为in[x],当结束dfs的时候que[tot++]=x,记为out[x],这样处理出来的队列, ...
- hdu 2795 Billboard(线段树+单点更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others ...
- ES6 新增的一些东西
一.常量 不允许重复定义 const a='HELLO' const a='world'//报错Uncaught SyntaxError: Identifier 'a' has already bee ...
- Linux typeof【转】
转自:http://blog.csdn.net/xiaofeng_yan/article/details/5248633 今天偶然又看到了typeof这个东西,只知道它大概是返回变量的类型,后来上网查 ...
- linux 命令行远程登录 后台运行命令的方法
linux 命令行远程登录 后台运行命令的方法 http://blog.csdn.net/isuker/article/details/55061595 Linux 技巧:让进程在后台可靠运行的几种方 ...
- openfire在内网的情况下 文件传输代理的设置
openfire在内网的情况下 文件传输代理的设置 http://blog.csdn.net/v6543210/article/details/22506565
- 如何消除类型是submit类型的按钮的默认文字 ‘确认提交’
只需要加上value="" 即可.默认的文字就可以去掉了.
- 算法入门系列2:k近邻算法
用官方的话来说,所谓K近邻算法(k-Nearest Neighbor,KNN),即是给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例(也就是上面所说的K个邻居), 这K个 ...
- beego学习笔记(1)
公司准备开发一个针对塔吊行业的APP. 后台采用微服务,docker容器部署. 准备采用go进行微服务的开发. 采用beego进行restful API的开发. 第一步,访问beego的官方网站: h ...
- 关于在C#中对类中的隐藏基类方法和重写方法的理解
最近在学习C#,在C#中的类看到重写和隐藏基类的方法这些概念.才开始感觉自己不是很理解这些概念.也区分不开这些概念.通过自己的查找资料和练习后.慢慢的理解了类中的隐藏和重写这个概念.在C#中只有在基类 ...