QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#683881 | #8761. 另一个计数问题 | Rykony | AC ✓ | 1322ms | 53064kb | C++20 | 3.1kb | 2024-10-28 01:04:08 | 2024-10-28 01:04:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL=long long;
const LL maxn=1e6+10,mod=998244353;
LL n,sq;
LL v[maxn],prime[maxn],sp1[maxn],sp2[maxn],sp3[maxn];
LL g1[maxn],g2[maxn],g3[maxn],w[maxn],id1[maxn],id2[maxn],tot;
LL qpow(LL a,LL z)
{
LL ans=1;
while (z){
if (z&1) ans=ans*a%mod;
a=a*a%mod;
z>>=1;
}
return ans;
}
LL inv2=qpow(2,mod-2),inv6=qpow(6,mod-2);
void init()
{
prime[0]=0;
for (int i=2;i<=sq;++i){
if (!v[i]){
prime[++prime[0]]=i;
int cnt=prime[0];
sp1[cnt]=(sp1[cnt-1]+i)%mod;
sp2[cnt]=(sp2[cnt-1]+1)%mod;
sp3[cnt]=(sp3[cnt-1]+1ll*i*i%mod)%mod;
}
for (int j=1;prime[j]*i<maxn;++j){
v[prime[j]*i]=prime[j];
if (i%prime[j]==0) break;
}
}
}
LL g(LL k,LL x)
{
return k/(k/x);
}
LL S(LL i,LL j)
{
if (prime[j]>=i) return 0;
LL p=i<=sq?id1[i]:id2[n/i];
LL ans=((g1[p]+mod)%mod-(sp1[j]+mod)%mod+mod)%mod;
for (int k=j+1;prime[k]*prime[k]<=i and k<=prime[0];k++){
LL pe=prime[k];
for (int e=1;pe<=i;e++,pe=pe*prime[k]){
ans=(ans+(prime[k]^e)%mod*(S(i/pe,k)+(e>1))%mod)%mod;
}
}
return ans;
}
int main()
{
cin>>n;
LL allsum=n%mod*((n+1)%mod)%mod*inv2%mod;
allsum=(allsum-1+mod)%mod;
LL allpls=n%mod*((n+1)%mod)%mod*((2*n+1)%mod)%mod*inv6%mod;
allpls=(allpls-1+mod)%mod;
sq=sqrt(n);
init();
// cout<<"sum(i) = "<<allsum<<'\n';
// cout<<"sum(i^2) = "<<allpls<<'\n';
for (LL l=1,r;l<=n;l=r+1){
r=min(n,g(n,l));
w[++tot]=n/l%mod;
g1[tot]=(w[tot]*(w[tot]+1)%mod*inv2%mod-1+mod)%mod;
g2[tot]=(w[tot]-1+mod)%mod;
g3[tot]=(w[tot]*(w[tot]+1)%mod*(2*w[tot]+1)%mod*inv6%mod-1+mod)%mod;
w[tot]=n/l;
if (w[tot]<=sq) id1[w[tot]]=tot;
else id2[n/w[tot]]=tot;
}
for (int j=1;j<=prime[0];j++){
for (int i=1;i<=tot and prime[j]*prime[j]<=w[i];i++){
LL tmp=w[i]/prime[j];
LL p=tmp<=sq?id1[tmp]:id2[n/tmp];
g1[i]=(g1[i]-prime[j]*(g1[p]-sp1[j-1]+mod)%mod+mod)%mod;
g2[i]=(g2[i]-1%mod*(g2[p]-sp2[j-1]+mod)%mod+mod)%mod;
g3[i]=(g3[i]-prime[j]*prime[j]%mod*(g3[p]-sp3[j-1]+mod)%mod+mod)%mod;
}
}
LL f2=g1[1],f2p=g3[1];
// cout<<"sum(p) = "<<f2<<'\n';
// cout<<"sum(p^2) = "<<f2p<<'\n';
n=n/2;
sq=sqrt(n);
init();
tot=0;
for (LL l=1,r;l<=n;l=r+1){
r=min(n,g(n,l));
w[++tot]=n/l%mod;
g1[tot]=(w[tot]*(w[tot]+1)%mod*inv2%mod-1+mod)%mod;
g2[tot]=(w[tot]-1+mod)%mod;
g3[tot]=(w[tot]*(w[tot]+1)%mod*(2*w[tot]+1)%mod*inv6%mod-1+mod)%mod;
w[tot]=n/l;
if (w[tot]<=sq) id1[w[tot]]=tot;
else id2[n/w[tot]]=tot;
}
for (int j=1;j<=prime[0];j++){
for (int i=1;i<=tot and prime[j]*prime[j]<=w[i];i++){
LL tmp=w[i]/prime[j];
LL p=tmp<=sq?id1[tmp]:id2[n/tmp];
g1[i]=(g1[i]-prime[j]*(g1[p]-sp1[j-1]+mod)%mod+mod)%mod;
g2[i]=(g2[i]-1%mod*(g2[p]-sp2[j-1]+mod)%mod+mod)%mod;
g3[i]=(g3[i]-prime[j]*prime[j]%mod*(g3[p]-sp3[j-1]+mod)%mod+mod)%mod;
}
}
// cout<<"sum(lip) = "<<g1[1]<<'\n';
// cout<<"sum(lip^2) = "<<g3[1]<<'\n';
f2=(f2-g1[1]+mod)%mod;
f2p=(f2p-g3[1]+mod)%mod;
LL ans=(allsum-f2+mod)%mod*(allsum-f2+mod)%mod;
ans=(ans-allpls+mod)%mod;
ans=(ans+f2p)%mod;
cout<<ans*inv2%mod<<'\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15828kb
input:
4
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 0ms
memory: 16016kb
input:
5
output:
8
result:
ok 1 number(s): "8"
Test #3:
score: 0
Accepted
time: 0ms
memory: 15904kb
input:
6
output:
80
result:
ok 1 number(s): "80"
Test #4:
score: 0
Accepted
time: 2ms
memory: 15984kb
input:
7
output:
80
result:
ok 1 number(s): "80"
Test #5:
score: 0
Accepted
time: 0ms
memory: 15832kb
input:
8
output:
200
result:
ok 1 number(s): "200"
Test #6:
score: 0
Accepted
time: 0ms
memory: 15892kb
input:
9
output:
407
result:
ok 1 number(s): "407"
Test #7:
score: 0
Accepted
time: 2ms
memory: 16016kb
input:
10
output:
937
result:
ok 1 number(s): "937"
Test #8:
score: 0
Accepted
time: 0ms
memory: 15832kb
input:
79
output:
3224298
result:
ok 1 number(s): "3224298"
Test #9:
score: 0
Accepted
time: 0ms
memory: 15960kb
input:
123
output:
21077222
result:
ok 1 number(s): "21077222"
Test #10:
score: 0
Accepted
time: 2ms
memory: 15904kb
input:
158
output:
57411585
result:
ok 1 number(s): "57411585"
Test #11:
score: 0
Accepted
time: 0ms
memory: 15904kb
input:
285
output:
605750829
result:
ok 1 number(s): "605750829"
Test #12:
score: 0
Accepted
time: 0ms
memory: 15912kb
input:
355
output:
509863120
result:
ok 1 number(s): "509863120"
Test #13:
score: 0
Accepted
time: 2ms
memory: 15916kb
input:
484
output:
311440260
result:
ok 1 number(s): "311440260"
Test #14:
score: 0
Accepted
time: 2ms
memory: 15976kb
input:
520
output:
102191845
result:
ok 1 number(s): "102191845"
Test #15:
score: 0
Accepted
time: 0ms
memory: 15924kb
input:
706
output:
300787918
result:
ok 1 number(s): "300787918"
Test #16:
score: 0
Accepted
time: 1ms
memory: 15832kb
input:
747
output:
505062591
result:
ok 1 number(s): "505062591"
Test #17:
score: 0
Accepted
time: 1ms
memory: 15844kb
input:
784
output:
181810798
result:
ok 1 number(s): "181810798"
Test #18:
score: 0
Accepted
time: 2ms
memory: 17988kb
input:
76879
output:
716166793
result:
ok 1 number(s): "716166793"
Test #19:
score: 0
Accepted
time: 0ms
memory: 19540kb
input:
209295
output:
753032272
result:
ok 1 number(s): "753032272"
Test #20:
score: 0
Accepted
time: 0ms
memory: 18856kb
input:
220895
output:
874612082
result:
ok 1 number(s): "874612082"
Test #21:
score: 0
Accepted
time: 0ms
memory: 19512kb
input:
243390
output:
68635874
result:
ok 1 number(s): "68635874"
Test #22:
score: 0
Accepted
time: 0ms
memory: 20372kb
input:
414767
output:
862578797
result:
ok 1 number(s): "862578797"
Test #23:
score: 0
Accepted
time: 3ms
memory: 19488kb
input:
431662
output:
231728766
result:
ok 1 number(s): "231728766"
Test #24:
score: 0
Accepted
time: 0ms
memory: 20072kb
input:
521130
output:
106207351
result:
ok 1 number(s): "106207351"
Test #25:
score: 0
Accepted
time: 2ms
memory: 21620kb
input:
668419
output:
580625063
result:
ok 1 number(s): "580625063"
Test #26:
score: 0
Accepted
time: 0ms
memory: 23116kb
input:
700378
output:
790849562
result:
ok 1 number(s): "790849562"
Test #27:
score: 0
Accepted
time: 0ms
memory: 23556kb
input:
965876
output:
856082142
result:
ok 1 number(s): "856082142"
Test #28:
score: 0
Accepted
time: 48ms
memory: 28136kb
input:
998244350
output:
539142456
result:
ok 1 number(s): "539142456"
Test #29:
score: 0
Accepted
time: 51ms
memory: 26188kb
input:
998244351
output:
730264865
result:
ok 1 number(s): "730264865"
Test #30:
score: 0
Accepted
time: 52ms
memory: 25956kb
input:
998244352
output:
326703895
result:
ok 1 number(s): "326703895"
Test #31:
score: 0
Accepted
time: 49ms
memory: 25980kb
input:
998244353
output:
326703895
result:
ok 1 number(s): "326703895"
Test #32:
score: 0
Accepted
time: 56ms
memory: 28120kb
input:
998244354
output:
730264864
result:
ok 1 number(s): "730264864"
Test #33:
score: 0
Accepted
time: 56ms
memory: 25764kb
input:
998244355
output:
539142451
result:
ok 1 number(s): "539142451"
Test #34:
score: 0
Accepted
time: 49ms
memory: 28132kb
input:
998244356
output:
751581014
result:
ok 1 number(s): "751581014"
Test #35:
score: 0
Accepted
time: 86ms
memory: 28100kb
input:
2165916141
output:
216013547
result:
ok 1 number(s): "216013547"
Test #36:
score: 0
Accepted
time: 125ms
memory: 28520kb
input:
3550627266
output:
318019384
result:
ok 1 number(s): "318019384"
Test #37:
score: 0
Accepted
time: 283ms
memory: 30948kb
input:
11640239920
output:
137498099
result:
ok 1 number(s): "137498099"
Test #38:
score: 0
Accepted
time: 366ms
memory: 36532kb
input:
16191777349
output:
991399721
result:
ok 1 number(s): "991399721"
Test #39:
score: 0
Accepted
time: 598ms
memory: 39656kb
input:
31326230483
output:
99981147
result:
ok 1 number(s): "99981147"
Test #40:
score: 0
Accepted
time: 597ms
memory: 43440kb
input:
32810385543
output:
284259680
result:
ok 1 number(s): "284259680"
Test #41:
score: 0
Accepted
time: 655ms
memory: 38808kb
input:
37368395332
output:
511468046
result:
ok 1 number(s): "511468046"
Test #42:
score: 0
Accepted
time: 685ms
memory: 43796kb
input:
40002331093
output:
282851705
result:
ok 1 number(s): "282851705"
Test #43:
score: 0
Accepted
time: 1151ms
memory: 47452kb
input:
82884464396
output:
767050832
result:
ok 1 number(s): "767050832"
Test #44:
score: 0
Accepted
time: 1286ms
memory: 50636kb
input:
96506992785
output:
31413975
result:
ok 1 number(s): "31413975"
Test #45:
score: 0
Accepted
time: 1319ms
memory: 52360kb
input:
99999999995
output:
456189842
result:
ok 1 number(s): "456189842"
Test #46:
score: 0
Accepted
time: 1304ms
memory: 52076kb
input:
99999999996
output:
516138273
result:
ok 1 number(s): "516138273"
Test #47:
score: 0
Accepted
time: 1304ms
memory: 51916kb
input:
99999999997
output:
136420410
result:
ok 1 number(s): "136420410"
Test #48:
score: 0
Accepted
time: 1316ms
memory: 52276kb
input:
99999999998
output:
841974696
result:
ok 1 number(s): "841974696"
Test #49:
score: 0
Accepted
time: 1322ms
memory: 49704kb
input:
99999999999
output:
164762165
result:
ok 1 number(s): "164762165"
Test #50:
score: 0
Accepted
time: 1317ms
memory: 53064kb
input:
100000000000
output:
627965619
result:
ok 1 number(s): "627965619"