2020 CCPC网络选拔赛1002
原题链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=909

题目核心是给定范围(1 ~ 1e9)内的素数求和.

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
#include <bits/stdc++.h>

using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))

typedef long long LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int N = 1e6 + 5;
const int M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ll LL_MAX = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

inline ll read() {
ll res = 0;
bool f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = 1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
res = (res << 3) + (res << 1) + ch - '0';
ch = getchar();
}
return f ? (~res + 1) : res;
}

int pri[N], cntt1[N], cntt2[N], flag[N], ncnt, m;

LL g[N], sum[N], a[N], T, n;

void iiint() {
ms(pri, 0);
ms(cntt1, 0);
ms(cntt2, 0);
ms(flag, 0);
ncnt = 0;
ms(g, 0);
ms(sum, 0);
ms(a, 0);
T = 0;
n = 0;
m = 0;
}

inline int ID(LL x) {
if (x <= T) return cntt1[x];
return cntt2[n / x];
}

inline LL calc(LL x) {
return x * (x + 1) / 2 - 1;
}

inline LL f(LL x) {
return x;
}

inline void init() {
T = sqrt(n + 0.5);
for (int i = 2; i <= T; i++) {
if (!flag[i]) pri[++ncnt] = i, sum[ncnt] = sum[ncnt - 1] + i;
for (int j = 1; j <= ncnt && i * pri[j] <= T; j++) {
flag[i * pri[j]] = 1;
if (i % pri[j] == 0) break;
}
}
for (LL l = 1; l <= n; l = n / (n / l) + 1) {
a[++m] = n / l;
if (a[m] <= T) cntt1[a[m]] = m; else cntt2[n / a[m]] = m;
g[m] = calc(a[m]);
}
for (int i = 1; i <= ncnt; i++)
for (int j = 1; j <= m && (LL) pri[i] * pri[i] <= a[j]; j++)
g[j] = g[j] - (LL) pri[i] * (g[ID(a[j] / pri[i])] - sum[i - 1]);
}

inline LL solve(LL x) {
if (x <= 1) return x;
return n = x, init(), g[ID(n)];
}


int main() {
int t = read();
while (t--) {
iiint();
n = read();
ll k = read();
ll ans;
if (n & 1) {
ans = (n - 1) / 2 % k;
ans = (ans * ((n + 4) % k)) % k;
} else {
ans = (n + 4) / 2 % k;
ans = (ans * ((n - 1) % k)) % k;
}
// cout << ans << "\n";
ll tmp = solve(n + 1) - 2;
ans = (ans + (tmp) % k) % k;
printf("%lld\n", ans);

}
return 0;