QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#480268 | #8761. 另一个计数问题 | Hiraethsoul | AC ✓ | 1940ms | 62552kb | C++20 | 3.7kb | 2024-07-16 11:59:15 | 2024-07-16 11:59:16 |
Judging History
answer
#include <bits/stdc++.h>
#define int __int128
const int p = 998244353;
const int inv2 = 499122177;
const int inv4 = 748683265;
const int inv6 = 166374059;
struct Min25 // 传入一个n 先求g函数 再求s函数
{
int n, cur;
int cnt = 0;
std::vector<int> id1, id2, w;
std::vector<int> g1, g2; // 两个前缀和
std::vector<int> a, vis, s, t;
int get_id(int val)
{
return (val <= cur ? id1[val] : id2[n / val]);
} // 注意w要开2*cur的空间 因为整除分块最多有2*sqrt(n)种取值
Min25(int n) : n(n), cur(sqrt(n)), w(2 * cur + 10), g1(2 * cur + 10), g2(2 * cur + 10), a(cur + 10), vis(cur + 10, 0), s(cur + 10), t(cur + 10), id1(cur + 10), id2(cur + 10)
{
s[0] = t[0] = 0;
vis[0] = vis[1] = true;
for (int i = 2; i <= cur; ++i)
{
if (!vis[i])
{
a[++cnt] = i;
s[cnt] += (s[cnt - 1] + i * i % p) % p;
t[cnt] += (t[cnt - 1] + i) % p;
}
for (int j = 1; j <= cnt and i * a[j] <= cur; ++j)
{
vis[i * a[j]] = true;
if (i % a[j] == 0)
{
break;
}
}
} // 欧拉筛预处理前缀和
int tot = 0;
for (int i = 1, j; i <= n; i = j + 1) // 整除分块预处理id和g(0,m)
{
j = n / (n / i);
int val = n / i;
w[++tot] = val;
if (val <= cur)
{
id1[val] = tot;
}
else
{
id2[n / val] = tot;
}
val %= p;
// 求g(0,m)实际上是前缀和但是要去掉1
g1[tot] = (val * (val + 1) / 2 % p - 1 + p) % p;
g2[tot] = ((val * (val + 1) % p * (2ll * val + 1) % p * inv6 % p) % p - 1 + p) % p;
}
for (int j = 1; j <= cnt; ++j) // dp出g[j][id]
{
for (int i = 1; i <= tot and a[j] * a[j] <= w[i]; ++i)
{
int k = get_id(w[i] / a[j]);
g1[i] = (g1[i] - a[j] * (g1[k] - t[j - 1] + p) % p + p) % p;
g2[i] = (g2[i] - a[j] * a[j] % p * (g2[k] - s[j - 1] + p) % p + p) % p;
}
}
}
int getprimepres1(int n) // 质数处的函数值的前缀和
{
int k = get_id(n);
return g1[k];
}
int getprimepres2(int n) // 质数处的函数值的前缀和
{
int k = get_id(n);
return g2[k];
}
int getS(int x, int y)
{
if (x < a[y] or x <= 1)
{
return 0;
}
int k = get_id(x);
int res = ((g2[k] - g1[k] + p) % p - (s[y - 1] - t[y - 1] + p) % p + p) % p;
for (int i = y; i <= cnt and a[i] * a[i] <= x; ++i)
{
int t1 = a[i], t2 = a[i] * a[i];
for (int j = 1; t2 <= x; ++j, t1 = t2, t2 = t2 * a[i])
{
int tt1 = t1 % p, tt2 = t2 % p;
res = (res + getS(x / t1, i + 1) * tt1 % p * (tt1 - 1) % p + tt2 * (tt2 - 1) % p) % p;
}
}
return res;
}
int getpre(int n) // 前缀和
{
return (getS(n, 1) + 1) % p;
}
};
signed main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0);
long long n;
std::cin >> n;
int cur = n % p;
long long ans = (((cur * cur % p * (cur + 1) % p * (cur - 2 + p) % p) * inv2 % p - ((cur * cur % p * (cur - 1 + p) % p * (cur - 1 + p) % p) * inv4 % p - 1 + p) % p) + cur * (cur + 1) % p * (cur - 2 + p) % p * inv2 % p - ((cur * (cur - 1 + p) % p * (2 * cur % p - 1 + p) % p) * inv6 % p - 1 + p) % p + 4 * p) % p;
ans = ans * inv2 % p;
cur = n / 2;
Min25 calc(n);
//Min25 calc1(cur);
int sum1 = calc.getprimepres1(n) - calc.getprimepres1(cur); // 质数和
int sum2 = calc.getprimepres2(n) - calc.getprimepres2(cur); // 质数平方和
n = n % p;
int res = ((n * (n + 1) * inv2 % p - 1 + p) % p * sum1 % p - sum2 - (sum1 * sum1 % p - sum2 + p) % p * inv2 % p % p + p) % p;
ans = (ans - res + p) % p;
ans = (ans + p) % p;
std::cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
4
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
5
output:
8
result:
ok 1 number(s): "8"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3736kb
input:
6
output:
80
result:
ok 1 number(s): "80"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
7
output:
80
result:
ok 1 number(s): "80"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
8
output:
200
result:
ok 1 number(s): "200"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
9
output:
407
result:
ok 1 number(s): "407"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3736kb
input:
10
output:
937
result:
ok 1 number(s): "937"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
79
output:
3224298
result:
ok 1 number(s): "3224298"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
123
output:
21077222
result:
ok 1 number(s): "21077222"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
158
output:
57411585
result:
ok 1 number(s): "57411585"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
285
output:
605750829
result:
ok 1 number(s): "605750829"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
355
output:
509863120
result:
ok 1 number(s): "509863120"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
484
output:
311440260
result:
ok 1 number(s): "311440260"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
520
output:
102191845
result:
ok 1 number(s): "102191845"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
706
output:
300787918
result:
ok 1 number(s): "300787918"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
747
output:
505062591
result:
ok 1 number(s): "505062591"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
784
output:
181810798
result:
ok 1 number(s): "181810798"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3804kb
input:
76879
output:
716166793
result:
ok 1 number(s): "716166793"
Test #19:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
209295
output:
753032272
result:
ok 1 number(s): "753032272"
Test #20:
score: 0
Accepted
time: 1ms
memory: 3644kb
input:
220895
output:
874612082
result:
ok 1 number(s): "874612082"
Test #21:
score: 0
Accepted
time: 1ms
memory: 3844kb
input:
243390
output:
68635874
result:
ok 1 number(s): "68635874"
Test #22:
score: 0
Accepted
time: 1ms
memory: 3728kb
input:
414767
output:
862578797
result:
ok 1 number(s): "862578797"
Test #23:
score: 0
Accepted
time: 1ms
memory: 3796kb
input:
431662
output:
231728766
result:
ok 1 number(s): "231728766"
Test #24:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
521130
output:
106207351
result:
ok 1 number(s): "106207351"
Test #25:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
668419
output:
580625063
result:
ok 1 number(s): "580625063"
Test #26:
score: 0
Accepted
time: 1ms
memory: 3884kb
input:
700378
output:
790849562
result:
ok 1 number(s): "790849562"
Test #27:
score: 0
Accepted
time: 1ms
memory: 3716kb
input:
965876
output:
856082142
result:
ok 1 number(s): "856082142"
Test #28:
score: 0
Accepted
time: 77ms
memory: 9148kb
input:
998244350
output:
539142456
result:
ok 1 number(s): "539142456"
Test #29:
score: 0
Accepted
time: 72ms
memory: 9236kb
input:
998244351
output:
730264865
result:
ok 1 number(s): "730264865"
Test #30:
score: 0
Accepted
time: 74ms
memory: 9228kb
input:
998244352
output:
326703895
result:
ok 1 number(s): "326703895"
Test #31:
score: 0
Accepted
time: 73ms
memory: 9240kb
input:
998244353
output:
326703895
result:
ok 1 number(s): "326703895"
Test #32:
score: 0
Accepted
time: 69ms
memory: 9100kb
input:
998244354
output:
730264864
result:
ok 1 number(s): "730264864"
Test #33:
score: 0
Accepted
time: 77ms
memory: 9252kb
input:
998244355
output:
539142451
result:
ok 1 number(s): "539142451"
Test #34:
score: 0
Accepted
time: 77ms
memory: 9240kb
input:
998244356
output:
751581014
result:
ok 1 number(s): "751581014"
Test #35:
score: 0
Accepted
time: 129ms
memory: 11736kb
input:
2165916141
output:
216013547
result:
ok 1 number(s): "216013547"
Test #36:
score: 0
Accepted
time: 181ms
memory: 14376kb
input:
3550627266
output:
318019384
result:
ok 1 number(s): "318019384"
Test #37:
score: 0
Accepted
time: 418ms
memory: 23248kb
input:
11640239920
output:
137498099
result:
ok 1 number(s): "137498099"
Test #38:
score: 0
Accepted
time: 540ms
memory: 26852kb
input:
16191777349
output:
991399721
result:
ok 1 number(s): "991399721"
Test #39:
score: 0
Accepted
time: 855ms
memory: 36412kb
input:
31326230483
output:
99981147
result:
ok 1 number(s): "99981147"
Test #40:
score: 0
Accepted
time: 894ms
memory: 37224kb
input:
32810385543
output:
284259680
result:
ok 1 number(s): "284259680"
Test #41:
score: 0
Accepted
time: 978ms
memory: 39444kb
input:
37368395332
output:
511468046
result:
ok 1 number(s): "511468046"
Test #42:
score: 0
Accepted
time: 1013ms
memory: 40848kb
input:
40002331093
output:
282851705
result:
ok 1 number(s): "282851705"
Test #43:
score: 0
Accepted
time: 1679ms
memory: 57156kb
input:
82884464396
output:
767050832
result:
ok 1 number(s): "767050832"
Test #44:
score: 0
Accepted
time: 1881ms
memory: 61396kb
input:
96506992785
output:
31413975
result:
ok 1 number(s): "31413975"
Test #45:
score: 0
Accepted
time: 1930ms
memory: 62428kb
input:
99999999995
output:
456189842
result:
ok 1 number(s): "456189842"
Test #46:
score: 0
Accepted
time: 1939ms
memory: 62384kb
input:
99999999996
output:
516138273
result:
ok 1 number(s): "516138273"
Test #47:
score: 0
Accepted
time: 1931ms
memory: 62552kb
input:
99999999997
output:
136420410
result:
ok 1 number(s): "136420410"
Test #48:
score: 0
Accepted
time: 1939ms
memory: 62384kb
input:
99999999998
output:
841974696
result:
ok 1 number(s): "841974696"
Test #49:
score: 0
Accepted
time: 1940ms
memory: 62364kb
input:
99999999999
output:
164762165
result:
ok 1 number(s): "164762165"
Test #50:
score: 0
Accepted
time: 1936ms
memory: 62448kb
input:
100000000000
output:
627965619
result:
ok 1 number(s): "627965619"