bzoj4542: [Hnoi2016]大数

18 篇文章 0 订阅
2 篇文章 0 订阅

题目链接

bzoj4542

题目描述

Description

  小 B 有一个很大的数 S,长度达到了 N 位;这个数可以看成是一个串,它可能有前导 0,例如00009312345
。小B还有一个素数P。现在,小 B 提出了 M 个询问,每个询问求 S 的一个子串中有多少子串是 P 的倍数(0 也
是P 的倍数)。例如 S为0077时,其子串 007有6个子串:0,0,7,00,07,007;显然0077的子串007有6个子串都是素
数7的倍数。

Input

  第一行一个整数:P。第二行一个串:S。第三行一个整数:M。接下来M行,每行两个整数 fr,to,表示对S 的
子串S[fr…to]的一次询问。注意:S的最左端的数字的位置序号为 1;例如S为213567,则S[1]为 2,S[1…3]为 2
13。N,M<=100000,P为素数

Output

  输出M行,每行一个整数,第 i行是第 i个询问的答案。

Sample Input

11
121121
3
1 6
1 5
1 4

Sample Output

5
3
2

HINT

第一个询问问的是整个串,满足条件的子串分别有:121121,2112,11,121,121。
2016.4.19新加数据一组

题解

我们可以先预处理出字符串的后缀模 P 的值,记为a[i]。考虑一个子串 s[l...r] ,设它模P的值为 x ,则有x10nr=a[l]a[r+1]。若 P 不为2 5 ,则10imodP0。则 x=0 当且仅当 a[l]=a[r+1] 。这样我们考虑莫队算法,当端点移动时我们只要统计区间内有多少个数等于某个数即可,离散之后直接做就行了。当 P 等于2 5 <script type="math/tex" id="MathJax-Element-530">5</script>时,一个数能否被P整除,只与该位末位有关了,预处理一下就行了。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;

const int N=100010;
typedef long long ll;
ll P;
char s[N];
int n,m,x,y,S,now,ans[N],a[N],b[N],c[N],cnt1[N],cnt2[N];
struct query{
    int l,r,id;
    void init(int k){id=k; scanf("%d%d",&l,&r);}
    friend bool operator <(const query &a,const query &b){
        if(a.l/S==b.l/S) return a.r<b.r;
        return a.l/S<b.l/S;
    }
}q[N];

void incr(int &x){cnt1[c[++x]]++; cnt2[c[x+1]]++; now+=cnt1[c[x+1]];}
void decr(int &x){now-=cnt1[c[x+1]]; cnt2[c[x+1]]--; cnt1[c[x--]]--;}
void incl(int &x){now-=cnt2[c[x]]; cnt1[c[x]]--; cnt2[c[++x]]--;}
void decl(int &x){cnt2[c[x]]++; cnt1[c[--x]]++; now+=cnt2[c[x]];}
bool cmp(int x,int y){return a[x]<a[y];}
void solve(){
    static ll cnt[N],sum[N];
    for(int i=1;i<=n;i++) cnt[i]=cnt[i-1]+((s[i]-'0')%P==0);
    for(int i=1;i<=n;i++) sum[i]=sum[i-1]+((s[i]-'0')%P==0?i:0);
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        printf("%lld\n",sum[y]-sum[x-1]-(cnt[y]-cnt[x-1])*(x-1));
    }
}
int main(){
    scanf("%lld%s",&P,s+1);
    b[0]=1; n=strlen(s+1); S=sqrt(n);
    if(P==2||P==5) {solve(); return 0;}
    for(int i=1;i<=n;i++) b[i]=10ll*b[i-1]%P;
    for(int i=n;i;i--) a[i]=(1ll*b[n-i]*(s[i]-'0')+a[i+1])%P;
    for(int i=0;i<=n;i++) b[i]=i;
    sort(b+1,b+1+n,cmp);
    for(int i=1;i<=n;i++) c[b[i]]=c[b[i-1]]+(a[b[i-1]]!=a[b[i]]);
    scanf("%d",&m);
    for(int i=1;i<=m;i++) q[i].init(i);
    sort(q+1,q+1+m);
    x=y=1; cnt1[c[1]]++; cnt2[c[2]]++; now=cnt1[c[2]];
    for(int i=1;i<=m;i++){
        while(y<q[i].r) incr(y);
        while(x<q[i].l) incl(x);
        while(x>q[i].l) decl(x);
        while(y>q[i].r) decr(y);
        ans[q[i].id]=now; 
    }
    for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值