1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include<bits/stdc++.h> using namespace std; #define il inline #define rg register #define int long long il int read() { int k=1,re=0;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')k=-1;ch=getchar();} while(ch<='9'&&ch>='0'){re=re*10+ch-48;ch=getchar();} return re*k; } il void write(int x) { if(x<0)return putchar('-'),write(-x),void(); if(x<=9)return putchar(x+48),void(); return write(x/10),write(x%10),void(); } int n,T,W,nxt[500005],f[500005],las; char s[500005]; int b[500005],lb; const int M=998244353; void getb() { int k=0; for(rg int i=2;i<=n;i++) { while(k&&s[i]!=s[k+1])k=nxt[k]; nxt[i]=(s[i]==s[k+1])?++k:0; } while(k) { b[++lb]=n-k; k=nxt[k]; } } int gcd(int u,int v) { if(!v)return u; return gcd(v,u%v); } int st[1000005],ls,p[500005],Q[500005],lq,w[500005],tl,hd,pos[500005]; void tomod(int x) { int g=gcd(x,las); for(rg int i=0;i<las;i++) p[i]=f[i]; for(rg int i=0;i<x;i++) f[i]=0x3f3f3f3f3f3f3f3f; for(rg int i=0;i<las;i++) f[p[i]%x]=min(p[i],f[p[i]%x]); for(rg int i=0;i<g;i++) { ls=0;st[++ls]=i;int now=(i+las)%x; while(st[1]!=now) { st[++ls]=now; now=(now+las)%x; } for(rg int j=1;j<=ls;j++) { st[ls+j]=st[j]; } ls<<=1; for(rg int j=2;j<=ls;j++) f[st[j]]=min(f[st[j-1]]+las,f[st[j]]); } las=x; } void sol(int x,int sz,int d) { int g=gcd(x,d); tomod(x); if(d<0)return; for(rg int i=0;i<g;i++) { ls=lq=0;Q[++lq]=i;int now=(i+d)%x,tt=1; while(Q[1]!=now) { if(f[Q[tt]]>f[now])tt=lq+1; Q[++lq]=now;now=(now+d)%x; } for(rg int j=tt;j<=lq;j++) st[++ls]=Q[j]; for(rg int j=1;j<tt;j++) st[++ls]=Q[j]; hd=tl=0; w[0]=f[st[1]]-d;pos[0]=1; for(rg int j=2;j<=ls;j++) { while(hd<=tl&&pos[hd]<j-sz)hd++; if(hd<=tl)f[st[j]]=min(f[st[j]],w[hd]+j*d+x); while(hd<=tl&&w[tl]>f[st[j]]-j*d)tl--; w[++tl]=f[st[j]]-j*d;pos[tl]=j; } } } signed main() { T=read(); while(T--) { n=read();W=read()-n; scanf("%s",s+1);lb=0; getb();memset(f,0x3f,sizeof(f));memset(nxt,0,sizeof(nxt));las=n; f[0]=0;b[lb+1]=0; for(rg int i=1,j=1;i<=lb;i=j) { while(b[i+1]-b[i]==b[j+1]-b[j])j++; sol(b[i],j-i-1,b[i+1]-b[i]); } int ans=0; for(rg int i=0;i<las;i++) { if(f[i]<=W) ans+=(W-f[i])/las+1; } write(ans);puts(""); } }
|