# bzoj 4542: [Hnoi2016]大数

### solution

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=100005;
int n,m,block;char s[N];ll mod;
namespace solve1{
ll sum[N],c[N];
void main(){
for(int i=1;i<=n;i++){
sum[i]=sum[i-1],c[i]=c[i-1];
if((s[i]-'0')%mod==0)sum[i]+=i,c[i]++;
}
int m,x,y;cin>>m;
while(m--){
scanf("%d%d",&x,&y);
printf("%lld\n",(sum[y]-sum[x-1])-(x-1)*(c[y]-c[x-1]));
}
}
}
struct node{
int l,r,b,id;
bool operator <(const node &pr)const{
if(b!=pr.b)return b<pr.b;
return r<pr.r;
}
}q[N];
ll v[N],t[N],b[N];ll ans[N],ret=0;
inline void add(int x){ret+=t[v[x]];t[v[x]]++;}
inline void delet(int x){t[v[x]]--;ret-=t[v[x]];}
void solve(){
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].r++;q[i].id=i;
q[i].b=q[i].l/block;
}
sort(q+1,q+m+1);
ll str=0,tot=1;
for(int i=n;i>=1;i--){
str+=tot*(s[i]-48);str%=mod;
tot*=10;tot%=mod;
v[i]=b[i]=str;
}
b[0]=0;
sort(b,b+n+1);
int to=unique(b,b+n+1)-b-1;
for(int i=1;i<=n+1;i++)v[i]=lower_bound(b,b+to+1,v[i])-b;
int l=1,r=0;
for(int i=1;i<=m;i++){
while(r>q[i].r)delet(r),r--;
while(l<q[i].l)delet(l),l++;
ans[q[i].id]=ret;
}
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}
void work()
{
scanf("%lld%s",&mod,s+1);
n=strlen(s+1);block=sqrt(n);
if(mod==2 || mod==5)solve1::main();
else solve();
}

int main()
{
work();
return 0;
}


posted @ 2017-12-03 00:53  PIPIBoss  阅读(100)  评论(0编辑  收藏  举报