[BZOJ4542]大数

题目链接BZOJ4542

题目大意
给出一个数字串,问某个子串有多少个子串可以被 P 整除。N1e5

分析
1. 又是一道长得就非常莫队的题,思考怎么转移。
2. 当 P2 P5 时。设 suf[i] 为从 i 开始的后缀modP的值,如果有 suf[i]=suf[j]ij ,那么子串 num[l..j1]modP=0
3. 证明:设 num[i..j1]=Anum[j..n]=B ,则有 A×10lenthB+BBmodP ,又 10lenthBmodP0 ,所以 AmodP=0
4. 那么询问就变成了:问区间 [l..r+1] 内有多少对相等的 suf ,经典的莫队题目。
5. 当 P=2 P=5 时,可以直接由子串的最后一位判断。

上代码

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

typedef long long LL;
const int SQR = 320;
const int N = 1e5 + 10;

int n, m, mod;
char ss[N];
inline int read() {
    char ch;
    int ans = 0, neg = 1;
    while (ch = getchar(), ch < '0' || ch > '9')
        if (ch == '-') neg = -1;
    while (ch >= '0' && ch <= '9')
        ans = ans * 10 + ch - '0', ch = getchar();
    return ans * neg;
}

struct nodeQry {
    int l, r, bl, id, ans;
    nodeQry() {}
    nodeQry(int a, int b) : l(a), r(b) { bl = l / SQR; }
    inline void input(int a) {
        l = read(), r = read() + 1, bl = l / SQR, id = a;
    }
    inline bool operator < (const nodeQry &a) const {
        return (bl != a.bl ? (bl < a.bl) : (r < a.r));
    }
} qry[N];
struct nodeAns {
    int id; LL ans;
    nodeAns() {}
    nodeAns(int a, LL b) : id(a), ans(b) {}
    inline bool operator < (const nodeAns &a) const {
        return id < a.id;
    }
} ans[N];

namespace ans1 {
    int pT[N], val[N];
    struct node {
        int id, val;
        inline bool operator < (const node &a) const {
            return val < a.val;
        }
    } map[N];
    void init() {
        pT[0] = 1;
        for (int i = 1; i <= n; i++)
            pT[i] = (LL)pT[i - 1] * 10 % mod;
        for (int i = n; i >= 1; i--) {
            val[i] = ((LL)(ss[i] - '0') * pT[n - i] + val[i + 1]) % mod;
            map[i].id = i, map[i].val = val[i];
        } sort(map + 1, map + n + 1);
        int cnt = 0;
        for (int i = 1; i <= n; i++)
            if (map[i].val != map[i - 1].val)
                val[map[i].id] = ++cnt;
            else val[map[i].id] = cnt;
    }
    LL tot;
    int cnt[N];
    void figure() {
        nodeQry tmp = nodeQry(qry[1].l, qry[1].l);
        cnt[val[tmp.l]]++;
        for (int i = 1; i <= m; i++) {
            while (tmp.r < qry[i].r) tot += (LL)cnt[val[++tmp.r]]++;
            while (tmp.l > qry[i].l) tot += (LL)cnt[val[--tmp.l]]++;
            while (tmp.r > qry[i].r) tot -= (LL)--cnt[val[tmp.r--]];
            while (tmp.l < qry[i].l) tot -= (LL)--cnt[val[tmp.l++]];
            ans[i] = nodeAns(qry[i].id, tot);
        } sort(ans + 1, ans + m + 1);
        for (int i = 1; i <= m; i++) printf("%lld\n", ans[i].ans);
    }
    void main() {
        init();
        figure();
    }
}

namespace ans2 {
    int val[N];
    void init() {
        for (int i = 1; i <= n; i++) val[i] = ss[i] - '0';
    }
    LL tot;
    int cnt;
    void figure() {
        nodeQry tmp = nodeQry(qry[1].l, qry[1].l);
        tot = cnt = val[tmp.l] % mod ? 0 : 1;
        for (int i = 1; i <= m; i++) {
            qry[i].r--;
            while (tmp.r < qry[i].r)
                if (!(val[++tmp.r] % mod))
                    cnt++, tot += (LL)(tmp.r - tmp.l + 1);
            while (tmp.l > qry[i].l) {
                if (!(val[--tmp.l] % mod)) cnt++;
                tot += (LL)cnt;
            }
            while (tmp.r > qry[i].r)
                if (!(val[tmp.r--] % mod))
                    cnt--, tot -= (LL)(tmp.r - tmp.l + 2);
            while (tmp.l < qry[i].l) {
                tot -= (LL)cnt;
                if (!(val[tmp.l++] % mod)) cnt--;
            }
            ans[i] = nodeAns(qry[i].id, tot);
        } sort(ans + 1, ans + m + 1);
        for (int i = 1; i <= m; i++)
            printf("%lld\n", ans[i].ans);
    }
    void main() {
        init();
        figure();
    }
}

int main() {
    scanf("%d %s", &mod, ss + 1);
    n = strlen(ss + 1), scanf("%d", &m);
    for (int i = 1; i <= m; i++)
        qry[i].input(i);
    sort(qry + 1, qry + m + 1);
    if (mod != 2 && mod != 5)
        ans1::main();
    else ans2::main(); // 哇,感觉我的ans2写得比ans1丑多了
    return 0;
}

以上

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值