QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#602137 | #7876. Cyclic Substrings | lzx2017 | AC ✓ | 1046ms | 974724kb | C++20 | 2.9kb | 2024-09-30 20:05:10 | 2024-09-30 20:05:12 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=3e6+10;
const int mod=998244353;
int i,j,n,m,k,l,a[N*4],tree[N*4][11],root,num,r,d[N*4],wz,f[N*4][24],pos[N*4],g[N*2],qz[30],x,y,lg[N*2+1],root1,dep[N*2];
long long ans;
string s;
void update(int x)
{
for(int i=1;i<=23;++i)
f[x][i]=f[f[x][i-1]][i-1];
}
int up(int x,int y)
{
if(!y) return x;
for(int i=lg[y];i>=0;i--) if(qz[i]<=y) x=f[x][i],y-=qz[i];
return x;
}
int main()
{
//freopen("1.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
for (int i = 1; i <= N*2; i++)lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
cin>>n>>s;
for(i=1;i<=n;++i)
{
a[++a[0]]=s[i-1]-'0';
}
for(i=1;i<=n;++i)
{
a[++a[0]]=s[i-1]-'0';
}
qz[0]=1;
for(i=1;i<=23;++i) qz[i]=qz[i-1]*2;
root=0;num=0;
for(i=1,l=1,r=0;i<=n*2;++i)
{
wz=root;
if(!tree[wz][a[i]]){
tree[wz][a[i]]=++num;
f[num][0]=wz;
update(num);
}
wz=tree[root][a[i]];
if(i>r)
{
k=1;
g[wz]++;
g[f[wz][0]]--;
}
else
{
if(d[l+r-i]<=r-i+1)
{
k=d[l+r-i];
x=pos[l+r-i];
g[x]++;
g[f[wz][0]]--;
wz=x;
}
else
{
k=r-i+1;
y=d[l+r-i]-(r-i+1);
x=pos[l+r-i];
x=up(x,y);
g[x]++;
g[f[wz][0]]--;
wz=x;
}
}
while(1<=i-k&&i+k<=2*n&&a[i-k]==a[i+k]){
if(!tree[wz][a[i+k]])
{
tree[wz][a[i+k]]=++num;
f[num][0]=wz;
update(num);
}
g[wz]--;
g[tree[wz][a[i+k]]]++;
wz=tree[wz][a[i+k]];
k++;
}
pos[i]=wz;
d[i]=k--;
if(i+k>r){
l=i-k;
r=i+k;
}
if(i>n)
{
int len=0;
if(i-d[i]+1<=n) len=i-n;
else len=d[i];
x=pos[i]; y=d[i]-len;
x=up(x,y);
g[x]--;
}
}
root1=++num;
for(i=1,l=1,r=0;i<=n*2;++i)
{
wz=root1;
if(i>r) k=0;
else
{
if(d[l+r-i+1]<=r-i+1)
{
k=d[l+r-i+1];
x=pos[l+r-i+1];
g[x]++;
g[root1]--;
wz=x;
}
else
{
k=r-i+1;
y=d[l+r-i+1]-(r-i+1);
x=pos[l+r-i+1];
x=up(x,y);
g[x]++;
g[root1]--;
wz=x;
}
}
while(1<=i-k-1&&i+k<=2*n&&a[i-k-1]==a[i+k]){
if(!tree[wz][a[i+k]])
{
tree[wz][a[i+k]]=++num;
f[num][0]=wz;
update(num);
}
g[wz]--;
g[tree[wz][a[i+k]]]++;
wz=tree[wz][a[i+k]];
k++;
}
pos[i]=wz;
d[i]=k--;
if(i+k>r){
l=i-k-1;
r=i+k;
}
if(i>n)
{
int len=0;
if(i-d[i]<=n) len=i-n-1;
else len=d[i];
x=pos[i]; y=d[i]-len;
x=up(x,y);
g[x]--;
}
}
for(i=0;i<=num;i++) if(i!=0&&i!=root1) dep[i]=dep[f[i][0]]+1;
for(i=root1-1;i>0;i--)
{
g[f[i][0]]+=g[i];
if(dep[i]*2-1<=n) ans+=1ll*(dep[i]*2-1)*g[i]%mod*g[i]%mod;
ans%=mod;
}
for(i=num;i>root1;i--)
{
g[f[i][0]]+=g[i];
if(dep[i]*2<=n) ans+=1ll*(dep[i]*2)*g[i]%mod*g[i]%mod;
ans%=mod;
}
ans=(ans%mod+mod)%mod;
cout<<ans<<endl;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 10ms
memory: 35456kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 7ms
memory: 36592kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 3ms
memory: 36728kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 10ms
memory: 36876kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 9ms
memory: 35596kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 3ms
memory: 36808kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 11ms
memory: 35564kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 312ms
memory: 348836kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 938ms
memory: 973476kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 378ms
memory: 495664kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 953ms
memory: 970832kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 1046ms
memory: 974724kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 692ms
memory: 868680kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 699ms
memory: 896108kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 424ms
memory: 539628kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 226ms
memory: 234552kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 671ms
memory: 837696kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 620ms
memory: 816444kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed