CDOJ 811 GCD

建议先看这篇文章.
很显然,所求的式子可以化为
$$\sum_{i=1}^{n}i^{k}\left(2\phi\left(\frac{n}{i}\right)-1\right)$$
其中,$\phi\left(\frac{n}{i}\right)$是欧拉函数的前缀和,这个可以用刚刚提到的那篇文章的方法在$o\left ( n^{\frac{2}{3}} \right )$复杂度求解。而左边那个$k$次方前缀和可以直接百度求值,或者暴力插值求解。
反正就这样搞搞就行了,智障少年写不动题解了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//created by missever
#include<bits/stdc++.h>
#define MAX 1000000007
using namespace std;
typedef long long LL;
const int maxn = 1e7+5;
const int rev = (MAX + 1) >> 1;
bool flag[maxn];
int phi[maxn],pri[maxn];
int cnt = 0,k;
LL sum[maxn];
LL a[8],b[8],c[8];
map<LL,LL> p;
void Get_phi()
{
phi[1] = 1;
sum[1] = 1;
for(int i = 2; i < maxn; i++)
{
if(!flag[i])
{
pri[cnt++] = i;
phi[i] = i - 1;
}
for(int j = 0; j < cnt; j++)
{
if(i * pri[j] > maxn) break;
flag[i * pri[j]] = true;
if(i % pri[j] == 0)
{
phi[i * pri[j]] = pri[j] * phi[i];
break;
}
else phi[i * pri[j]] = (pri[j] - 1) * phi[i];
}
sum[i] = (sum[i - 1] + phi[i]) % MAX;
}
}
LL Get_sumpri(LL n)
{
if(n < maxn) return sum[n];
if(p.find(n) != p.end()) return p[n];
LL i,j;
LL s = n % MAX * ((n + 1) % MAX) % MAX * rev % MAX;
for(i = 2; i <= n; i = j + 1)
{
j = n / (n / i);
s = (s - (j - i + 1) % MAX * Get_sumpri(n / i) % MAX) % MAX;
}
s = (s + MAX) % MAX;
p[n] = s;
return s;
}
void extend_Euclid(LL a, LL b, LL &x, LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
extend_Euclid(b, a % b, x, y);
LL tmp = x;
x = y;
y = tmp - (a / b) * y;
}
LL Inv(LL a, LL b)
{
LL x, y;
extend_Euclid(a, b, x, y);
return (x % b + b) % b;
}
LL Get_kpow(LL n)
{
if(n <= k + 1) return a[n];
LL i,j;
LL s = 0,f;
for(i = 0;i <= k + 1; i++)
{
f = 1;
for(j = 0;j <= k + 1; j++)
{
if(j != i) f = (n - j) % MAX * f % MAX;
}
s = (s + f * c[i] % MAX) % MAX;
}
return (s + MAX) % MAX;
}
LL Get_ans(LL n)
{
LL i,j;
LL s = 0,f = 0,g;
for(i = 1; i <= n; i = j + 1)
{
j = n / (n / i);
g = Get_kpow(j);
s = (s + (Get_sumpri(n / i) * 2LL - 1LL) % MAX * ((g - f + MAX) % MAX) % MAX) % MAX;
f = g;
}
return (s + MAX) % MAX;
}
int main()
{
Get_phi();
LL n;
int i,j;
scanf("%lld",&n);
scanf("%d",&k);
b[0] = 1;
for(i = 1;i <= k + 1; i++)
{
a[i] = i;
for(j = 1;j < k; j++) a[i] = a[i] * i % MAX;
a[i] = (a[i] + a[i - 1]) % MAX;
b[i] = b[i - 1] * i % MAX;
}
for(i = 1;i <= k + 1; i++)
{
c[i] = a[i] * Inv(b[i] * b[k + 1 - i] % MAX,MAX) % MAX;
if((k + 1 - i) & 1) c[i] = -c[i];
}
printf("%lld\n",Get_ans(n));
return 0;
}