ICPC南京网络赛E.Ksum

题意:

化简开始

莫比乌斯反演

令$T=xd$

前面分块即可
然后需要求后面那一块,就设

杜教筛

又可以在一顿推导后

就可以线性筛递推

代码
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 5e6 + 10;
const int mod = 1e9 + 7;
typedef long long ll;

bool notprime[N];
int prime[N], cnt;
int sumf[N];
int f[N];
unordered_map<int, int> v;
int addmod(ll x, ll y)
{
if (y < 0)
y += mod;
if (x + y >= mod)
x -= mod;
return x + y;
}
int qpow(int a, int x, int mo)
{
int res = 1;
while (x)
{
if (x & 1)
res = 1ll * res * a % mo;
a = 1ll * a * a % mo;
x >>= 1;
}
return res;
}
void mublus()
{
f[1] = 1;
for (int i = 2; i < N; i++)
{
if (!notprime[i])
{
f[i] = (1ll * i * i - 1) % mod;
prime[++cnt] = i;
}
for (int j = 1; j <= cnt && prime[j] * i < N; j++)
{
notprime[prime[j] * i] = true;
if (i % prime[j])
{
f[i * prime[j]] = 1ll * f[i] * (1ll * prime[j] * prime[j] % mod - 1) % mod;
}
else
{
f[i * prime[j]] = 1ll * f[i] * prime[j] % mod * prime[j] % mod;
break;
}
}
}
for (int i = 1; i < N; i++)
sumf[i] = addmod(sumf[i - 1], f[i]);
}
int k, kk, inv6;
int getsum(int x)
{
if (x == 1)
return kk - 1;
return 1ll * x * addmod(qpow(x, k, mod), -x) % mod * qpow(x - 1, mod - 2, mod) % mod;
}
int getsumx2(int x)
{
return 1ll * x * (x + 1) % mod * (2 * x + 1) % mod * inv6 % mod;
}
int getf(int x)
{
if (x < N)
return sumf[x];
if (v[x])
return v[x];
int ans = getsumx2(x);
for (int i = 2, j; i <= x; i = j + 1)
{
j = x / (x / i);
ans = addmod(ans, -(1ll * (j - i + 1) * getf(x / i) % mod));
}
return v[x] = ans;
}

int n, T;
char str[N];
int main()
{
mublus();
inv6 = qpow(6, mod - 2, mod);
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
scanf("%s", str);

int ans = 0;
int len = strlen(str);
int p = mod - 1;
k = 0;
kk = 0;
for (int i = 0; i < len; i++)
{
k = addmod(1ll * k * 10 % p, str[i] - '0');
kk = addmod(1ll * kk * 10 % mod, str[i] - '0');
}
//cout << getf(3) << endl;
for (int i = 1, j; i <= n; i = j + 1)
{
j = n / (n / i);
//cout << n / i << " " << getsum(n / i) << endl;
ans = addmod(ans, 1ll * addmod(getf(j), -getf(i - 1)) * getsum(n / i) % mod);
}
cout << ans << endl;
}
}