CF2053G 赛时代码:
#include<bits/stdc++.h>
using namespace std;
namespace file_read{
namespace input_file_io{
char ib[1<<25],*ip1=ib,*ip2=ib;
inline char gc(){
#ifndef ONLINE_JUDGE
return getchar();
#else
return (ip1==ip2&&(ip2=(ip1=ib)+fread(ib,1,1<<24,stdin)),ip1==ip2?EOF:*ip1++);
#endif
}
inline int read(){
int x=0,f=1;
char c=gc();
while((c<'0'||c>'9')&&c!='-')c=gc();
if(c=='-')f=-1,c=gc();
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^'0');
c=gc();
}
return x*f;
}
};
namespace output_file_io{
char ob[1<<25],*op=ob;
inline void pc(char c){
*op++=c;
}
void write(int x){
if(x<0){
pc('-');
x=-x;
}
if(x==0)pc('0');
static int number_stack[40];
int total_count=0;
while(x)number_stack[++total_count]=x%10,x/=10;
while(total_count){
pc(number_stack[total_count]+'0');
--total_count;
}
}
inline void final_write(){
fwrite(ob,op-ob,1,stdout);
}
};
using namespace input_file_io;
using namespace output_file_io;
};
using namespace file_read;
char s[5000005],t[5000005];
const long long mod=1004535818040822281ll;
long long bs[5000005],ms[5000005],mt[5000005];
struct apple{
long long mm;
int l,r;
apple(long long mm=0,int l=0,int r=0):mm(mm),l(l),r(r){}
};
inline long long geths(int l,int r){
return (ms[r]-(__int128)ms[l-1]*bs[r-l+1])%mod;
}
inline bool sok(int l,int r,int ll,int rr){
return (geths(l,r)-geths(ll,rr))%mod==0;
}
inline long long getht(int l,int r){
return (mt[r]-(__int128)mt[l-1]*bs[r-l+1])%mod;
}
inline bool tok(int l,int r,int ll,int rr){
return (getht(l,r)-getht(ll,rr))%mod==0;
}
apple X,Y;int p,q,ff;
//A=X^p;B=X^qY
void suan(apple a,apple b){
ff=0;
if(b.r-b.l<a.r-a.l)swap(a,b);
int aa=a.r-a.l+1,bb=b.r-b.l+1;
int bl=b.l;
if(!sok(a.l,a.r,bl,bl+aa-1)){
X=a;Y=b;p=1;q=0;
return;
}
int L=2,R=bb/aa;
while(L<=R){
int x=(L+R)>>1;
if(sok(bl,bl+(x-1)*aa-1,bl+aa,bl+x*aa-1))L=x+1;
else R=x-1;
}
L=R;
X=a;p=1;Y=apple(geths(bl+L*aa,b.r),bl+L*aa,b.r);q=L;
if(L==bb/aa){
int g=__gcd(aa,bb);
if(sok(a.l,a.r-g,a.l+g,a.r)&&sok(b.l,b.r-g,b.l+g,b.r)){
ff=g;X=apple(geths(a.l,a.l+g-1),a.l,a.l+g-1);
p=aa/g,q=bb/g;
}
}
}
void exgcd(int a,int b,int &x,int &y){
if(!b){
x=1,y=0;
return;
}
exgcd(b,a%b,x,y);
int z=x;
x=y,y=z-a/b*y;
}
int main(){
int T;scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
scanf("%s%s",s+1,t+1);
bs[0]=1;
for(int i=1;i<=m;++i)
bs[i]=(__int128)bs[i-1]*131313131%mod;
for(int i=1;i<=n;++i){
ms[i]=((__int128)ms[i-1]*131313131+s[i])%mod;
}
for(int i=1;i<=m;++i){
mt[i]=((__int128)mt[i-1]*131313131+t[i])%mod;
}
for(int i=1;i<n;++i){
apple a(geths(1,i),1,i),b(geths(i+1,n),i+1,n);
suan(a,b);
if(ff){
int g=ff;
if(m%g||(mt[m-g]-mt[m]+(__int128)mt[g]*bs[m-g])%mod){
putchar('0');
continue;
}
if((mt[g]-X.mm)%mod){
putchar('0');
continue;
}
int gg=__gcd(p,q),mm=m/g;
if(mm%gg){
putchar('0');
continue;
}
p/=gg;q/=gg;mm/=gg;
int x=0,y=0;
exgcd(p,q,x,y);
x=(1ll*mm*x%q+q)%q;
if(1ll*p*x<=mm)putchar('1');
else putchar('0');
}else{
int t=1,fl=1;
int l=X.r-X.l+1,ll=Y.r-Y.l+1;
while(t<=m){
if(t+l-1>m){
fl=0;break;//wujie
}
int gg=1;
if((getht(t,t+l-1)-X.mm)%mod){
gg=0;
}
if(gg){
int L=2,R=max(1,(m-t+1)/l);
while(L<=R){
int x=(L+R)>>1;
if(tok(t,t+(x-1)*l-1,t+l,t+x*l-1))L=x+1;
else R=x-1;
}
gg=R;
}
t+=gg*l;
if(t>m){
break;
}
if(t+ll-1>m||(getht(t,t+ll-1)-Y.mm)%mod){
fl=0;break;
}
if(gg<q){
fl=0;break;
}
t+=ll;
}
putchar('0'+fl);
}
}
putchar('\n');
}
return 0;
}CF2053I2 订正代码:
#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
namespace file_read{
namespace input_file_io{
char ib[1<<25],*ip1=ib,*ip2=ib;
inline char gc(){
#ifndef ONLINE_JUDGE
return getchar();
#else
return (ip1==ip2&&(ip2=(ip1=ib)+fread(ib,1,1<<24,stdin)),ip1==ip2?EOF:*ip1++);
#endif
}
inline int read(){
int x=0,f=1;
char c=gc();
while((c<'0'||c>'9')&&c!='-')c=gc();
if(c=='-')f=-1,c=gc();
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^'0');
c=gc();
}
return x*f;
}
};
namespace output_file_io{
char ob[1<<25],*op=ob;
inline void pc(char c){
*op++=c;
}
void write(int x){
if(x<0){
pc('-');
x=-x;
}
if(x==0)pc('0');
static int number_stack[40];
int total_count=0;
while(x)number_stack[++total_count]=x%10,x/=10;
while(total_count){
pc(number_stack[total_count]+'0');
--total_count;
}
}
inline void final_write(){
fwrite(ob,op-ob,1,stdout);
}
};
using namespace input_file_io;
using namespace output_file_io;
};
using namespace file_read;
int powdv(int x,int y=mod-2){
int ans=1;
while(y){
if(y&1)ans=1ll*ans*x%mod;
y>>=1,x=1ll*x*x%mod;
}
return ans;
}
int n,a[3000005];
long long s[3000005],L[3000005],R[3000005];
void init(){
for(int i=1;i<=n;++i){
a[i]=read();s[i]=s[i-1]+a[i];
}
L[0]=R[0]=0;
for(int i=0;i<n;++i){
L[i+1]=-s[i];R[i+1]=s[n]-s[i];
}
for(int i=1;i<=n;++i){
R[i]=min(R[i],s[n]-s[i]);
L[i]=max(L[i],-s[i]);
}
L[n+1]=R[n+1]=0;
}
int f[3000005],g[3000005],p[3000005];
int S[3000005];
int ql[3000005],qr[3000005];
int sl[3000005],sr[3000005];
int main(){
int T=read();
while(T--){
n=read();init();
f[0]=0;g[0]=S[0]=1;
int hl=1,tl=0,hr=1,tr=0,w=0;
int he=0,h1=0,h2=0;
int bl=0,br=0;
sl[0]=sr[0]=-1;
for(int i=1;i<=n+2;++i){
//L[i-1] R[i-1];
p[i]=p[i-1];ql[hl-1]=qr[hr-1]=p[i]-1;
he=(he+(R[i-1]-L[i-1]+1)%mod*g[i-1])%mod;
while(hl<=tl&&L[ql[tl]]<=L[i-1]){
he=(he-(L[i-1]-L[ql[tl]])%mod*(S[ql[tl]]-(ql[tl-1]<0?0:S[ql[tl-1]])))%mod;
--tl;
}
ql[++tl]=i-1;
while(hr<=tr&&R[qr[tr]]>=R[i-1]){
he=(he-(R[qr[tr]]-R[i-1])%mod*(S[qr[tr]]-(qr[tr-1]<0?0:S[qr[tr-1]])))%mod;
--tr;
}
qr[++tr]=i-1;
while(1){
while(hl<=tl&&ql[hl]<p[i])++hl;
while(hr<=tr&&qr[hr]<p[i])++hr;
if(L[ql[hl]]<=R[qr[hr]])break;
h1=(h1+(R[qr[hr]]-L[ql[hl]]+1)%mod*g[p[i]])%mod;
++p[i];
}
f[i]=f[p[i]]+1;g[i]=(he-h1)%mod;
if(f[w]<f[i]){
bl=br=0;h2=0;w=i;
sl[0]=sr[0]=w-1;
}else{
h2=(h2+(R[i-1]-L[i-1]+1)%mod*g[i-1])%mod;
while(bl&&L[sl[bl]]<=L[i-1]){
h2=(h2-(L[i-1]-L[sl[bl]])%mod*(S[sl[bl]]-(sl[bl-1]<0?0:S[sl[bl-1]])))%mod;
--bl;
}
while(br&&R[sr[br]]>=R[i-1]){
h2=(h2-(R[sr[br]]-R[i-1])%mod*(S[sr[br]]-(sr[br-1]<0?0:S[sr[br-1]])))%mod;
--br;
}
/*
for(int j=bl-1;j>=1;--j){
if(L[sl[j]]<=L[i-1]){
h2=(h2-(L[i-1]-L[sl[j]])%mod*g[sl[j]])%mod;
}
}
for(int j=br-1;j>=1;--j){
if(R[sr[j]]>=R[i-1]){
h2=(h2-(R[sr[j]]-R[i-1])%mod*g[sr[j]])%mod;
}
}
*/
sl[++bl]=i-1;sr[++br]=i-1;
}
g[i]=(g[i]-h2)%mod;
/*
while(f[w]<f[i]){
while(bl<=tl&&ql[bl]<w)++bl;
while(br<=tr&&qr[br]<w)++br;
h2=(h2+(R[qr[br]]-L[ql[bl]]+1)%mod*g[w])%mod;
++w;
}
long long ll=-4e18,rr=4e18;
for(int j=i;j>w;--j){
ll=max(ll,L[j-1]);rr=min(rr,R[j-1]);
g[i]=(g[i]-(rr-ll+1)%mod*g[j-1])%mod;
}
*/
S[i]=(S[i-1]+g[i])%mod;
}
int ans=(g[n+2]+mod)%mod;
printf("%d\n",ans);
}
return 0;
}