CF——Codeforces Round #428 (Div. 2)D. Winter is here
2017-08-16 17:15
531 查看
D. Winter is here
题解1.分别统计a
中2-n的倍数的个数b
,设c
= 2^(b
-1)-1;
2.GCD=i的组合个数 = c[i] - ∑c[j](j为i的倍数)(推导过程略)
3.ANS = ∑a[i](a[i]≠1) + ∑(GCD=i的组合个数 )(i≠1)
AC code
#include<bits/stdc++.h> #define maxn 1000000 + 10 const int MOD = 1000000000 + 7; typedef long long LL; using namespace std; LL ans=0; LL cnt[maxn]; LL a[maxn]; LL p[maxn]; void init() { LL k = 1; p[0] = 1; for(int i = 1; i < maxn; i++) { p[i] = (p[i-1]*2); p[i] = p[i] % MOD; } // cout << p[0] << " " << p[1] << " " << p[2] << endl; } LL GCD(LL a,LL b) { if(b == 0) return a; else return GCD(b,a%b); } int main() { int n; cin >> n; init(); memset(a,0,sizeof(a)); memset(cnt,0,sizeof(cnt)); for(int i = 0; i < n; i++) { int e; scanf("%I64d",&e); a[e]++; if(e>1) ans += e; ans %= MOD; } for(int i = 2; i <= 1000000; i++) { for(int j = i; j <= 1000000;j+=i) { cnt[i] += a[j]; } // if(cnt[i]) cout << cnt[i] << " " << p[cnt[i]-1] << endl; if(cnt[i]) cnt[i] = (p[cnt[i]-1]-1)*cnt[i]%MOD; // if(cnt[i]) cout << i << " " << cnt[i] << endl; } for(int i = 1000000; i >= 2; i--) { if(cnt[i] == 0) continue; for(int j = 2*i; j <= 1000000; j+=i) { cnt[i] -= cnt[j]; cnt[i] = (cnt[i] + MOD)%MOD; } ans += cnt[i]*i; ans %= MOD; } cout << ans%MOD << endl; return 0; }
相关文章推荐
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- (容斥)Codeforces Round #428 (Div. 2) D. Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(容斥,补题)
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here 容斥
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- 【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- [Codeforces Round #428 DIV2D (CF839D)] Winter is here
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2) D. Winter is here(序列元素个数*gcd
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式) +容斥
- Codeforces Round #428 (Div. 2) - D Winter is here