%%% Claris :http://www.cnblogs.com/clrs97/p/5131958.html
每个ai是互不相同的
所以在以ai为下标的一个表删一些区间,剩余的就是解
#include<cstdio> #include<cstdlib> #include<algorithm> using namespace std; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++; } inline void read(int &x) { char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } inline void read(char *s) { char c=nc(); int len=0; for (;!(c=='0' || c=='1');c=nc()); for (;c=='0' || c=='1';s[++len]=c,c=nc()); s[++len]=0; } struct edge{ int x,y; edge(int x=0,int y=0):x(x),y(y) {} bool operator < (const edge &B) const{ return x<B.x; } }E[4000005]; int cnt; int n,m,A,B,P; char w[1000005]; inline void add(int a,int b,int c,int d){ if(a) E[++cnt]=edge(0,a); if(b<c) E[++cnt]=edge(b,c); if(d<n) E[++cnt]=edge(d,n); } int main() { freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(A); read(B); read(P); read(m); read(w); for (int i=1;i<=m;i++,(B+=A)%=n) if (w[i]=='0') add(0,max(P-B,0),n-B,min(P-B+n,n)); else add(max(P-B,0),n-B,min(P-B+n,n),n); B=n-A; for(int i=n-1;i>n-m;B=(B-A+n)%n,i--) E[++cnt]=edge(B,B+1); E[++cnt]=edge(n,n+1); sort(E+1,E+cnt+1); int ans=0,r=0; for (int i=1;i<=cnt;i++) { if(E[i].x>r) ans+=E[i].x-r; if(E[i].y>r) r=E[i].y; } printf("%d\n",ans); return 0; }