Codeforces Round #428 (Div. 2) D. Winter is here[数论 II]

Posted Meternal

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #428 (Div. 2) D. Winter is here[数论 II]相关的知识,希望对你有一定的参考价值。

题目:http://codeforces.com/contest/839/problem/D

题意:找出每种情况使得所有数字gcd不为1,对答案的贡献为gcd值乘数字个数。

题解:因为数字不大,可以哈希出每种数字的个数,然后从后往前,f[i]代表在gcd==i时存在的数字搭配种数。每次计算i时,要减去计算过的种数,所以从后向前计算。每个数字贡献次数为${2}^{sum-1}$(写二进制数的情况可以观察出来),所有数字贡献为$sum*{2}^{sum-1}$ 2次x次方可以先预处理取模出来。

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #pragma comment(linker, "/STACK:102400000,102400000")
 3 #include<iostream>  
 4 #include<cstdio>  
 5 #include<fstream>  
 6 #include<iomanip>
 7 #include<algorithm>  
 8 #include<cmath>  
 9 #include<deque>  
10 #include<vector>
11 #include<bitset>
12 #include<queue>  
13 #include<string>  
14 #include<cstring>  
15 #include<map>  
16 #include<stack>  
17 #include<set>
18 #include<functional>
19 #define pii pair<int, int>
20 #define mod 1000000007
21 #define mp make_pair
22 #define pi acos(-1)
23 #define eps 0.00000001
24 #define mst(a,i) memset(a,i,sizeof(a))
25 #define all(n) n.begin(),n.end()
26 #define lson(x) ((x<<1))  
27 #define rson(x) ((x<<1)|1) 
28 #define inf 0x3f3f3f3f
29 typedef long long ll;
30 typedef unsigned long long ull;
31 using namespace std;
32 const int maxn = 1e6 + 5;
33 ll savepow[maxn];
34 int cnt[maxn];
35 ll f[maxn];
36 int main()
37 {
38     ios::sync_with_stdio(false);
39     cin.tie(0); cout.tie(0);
40     int i, j, k, m, n;
41     cin >> n;
42     for (int i = 1; i <= n; ++i)
43     {
44         cin >> k;
45         cnt[k]++;
46     }
47     savepow[0] = 1;
48     for (int i = 1; i <= 1000000; ++i)savepow[i] = (savepow[i - 1] << 1) % mod;
49     ll ans = 0;
50     for (ll i = 1000000; i > 1; --i)
51     {
52         int sum = 0;
53         for (int j = i; j <= maxn; j += i)
54             sum += cnt[j];
55         if (sum)
56         {
57             ll tempans = 0;
58             tempans = 1LL * sum*savepow[sum - 1] % mod;
59             for (int j = i + i; j <= maxn; j += i)
60                 tempans = (tempans - f[j] + mod) % mod;
61             f[i] = tempans;
62             ans = (ans + i*f[i]) % mod;
63         }
64     }
65     cout << ans << endl;
66     return 0;
67 }

 

以上是关于Codeforces Round #428 (Div. 2) D. Winter is here[数论 II]的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces839B[思维] Codeforces Round #428 (Div. 2)

Codeforces Round #428A

Codeforces Round #428 (Div. 2) C-Journey

Codeforces Round #428 B

Codeforces Round #428C

codeforces round #428 div2