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\) ...
随机推荐
- Windows10右键添加“在此处打开命令窗口”
cmdHere.reg: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere] @= ...
- [转] mongoose 之Shema
总 之见到我写点啥简直是奇迹,由于现在喜欢上玩转node.js +mongoose,个人爱好,靠近前端的又是英文文档,苦逼得很,悟出来一个写一个吧.之前喜欢误打误撞,网上搜索一点解决一下问题,后来实在 ...
- javascript OOP(下)(九)
一.javascript模拟重载 java中根据参数类型和数量的区别来实现重载,javascript弱类型,没有直接的机制实现重载,javascript中参数类型不确定和参数个数任意,通过判断实际传入 ...
- Typescript知识梳理
概述 TypeScript简称TS,具有类型系统,且是JavaScript的超集. 它可以编译成普通的JavaScript代码.TypeScript支持任意浏览器,任意环境,任意系统并且是开源的.通过 ...
- [转]教你十分钟下载并破解IntelliJ IDEA(2017)
来源:http://www.itwendao.com/article/detail/400687.html 温馨提示:IntelliJ IDEA(2017)需要安装JDK8以上才能运行 如果你是JDK ...
- 基于spring security 实现前后端分离项目权限控制
前后端分离的项目,前端有菜单(menu),后端有API(backendApi),一个menu对应的页面有N个API接口来支持,本文介绍如何基于spring security实现前后端的同步权限控制. ...
- 怎样把linux客户端用户禁止用 su命令来切换用户
系统中有一个组叫做“wheel”,我们可以利用该组实现一些特殊的功能.我们可以将拥有su使用权限的用户加入到wheel组中并且对该组进行限制,那么只有在该组中的用户才有su的使用权限. 如要实现该功能 ...
- 函数防抖 debounce
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Java中常见的排序方式-冒泡排序(升序)
[基本思想] 假设数组为int[] a = { 49, 38, 65, 97, 76, 13, 27 },数组元素个数为7个. 第1轮比较:先是a[0]与a[1]比较,大于则先交换,再比较a[1]和a ...
- Python图表数据可视化Seaborn:3. 线性关系数据| 时间线图表| 热图
1. 线性关系数据可视化 lmplot( ) import numpy as np import pandas as pd import matplotlib.pyplot as plt import ...