4542: [Hnoi2016] Large number of time limit:20 Sec Memory limit:128 MB
submit:1637 solved:568
[Submit] [Status] [Discuss] Description
Small B has a large number of S, the length reached N bits; This number can be seen as a string, it may have a leading 0, for example 00009312345
。 Small B also has a prime number p. Now, small B raises M queries, and each query asks how many substrings in a substring of S are multiples of P (0 Also
is a multiple of P). For example, S is 0077, its substring 007 has 6 substrings: 0,0,7,00,07,007; obviously 0077 of the substring 007 has 6 substrings are all vegetarian
Multiples of 7.
Input
The first line is an integer: P. The second line is a string: S. The third line is an integer: M. The next m line, two integers per line fr,to, represents the
Sub-string s[fr...to]. Note: The leftmost number of S has a position ordinal of 1; For example, S is 213567, then s[1] is 2,s[1 ... 3] is 2
13. N,m<=100000,p as prime number
Output
Output m lines, one integer per line, and line I is the answer to the I query.
Sample Input11
121121
3
1 6
1 5
1 4Sample Output5
3
2
The first question is the whole string, and the substrings that satisfy the condition are: 121121,2112,11,121,121. HINT
2016.4.19 New Plus Data group
Source
T got two points what a ghost Qwq
1#include"bits/stdc++.h"2 using namespacestd;3typedefLong LongLL;4 Const intmax=1e6+5;5 LL N,m,p,bas,pos[max];6 LL A[max],b[max],d[max],an[max],ans;7 LL Ab[max],cd[max];8 CharS[max];9 structnode{Ten intID; One intL,r; A BOOL operator< (ConstNode &tt)Const { - if(pos[l]!=POS[TT.L]) - returnpos[l]<POS[TT.L]; the returnr<TT.R; - } - }que[max]; - inline LL Read () { +LL an=0, x=1;CharC=GetChar (); - while(c<'0'|| C>'9') {if(c=='-') x=-1; c=GetChar ();} + while(c>='0'&& c<='9') {an=an*Ten+c-'0'; c=GetChar ();} A returnan*x; at } - intMain () { -Freopen ("huge.in","R", stdin); Freopen ("Huge.out","W", stdout); -LL i,j,cnt=1, x, y; -scanf"%lld\n", &p); scanf ("%s", s+1); N=strlen (s+1); bas= (LL) sqrt (n1.0); - if(p==2|| p==5){ in for(i=1; i<=n;i++){ - if((s[i]-'0')%p==0) toab[i]=ab[i-1]+1, cd[i]=cd[i-1]+i; + Elseab[i]=ab[i-1],cd[i]=cd[i-1]; - } them=read (); * for(i=1; i<=m;i++){ $X=read (), y=read ();Panax Notoginsengprintf"%lld\n", cd[y]-cd[x-1]-(ab[y]-ab[x-1]) * (X-1)); - } the return 0; + } A for(i=n;i>=1; i--){ theCnt= (cnt*Ten)%p; +B[i]=a[i]= ((s[i]-'0') *cnt+a[i+1])%p; - } $Sort (b +1, b+n+1); $ for(i=1; i<=n+1; i++) A[i]=lower_bound (b +1, b+n+1, A[i])-b; -m=read (); - for(i=1; i<=m;i++){ theQue[i].id=i; -Que[i].l=read (), Que[i].r=read (); que[i].r++;Wuyi } theSort (que+1, que+m+1); - intL=1, r=0; Wu for(i=1; i<=m;i++){ - while(R<QUE[I].R) ans+=ab[a[++r]]++; About while(L>QUE[I].L) ans+=ab[a[--l]]++; $ while(L<QUE[I].L) ans-=--ab[a[l++]]; - while(R>QUE[I].R) ans-=--ab[a[r--]]; -an[que[i].id]=ans; - } A for(i=1; i<=m;i++) +printf"%lld\n", An[i]); the return 0; -}
BZOJ-4542: [Hnoi2016] Large number (MO team algorithm)