Codeforces 839D-Winter is here
来源:互联网 发布:filco minila mac 编辑:程序博客网 时间:2024/06/04 19:05
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).
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
题意:给你一个序列,找出gcd大于1的字序列,算出它们gcd*字序列的长度的和
解题思路:记录下每个数字出现的次数,从大到小枚举gcd,找出所有这个gcd的倍数的数有几个,sum[i]表示gcd为i的答案为多少,那么sum[i]=1*C(1,n)+2*C(2,n)+......+(n-1)*C(n-1,n)+n*C(n,n)-sum[2*i]-sum[3*i]-sum[4*i]......(设这个gcd的倍数的数有n个), 化简后可得sum[i]=n*2^(n-1)-sum[2*i]-sum[3*i]-sum[4*i]......最后将所有sum[i]*i相加即可
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const LL mod = 1e9 + 7;int n;int a[200009];LL cnt[1000009], sum[1000009];LL qpow(LL x, LL y){LL ans = 1;while (y > 0){if (y & 1) (ans *= x) %= mod;y >>= 1;(x *= x) %= mod;}return ans;}int main(){while (~scanf("%d", &n)){memset(cnt, 0, sizeof cnt);memset(sum, 0, sizeof sum);for (int i = 1; i <= n; i++) scanf("%d", &a[i]), cnt[a[i]]++;LL ans = 0;for (int i = 1000000; i > 1; i--){LL k = 0;for (int j = i; j <= 1000000; j += i){(sum[i] -= sum[j]) %= mod;k += cnt[j];}(sum[i] += k*qpow(2, k - 1) % mod) %= mod;(sum[i] += mod) %= mod;(ans += 1LL * i*sum[i] % mod) %= mod;}printf("%lld\n", ans);}return 0;}
- codeforces 839 D Winter is here
- Codeforces 839D Winter is here
- 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 容斥
- Codeforces-839D Winter is here(容斥原理)
- Winter is here codeforces 839d 容斥
- CodeForces 839D Winter is here (容斥原理)
- Codeforces Round #428 (Div. 2) D. Winter is here
- Codeforces-822D Winter is here(DP)
- Codeforces 839 D. Winter is here (莫比乌斯反演)
- Codeforces 839D Winter is here 组合数公式/容斥
- CodeForces Round 428 div2 839D Winter is here(数学)(详解)
- [容斥] Codeforces#428 (Div. 2) 839D. Winter is here
- 【Codeforces 839D. Winter is here】& 莫比乌斯反演
- Codeforces Round #428 (Div. 2) D. Winter is here(容斥,补题)
- C#实现 单点登录(SSO)(附源码)
- C\C++中“#ifdef __cplusplus与extern "C" { #endif” 的理解
- Android 自定义WheelView
- <javascript>函数
- linux下EOF写法梳理
- Codeforces 839D-Winter is here
- js动态创建svg
- Android基础_传感器的使用 Fragment入门(十二)
- spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default p
- shell常用命令
- Hibernate中session.save()返回值是0
- JS原生数据双向绑定
- .Net单点登录详解 (SSO)
- 一段观察者对象(MutationObserver)的使用