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 容斥的更多相关文章

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

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

  3. 【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here

    给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/j ...

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

  5. Codeforces Round #619 (Div. 2)C(构造,容斥)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; int main(){ ios::syn ...

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

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

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

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

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

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

随机推荐

  1. 关于文件I/o的原子操作

    [摘自<Linux/Unix系统编程手册>] 所有系统调用都是以原子操作方式执行的.这里是指内核保证了某系统调用中的所有步骤会作为独立操作而一次性执行,其间不会为其它进程或线程所中断. 原 ...

  2. C#使用Emit生成构造函数和属性

    假设我们需要一个名叫Kitty的类,其在Pets程序集下. 1 // specify a new assembly name 2 var assemblyName = new AssemblyName ...

  3. 【总结】瞬时高并发(秒杀/活动)Redis方案(转)

    转载地址:http://bradyzhu.iteye.com/blog/2270698 1,Redis 丰富的数据结构(Data Structures) 字符串(String) Redis字符串能包含 ...

  4. 【Ruby】Mac gem的一些坑

    前言 自上一次升级MacOS系统后出现jekyll无法构建的问题,当时处理半天.谁知道最近又升级了MacOS,荒废博客多时,今天吝啬写了一篇准备发布,构建报错,问题重新.还是记录下,以防下次升级出问题 ...

  5. centos 6.9安装python 3.6

    .下载源码包在官网按照需要下载到本地 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz .解压源码包 tar -xvf Pyt ...

  6. grails服务端口冲突解决办法-【grails】

    grails中默认的服务端口为,当本机中需要同时启动两个不同的项目时,就会造成端口冲突,比如启动第二个服务时就会报如下的错误: Server failed to start for port 8080 ...

  7. python全栈开发day67--字段类型、字段属性、ORM回顾

    一.回顾总结 https://www.cnblogs.com/maple-shaw/articles/9323320.html 1. 视图 1. CBV 和 FBV from django.views ...

  8. nginx+keepalived实现 负载均衡 高可用

    Vip: 192.168.220.18 Rip1:192.168.220.2 Rip:192.168.220.3 Rip可以配置在一个服务器上通过ip做虚拟主机 1 rs上配置环境 2 配置应用 Yu ...

  9. hive中,动态添加map和reduce的大小,以增加并行度

    map是配置mapred.max.split.size,来定义map处理文件的大小,默认是256000000字段,换算就是256M.  如果想增加map的并行度,那么就是减少map处理文件的大小即可. ...

  10. P1080 国王游戏 贪心 高精度

    题目描述 恰逢 HH国国庆,国王邀请nn 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 nn 位大臣排成一排,国王站在队伍的 ...