[BZOJ4542]大數

題目鏈接BZOJ4542

題目大意
給出一個數字串,問某個子串有多少個子串可以被P 整除。N1e5

分析
1. 又是一道長得就非常莫隊的題,思考怎麼轉移。
2. 當P2P5 時。設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=2P=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;
}

以上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章