-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51nod-1847.cpp
97 lines (94 loc) · 2.48 KB
/
51nod-1847.cpp
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
#include <bits/stdc++.h>
#define uint QAQAQ
using namespace std;
typedef unsigned uint;
// typedef unsigned long long uint;
typedef unsigned long long ull;
const int N = 1e6 + 5, M = 1e3 + 10, K = 52;
uint n, c, k, p[N], pk[N], spk[N], phi[N];
uint pos[N], idx1[N], idx2[N], m, ss, g[N], h[N], a[N];
bool tag[N];
uint qpow(uint a, uint b) {
uint ans = 1;
for(; b; b >>= 1, a *= a) if(b & 1) ans *= a;
return ans;
}
void sieve(uint n) {
phi[1] = 1;
for(uint i = 2; i <= n; i ++) {
if(!tag[i]) {
p[++ c] = i; phi[i] = i - 1;
pk[c] = qpow(i, k); spk[c] = spk[c - 1] + pk[c];
}
for(uint j = 1; j <= c && i * p[j] <= n; j ++) {
uint u = i * p[j]; tag[u] = 1;
if(i % p[j] == 0) { phi[u] = phi[i] * p[j]; break ; }
phi[u] = phi[i] * (p[j] - 1);
}
phi[i] += phi[i - 1];
}
}
bool vis[M];
uint ans[M];
uint Sum(uint u) {
if(u < N) return phi[u];
uint x = n / u;
if(vis[x]) return ans[x];
vis[x] = 1;
uint &res = ans[x]; res = u * (u + 1) / 2;
for(uint i = 2, j; i <= u; i = j + 1) {
j = u / (u / i);
res -= (j - i + 1) * Sum(u / i);
}
return res;
}
uint S[K][K];
void init(uint n) {
S[0][0] = 1;
for(uint i = 1; i <= n; i ++)
for(uint j = 1; j <= i; j ++)
S[i][j] = S[i - 1][j - 1] + S[i - 1][j] * j;
}
uint calc(uint n) { //\sum i^k
uint ans = 0;
for(uint i = 1; i <= k && i <= n; i ++) {
uint res = 1;
for(uint x = n + 1; x > n - i; x --) {
res *= ( x % (i + 1) ) ? x : x / (i + 1);
}
ans += res * S[k][i];
}
return ans;
}
uint solve(uint x) {
int z = x <= ss ? idx1[x] : idx2[n / x];
return h[z] + a[z];
}
int main() {
cin >> n >> k;
for(ss = 1; ss * ss <= n; ss ++) ;
sieve(N - 1); init(k);
for(uint l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
pos[++ m] = n / l; a[m] = n / l - 1;
g[m] = calc(n / l) - 1;
if(n / l <= ss) idx1[n / l] = m;
else idx2[r] = m;
}
for(uint i = 1; i <= c; i ++) {
for(uint j = 1; j <= m && (ull) p[i] * p[i] <= pos[j]; j ++) {
uint x = pos[j] / p[i];
uint z = x <= ss ? idx1[x] : idx2[n / x];
a[j] -= a[z] - i + 1;
g[j] -= pk[i] * (g[z] - spk[i - 1]);
h[j] += g[z] - spk[i - 1];
}
}
uint ans = 0, la = 0, cur = 0;
for(uint l = 2, r; l <= n; l = r + 1, la = cur) {
r = n / (n / l); cur = solve(r);
ans += (2 * Sum(n / l) - 1) * (cur - la);
}
cout << ans << '\n';
return 0;
}