Codeforces-839D Winter is here(容斥原理)
来源:互联网 发布:mac下好用的ftp工具 编辑:程序博客网 时间:2024/06/05 08:35
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 ifi1 < 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).
33 3 1
12
42 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
题意:有一个序列A,设子序列的最小公约数为x=gcd(b1,b2,...,bk),x>1时定义这个子序列的值为x*k,求A序列中所有子序列值的和
题解:容斥
枚举公约数x,找出x的倍数的个数n,那么x对答案的贡献为x*(1*C(n,1)+2*C(n,2)+...+n*C(n,n))
根据组合数公式i*C(n,i)=n*C(n-1,i-1),上式可转化为x*n*2^(n-1)
不过这个贡献会有重复的,因此要减去gcd为x的倍数时的贡献,只要从大到小枚举x即可。
#include<bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define x first#define y secondusing namespace std;typedef long long LL;typedef pair<int, int> PII;const int MX = 1e6 + 5;const LL mod = 1e9 + 7;LL dp[MX], t[MX];int a[MX], cnt[MX];int main() { int n, mx = 0; //freopen("in.txt", "r", stdin); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; mx = max(mx, a[i]); } t[0] = 1; for (int i = 1; i <= mx; i++) t[i] = t[i - 1] * 2 % mod; LL ans = 0; for (int k = mx; k > 1; k--) { int num = 0; for (int i = k; i <= mx; i += k) num += cnt[i]; if (!num) continue; dp[k] = num * t[num - 1] % mod; for (int i = k * 2; i <= mx; i += k) dp[k] = (dp[k] - dp[i] + mod) % mod; ans = (ans + k * dp[k]) % mod; } printf("%I64d\n", ans); return 0;}
- Codeforces-839D Winter is here(容斥原理)
- CodeForces 839D Winter is here (容斥原理)
- Codeforces 839D Winter is here【容斥原理+数学公式】
- CodeForces 839D Winter is here(数学 容斥)
- Codeforces 839 D. Winter is here 容斥
- Winter is here codeforces 839d 容斥
- Codeforces 839D Winter is here 组合数公式/容斥
- [容斥] Codeforces#428 (Div. 2) 839D. Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(容斥,补题)
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式) +容斥
- Codeforces839D Winter is here (数论:容斥原理)
- codeforces 839 D Winter is here
- Codeforces 839D Winter is here
- codeforces 839d Winter is here
- Codeforces 839D-Winter is here
- Codeforces-822D Winter is here(DP)
- Codeforces 839 D. Winter is here (莫比乌斯反演)
- Codeforces Round #428 (Div. 2) D. Winter is here
- 线性代数总结
- 【转】树的基本概念以及性质
- JeePlus后台JSP页面编写指南
- 汇编中OR AX, AX 和 AND AX, AX的作用
- 设计模式之备忘录模式
- Codeforces-839D Winter is here(容斥原理)
- 微信第三方平台开发经验总结(六):接收微信用户发送的事件以及消息
- 十六、mcg-helper业务系统单表业务模块自动化生成serviceImpl实现类
- SecureCRT强制对表进行解锁
- 挑战nbc
- Java泛型
- Neural Networks and Deep Learning 学习笔记(一)
- 数据库的9种对象
- Anaconda3中出现sklearn.model_selection不存在问题