给你一个序列,让你对于所有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的更多相关文章

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

  2. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  3. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

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

  5. Codeforces Round #428 (Div. 2) 题解

    题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...

  6. Codeforces Round #428 (Div. 2)E. Mother of Dragons

    http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...

  7. 【Codeforces Round #428 (Div. 2) B】Game of the Rows

    [Link]:http://codeforces.com/contest/839/problem/B [Description] 给你n排的如题目所示的位置; 同一排中(1,2) 算相邻; (3,4) ...

  8. 【Codeforces Round #428 (Div. 2) C】Journey

    [Link]:http://codeforces.com/contest/839/problem/C [Description] 给一棵树,每当你到一个点x的时候,你进入x的另外一每一个出度的概率都是 ...

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

随机推荐

  1. A Simple Math Problem(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 思路:矩阵快速幂模板题,不过因为刚刚入门矩阵快速幂,所以经常把数组f存反,导致本地错误一晚,差点 ...

  2. 【ALB学习笔记】基于多线程方式的串行通信接口数据接收案例

    基于多线程方式的串行通信接口数据接收案例 广东职业技术技术学院  欧浩源 1.案例背景 在本博客的<[CC2530入门教程-06]CC2530的ADC工作原理与应用>中实现了电压数据采集的 ...

  3. 【HNOI】 小A的树 tree-dp

    [题目描述]给定一颗树,每个点有各自的权值,任意选取两个点,要求算出这两个点路径上所有点的and,or,xor的期望值. [数据范围]n<=10^5 首先期望可以转化为求树上所有点对的and,o ...

  4. H题 hdu 2520 我是菜鸟,我怕谁

    题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=2520 我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others)  ...

  5. Linux-进程间通信(六): 记录锁

    1. 记录锁:记录锁的功能是,当一个进程正在读或者修改文件的某个部分的时候,它可以阻止其他进程修改同一文件区: 2. fcntl记录锁: #include <fcntl.h> int fc ...

  6. 【bzoj1068】【SCOI2007】压缩

    一道区间dp f[i][j][0/1]表示[i,j]区间是否加入M,并且之前一位有M的最小长度 可以理解为在第一位之前有一个M 那么就可以转移了. #include<bits/stdc++.h& ...

  7. 【数位dp入门】【HDU4734】F(x)

    记录减的状态,表示还要凑多少才能达到当前值. 然后进行枚举即可.注意状态数不能重复. #include<bits/stdc++.h> #define N 10010 using names ...

  8. 图论-最小生成树-Kruskal算法

    有关概念: 最小生成树:在连通图G中,连接图G所有顶点且总权最小的边构成的树 思路: 首先对边按权从小到大排序,紧接着枚举每一条边,如果两个结点的祖先结点不同(并查集),则连上此边,直到边数等于结点数 ...

  9. JDBC原生数据库连接

    我们在开发JavaWeb项目时,常会需要连接数据库.我们以MySQL数据库为例,IDE工具为eclipse,讲述数据库连接与基本操作. 第一步,我们在Web项目的WebContent中建一个简单的前端 ...

  10. Leetcode 之Add Binary(29)

    比较简单,细节:先将字符串翻转,注意进位. string addBinary(string a, string b) { string result; int len = a.size() > ...