Codeforces 622F The Sum of the k-th Powers(数论)

题目链接 The Sum of the k-th Powers

其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇……

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define rep(i, a, b)    for (int i(a); i <= (b); ++i)
 6 #define dec(i, a, b)    for (int i(a); i >= (b); --i)
 7 
 8 const int mod = 1000000007;
 9 const int N   = 1000010;
10 
11 int v[N], p[N], f[N], r[N], b[N], c[N], d[N];
12 int n, m, ret, tmp, tot;
13 
14 inline int Pow(int a, int b){
15     int t = 1;
16     for (; b; b >>= 1, a = 1LL * a * a % mod)
17         if (b & 1) t = 1LL * t * a % mod;
18     return t;
19 }
20 
21 int main(){
22 
23     scanf("%d%d", &n, &m); ++m;
24 
25     f[1] = 1; 
26     rep(i, 2, m){
27         if (!v[i]){
28             f[i] = Pow(i, m - 1);
29             p[++tot] = i;
30         }
31 
32         rep(j, 1, tot){
33             if (i * p[j] > m) break;
34             v[i * p[j]] = 1;
35             f[i * p[j]] = 1LL * f[i] * f[p[j]] % mod;
36             if (i % p[j] == 0) break;
37         }
38     }
39 
40     rep(i, 2, m) (f[i] += f[i - 1]) %= mod;
41 
42     if ((n %= mod) <= m) return 0 * printf("%d\n", f[n]);
43 
44     r[0] = r[1] = 1;
45     rep(i, 2, m) r[i] = 1LL * (mod - r[mod % i]) * (mod / i) % mod;
46     rep(i, 2, m) r[i] = 1LL * r[i - 1] * r[i] % mod;
47     rep(i, 1, m + 1) b[i] = (n - i + 1 + mod) % mod;
48     
49     c[0] = d[m + 2] = 1;
50     rep(i, 1, m + 1) c[i] = 1LL * c[i - 1] * b[i] % mod;
51     dec(i, m + 1, 1) d[i] = 1LL * d[i + 1] * b[i] % mod;
52 
53     rep(i, 0, m){
54         tmp = 1LL * f[i] * r[m - i] % mod * r[i] % mod * c[i] % mod * d[i + 2] % mod;
55         if ((m - i) & 1) (ret += mod - tmp) %= mod;
56         else (ret += tmp) %= mod;
57     }
58 
59     return 0 * printf("%d\n", ret);
60 }

 

posted @ 2017-05-02 21:11  cxhscst2  阅读(293)  评论(0编辑  收藏  举报