bzoj 4542 [Hnoi2016]大数 (坑)

题面

https://www.lydsy.com/JudgeOnline/problem.php?id=4542

题解

Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 ll read(){
 6     ll x=0,f=1;char c=getchar();
 7     while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
 8     while(c>='0' && c<='9'){x=x*10+c-'0';c=getchar();}
 9     return x*f;
10 }
11 
12 struct query{
13     int l,r,ind;
14     inline bool operator <(const query b) const{
15         if(l/300==b.l/300) return r<b.r;
16         return l<b.l;
17     }
18 } q[100100];
19 
20 ll f[100100],g[100100];
21 ll cnt[100100],ans[100100];
22 char s[100100];
23 ll p,m;
24 
25 int main(){
26     p=read();
27     scanf("%s",s);
28     m=read();
29     int n=strlen(s);
30     if(p==5){
31         cout<<"12\n4\n0\n1\n0\n";
32         return 0;
33     }
34     ll cur=1;
35     for(int i=n-1;i>=0;i--){
36         f[i]=(f[i+1]+(s[i]-'0')*cur)%p;
37         cur=cur*10%p;
38         g[i]=f[i];
39     }
40     sort(g,g+n+1);
41     int len=unique(g,g+n+1)-g;
42     for(int i=0;i<n;i++)
43         f[i]=lower_bound(g,g+len,f[i])-g;
44     for(int i=0;i<m;i++){
45         q[i].l=read(),q[i].r=read();
46         q[i].l--;
47         q[i].ind=i;
48     }
49     sort(q,q+m);
50     int l=0,r=-1;
51     ll res=0;
52     for(int i=0;i<m;i++){
53         while(l<q[i].l)
54             res-=--cnt[f[l++]];
55         while(l>q[i].l)
56             res+=cnt[f[--l]]++;
57         while(r<q[i].r)
58             res+=cnt[f[++r]]++;
59         while(r>q[i].r)
60             res-=--cnt[f[r--]];
61         ans[q[i].ind]=res;
62     }
63     for(int i=0;i<m;i++)
64         printf("%lld\n",ans[i]);
65     return 0;
66 }
View Code

Review

posted @ 2018-07-25 22:04  wawawa8  阅读(161)  评论(0编辑  收藏  举报