Codeforces 839D Winter is here - 暴力 - 容斥原理
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.
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.
Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).
3
3 3 1
12
4
2 3 4 6
39
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
题目大意 给定n,集合A,设
表示把这个集合内的所有数求最大公约数的结果,求
。
根据常用套路,套一个循环去枚举gcd的结果,然后再求系数,于是有

现在设
,于是有
现在考虑求f(i)。可以想到容斥原理。
先假设所有的集合的gcd是i的倍数都符合条件然后计算答案(给定数集A中所有是i的倍数的数组成的集合任选一个子集),然后再减去f(2i), f(3i),...
现在要面临两个问题
- 第一次求值如何处理?
首先把式子写出来,设这个集合的大小为n,那么有
因为

对两边同时进行求导得到

再带入x = 1得到

- 设
为是i的倍数的数的个数,如何快速求出
?
根据定义式有

显然超时。虽然这是暴力,但是不够优美。
设
表示,集合A中恰好为i的数有多少个。然后就可以得到总时间复杂度为O(mlog2m)的暴力:

最后求求和就完事了。
Code
/**
* Codeforces
* Problem#839D
* Accepted
* Time: 171ms
* Memory: 15400k
*/
#include <bits/stdc++.h>
using namespace std; const int lim = 1e6 + ;
const int moder = 1e9 + ; int n;
int *a;
int *pow2;
int cnt[lim], counter[lim];
int f[lim];
int res = ; inline void init() {
scanf("%d", &n);
a = new int[(n + )];
pow2 = new int[(n + )];
pow2[] = ;
for(int i = ; i <= n; i++) {
scanf("%d", a + i);
counter[a[i]]++;
pow2[i] = (pow2[i - ] << ) % moder;
}
} inline void solve() {
for(int i = ; i < lim; i++)
for(int j = i; j < lim; j += i)
cnt[i] += counter[j]; for(int i = lim - ; i > ; i--) {
if(!cnt[i]) continue;
f[i] = (cnt[i] * 1LL * pow2[cnt[i] - ]) % moder;
for(int j = i << ; j < lim; j += i)
f[i] = (f[i] - f[j]) % moder;
if(f[i] < ) f[i] += moder;
res = (res + (f[i] * 1LL * i) % moder) % moder;
} printf("%d\n", res);
} int main() {
init();
solve();
return ;
}
更新日志
- 2017-11-30 更新两处指数错误
Codeforces 839D Winter is here - 暴力 - 容斥原理的更多相关文章
- Codeforces 839D Winter is here(容斥原理)
[题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...
- CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...
- Codeforces 839D Winter is here【数学:容斥原理】
D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces 839D Winter is here
链接:CF839D 题目大意 给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\). 选择其中任意\(len\ ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- Codeforces Gym 100015H Hidden Code 暴力
Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...
- Codeforces gym 100685 A. Ariel 暴力
A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...
- Codeforces Gym 100637G G. #TheDress 暴力
G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...
- [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力
Couple Cover Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Descri ...
随机推荐
- 两个integer比较时为什么有时候会失效?
当数值不在 -128~127范围时就不会进行缓存操作,它会选择进行new integer()的操作.这样他们被分配到堆里面.[这个可以去查看integer.valueof()的源码].在用==或者!= ...
- iOS应用安全开发,你不知道的那些事
来源:http://www.csdn.net/article/2014-04-30/2819573-The-Secret-Of-App-Dev-Security 摘要:iOS应用由于其直接运行在手机上 ...
- Mac版StarUML破解方法
StarUML是用nodejs写的.确切的说是用Electron前端框架写的.新版本中所有的starUML源代码是通过asar工具打包而成.确切的代码位置在“%LOCALAPPDATA%\Progra ...
- CSS的一个小bug,Gradient has outdated direction syntax. New syntax is like `to left` instead of `right`.
在vue重新渲染页面的时候,报了一个错误: 翻译了报错信息后,Gradient has outdated direction syntax. New syntax is like to left in ...
- 使用opencv去操作树莓派摄像头保存图片和视频
利用树莓派的摄像头去学习opencv的基本操作 —— 保存图片和视频 1.使用Opencv去控制树莓派的摄像头拍照并保存到本地,主要使用cv2和numpy库 #!/usr/bin/python3 # ...
- 关于free的使用疑惑
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "mainc26. ...
- 基于JFinal中搭建wopi协议支撑办法
1.添加maven依赖 <dependency> <groupId>com.github.icecooly</groupId> <artifactId> ...
- drf框架总结复习(1)
Serializers 序列化组件 为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成j ...
- 编程小白入门分享一:git的最基本使用
git简介 引用了网上的一张图,这张图清晰表达git的架构.workspace是工作区,可以用编辑器直接编辑其中的文件:Index/Stage是暂存区,编辑后的文件可以添加到(add)暂存区:Repo ...
- 2019牛客暑期多校训练营(第三场)G: Removing Stones(启发式分治)
题意:给定N,表示N堆石子,每堆石子数为a[],问多少个区间,可以满足“石子总和若为偶数,那么可以两两取来自不同堆的石子,直到取完: 如果为奇数,那么排除其中一个,然后可以两两取来自不同堆的石子,直到 ...