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 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).
Sample Input
3
3 3 1
Sample Output
12
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;
}
Codeforces Round #428 (Div. 2) D. Winter is here 容斥的更多相关文章
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...
- Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥
E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...
- 【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here
给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/j ...
- Codeforces Round #330 (Div. 2)B. Pasha and Phone 容斥
B. Pasha and Phone Pasha has recently bought a new phone jPager and started adding his friends' ph ...
- Codeforces Round #619 (Div. 2)C(构造,容斥)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; int main(){ ios::syn ...
- 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 #589 (Div. 2)-E. Another Filling the Grid-容斥定理
Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...
随机推荐
- C#学习-属性是对字段的扩展
属性是对字段的扩展. 根据面向对象语言的封装思想,字段最好设为private,因为这样可以防止客户端直接对字段进行篡改,从而保证了内部成员的完整性. 于是为了访问类中的私有字段,C#提供了属性这种机制 ...
- Sway
啥是Sway: http://livesino.net/archives/7520.live 地址:http://sway.com 现在又可以申请了 试试效果
- [转]PO BO VO DTO POJO DAO概念及其作用(附转换图)
来源:http://www.blogjava.net/vip01/archive/2013/05/25/92430.html J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候 ...
- 【HDU】HDU5664 Lady CA and the graph
原题链接 题解 距离省选只有一周了我居然才开始数据结构康复计划 这题很简单,就是点分树,然后二分一个值,我们计算有多少条路径大于这个值 对于一个点分树上的重心,我们可以通过双指针的方法求出它子树里的路 ...
- Python_configparser模块
configparser : 用于处理ini结构相似的文件,格式类似于: [DEFAULT] # 相当于一个分组 # option # 注释,说明性文字 ,默认以# 或‘:’开头的行 option1 ...
- ELK 环境搭建3-Logstash
一.Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的.多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件或者中间件. 二.搭建 1.因为要涉及到收 ...
- Vue2.0学习——axios用法详解
功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 http请求 支持 Promise API 拦截请求和响应 转换请求和响应数据 自动转换 JSON 数据 客 ...
- 微信小程序—文件系统
文件系统 文件系统是小程序提供的一套以小程序和用户维度隔离的存储以及一套响应的管理接口.通过wx.getFilesSystemManager()可以获取到全局唯一的文件管理器,所有文件管理操作通过Fi ...
- HDU4185 Oil Skimming 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8231146.html 题目传送门 - HDU4185 题意概括 每次恰好覆盖相邻的两个#,不能重复,求最大覆盖次 ...
- P1026 统计单词个数 区间dp
题目描述 给出一个长度不超过200200的由小写英文字母组成的字母串(约定;该字串以每行2020个字母的方式输入,且保证每行一定为2020个).要求将此字母串分成kk份(1<k \le 401& ...