QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#617595 | #7876. Cyclic Substrings | qvzeyang | AC ✓ | 767ms | 465292kb | C++20 | 3.3kb | 2024-10-06 16:22:58 | 2024-10-06 16:22:58 |
Judging History
answer
#include<bits/stdc++.h>
namespace FRTMLV{
#define ll long long
#define LD long double
#define i7 __int128
#define re return
#define con continue
using namespace std;
template<class T>inline bool ckmin(T &a,T b){re b<a?a=b,1:0;}
template<class T>inline bool ckmax(T &a,T b){re a<b?a=b,1:0;}
const int N=6e6+5;
inline int rd(){
int d=1,k=0;char c=getchar();
while(!(c>='0' and c<='9' or c=='-'))c=getchar();if(c=='-')d=-1,c=getchar();
while(c>='0' and c<='9')k=(k<<3)+(k<<1)+(c^48),c=getchar();
return d*k;
}
char stbyte;
const int mod=998244353;
int n,m,d[N<<1],cnt;
char s[N<<1],ss[N];
//ll mi1[N],mi2[N],hs1[N],hs2[N];
ll mi1[N],hs1[N];
#define ull unsigned ll
ull mi2[N],hs2[N];
//inline ll gths1(int l,int r){re (hs1[r]-hs1[l-1]*mi1[r-l+1]%mod+mod)%mod;}
//inline ll gths2(int l,int r){re (hs2[r]-hs2[l-1]*mi2[r-l+1]%mod+mod)%mod;}
inline ll gths1(int l,int r){ll x=hs1[r]-hs1[l-1]*mi1[r-l+1]%mod+mod;re x>=mod?x-mod:x;}
inline ll gths2(int l,int r){ull x=hs2[r]-hs2[l-1]*mi2[r-l+1];re x;}
int f[N<<2],c[N<<2],dgr[N<<2],len[N<<2];
void manacher(char *s,int n){
int m=0,r=0;
for(int i=1;i<=n;i++){
if(i<r)d[i]=min(r-i,d[(m<<1)-i]);
while(s[i-d[i]-1]==s[i+d[i]+1])++d[i];
if(ckmax(r,d[i]+i))m=i;
}
}
namespace hashtable{
const int Md=1e7+7;
int head[10000010],nex[10000010];
ll val[10000010];
int getv(ull x){
ull nx=x%Md;
for(int i=head[nx];i;i=nex[i])if(val[i]==x)return i;
return 0;
}
void newnode(ull x){
cnt++;
ull nx=x%Md;
nex[cnt]=head[nx],head[nx]=cnt,val[cnt]=x;
}
}
char edbyte;
signed main(){
// freopen("data.in","r",stdin);
// printf("%d\n",(int)(&edbyte-&stbyte)/1048576);
n=rd();
scanf("%s",ss+1);
for(int i=1;i<=n;i++)ss[i+n]=ss[i];
for(int i=1;i<=(n<<1);i++)s[i<<1]=ss[i],s[i<<1|1]='#';
s[0]='~',s[1]='#';
mi1[0]=mi2[0]=1;
// for(int i=1;i<=(n<<1);i++)mi1[i]=mi1[i-1]*11%mod,mi2[i]=mi2[i-1]*13%mod;
for(int i=1;i<=(n<<1);i++)mi1[i]=mi1[i-1]*11%mod,mi2[i]=mi2[i-1]*13;
// for(int i=1;i<=(n<<1);i++)hs1[i]=(hs1[i-1]*11+ss[i]-'0'+1)%mod,hs2[i]=(hs2[i-1]*13+ss[i]-'0'+1)%mod;
for(int i=1;i<=(n<<1);i++)hs1[i]=(hs1[i-1]*11+ss[i]-'0'+1)%mod,hs2[i]=(hs2[i-1]*13+ss[i]-'0'+1);
m=(n<<1)<<1|1;
// printf("m:%d\n",m);
manacher(s,m);
for(int i=1;i<=m;i++){
int L=d[i]>>1,p=i>>1;
int l,r,pre=0;
if(i&1)l=p-L+1,r=p+L;
else l=p-L,r=p+L;
if(i>(n<<1)){
if(l>n)con;
int tmp=n+1-l;
int nl=l+tmp,nr=r-tmp;
if(nl<=nr){
// ll hash=gths1(nl,nr)*1000000000+gths2(nl,nr);
ull hash=gths2(nl,nr);
--c[hashtable::getv(hash)];
}
}
int cur=0;
while(l<=r){
cur++;
// ll hash=gths1(l,r)*1000000000+gths2(l,r);
ull hash=gths2(l,r);
int x=hashtable::getv(hash);
if(x){
if(!pre)c[x]++;
else f[pre]=x,++dgr[x];
break;
}
hashtable::newnode(hash);
x=cnt;
len[x]=r-l+1;
if(!pre)c[x]++;
else f[pre]=x,++dgr[x];
pre=x;
l++,r--;
}
// if(!(i&127))cout<<i<<' '<<cur<<' '<<m<<'\n';
}
queue<int>q;
for(int i=1;i<=cnt;i++)
if(!dgr[i])q.push(i);
i7 ans=0;
while(q.size()){
int u=q.front();q.pop();
if(len[u]<=n)ans+=(i7)c[u]*c[u]*len[u];
c[f[u]]+=c[u];
if(f[u]&&!--dgr[f[u]])q.push(f[u]);
}
printf("%lld\n",(ll)(ans%mod));
re 0;
}
/*
3
000
2
00
1
1
*/
}signed main(){re FRTMLV::main();}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22348kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 22364kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 18132kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 22256kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 22260kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 4ms
memory: 22524kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 0ms
memory: 22332kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 253ms
memory: 188988kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 767ms
memory: 465292kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 419ms
memory: 368904kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 614ms
memory: 460304kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 588ms
memory: 459088kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 539ms
memory: 438944kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 586ms
memory: 444444kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 213ms
memory: 376912kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 136ms
memory: 319228kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 303ms
memory: 433220kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 247ms
memory: 429976kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed