bzoj4542 [Hnoi2016] Tarsus Mo + team congruence

Topic Portal

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

answer

We make \ (F_i \) represents the \ (I \) to (n-\) \ bit composition \ (\ bmod P \) values.

Then from a \ (l, r \) right string is \ (\ {f_L FRAC - R & lt F_ {} {}. 1 + 10 ^ {NR}} \) .

If desired this thing \ (= 0 \) , i.e.
\ [\ frac {f_l - f_
{r + 1}} {10 ^ {nr}} = 0 \ pmod P \] The next step is clearly put \ (10 ^ {nr} \) take up. However, \ (10 ^ {nr} \ ) in the mold \ (P \) has the meaning of the inverse element, if and only if \ (10 \) and \ (P \) coprime.

So to be divided into two cases.


The first is \ (P = 2 \) or \ (5 \) , then \ (10 \) and \ (P \) is not prime, not directly by \ (NR 10 ^ {} \) .

It can be found, \ (P = 2 \) , the end of the substring satisfying the condition must be \ (2,. 4,. 6,. 8, 0 \) ; \ (P =. 5 \) , the end of the sub-string that satisfies conditions must be \ (5, 0 \) .

This property can be used directly addressed.


The second is \ (P \ neq 2, 5 \) . At this time, \ (10 \) and \ (P \) coprime, so we \ (10 ^ {nr} \ ) by the past.

To give \ (f_L - R & lt F_ {+} = 0. 1 \) .

That is asking for a \ (l, r \) we need to do is to find \ ([l, r + 1 ] \) the number of rights within this range of values for the same point.

You can use MO team resolved.


The worst case time complexity \ (O (m \ n-sqrt) \) .

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 100000 + 7;

#define bl(x) (((x) - 1) / blo + 1)

int n, m, P;
char a[N];

namespace Task1 {
    inline bool istask() { return P == 2 || P == 5; }
    int s[N];
    ll ss[N];
    
    inline void work() {
        for (int i = 1; i <= n; ++i) a[i] -= '0';
        for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + !(a[i] % P), ss[i] = ss[i - 1] + i * !(a[i] % P);
        
        read(m);
        while (m--) {
            int l, r;
            read(l), read(r);
            printf("%lld\n", ss[r] - ss[l - 1] - (l - 1ll) * (s[r] - s[l - 1]));
        }
    }
}

namespace Task2 {
    int dis, blo;
    ll msum = 0;
    int f[N], b[N], cnt[N];
    ll ans[N];
    struct Query {
        int l, r;
        ll *ans;
        inline bool operator < (const Query &b) { return bl(l) != bl(b.l) ? l < b.l : r < b.r; }
    } q[N];
    
    inline int smod(int x) { return x >= P ? x - P : x; }
    inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
    inline int fpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
        return ans;
    }
    
    inline void madd(int x) {
        int &v = f[x];
        msum -= (ll)cnt[v] * (cnt[v] - 1) / 2;
        ++cnt[v];
        msum += (ll)cnt[v] * (cnt[v] - 1) / 2;
    }
    inline void mdel(int x) {
        int &v = f[x];
        assert(cnt[v]);
        msum -= (ll)cnt[v] * (cnt[v] - 1) / 2;
        --cnt[v];
        msum += (ll)cnt[v] * (cnt[v] - 1) / 2;
    }
    
    inline void ycl() {
        int ten = 1;
        for (int i = 1; i <= n; ++i) a[i] -= '0';
        for (int i = n; i; --i) f[i] = smod(f[i + 1] + (ll)a[i] * ten % P), ten = 10ll * ten % P;
        memcpy(b + 1, f + 1, sizeof(int) * (n + 1));
    }
    inline void lsh() {
        std::sort(b + 1, b + n + 1);
        dis = std::unique(b + 1, b + n + 1) - b - 1;
        for (int i = 1; i <= n; ++i) f[i] = std::lower_bound(b + 1, b + dis + 1, f[i]) - b;
    }
    
    inline void work() {
        blo = sqrt(n);
        ycl();
        ++n;
        lsh();
        read(m);
        for (int i = 1; i <= m; ++i) read(q[i].l), read(q[i].r), ++q[i].r, q[i].ans = ans + i;
        std::sort(q + 1, q + m + 1);
        int l = 1, r = 0;
        for (int i = 1; i <= m; ++i) {
            while (l > q[i].l) madd(--l);
            while (r < q[i].r) madd(++r);
            while (l < q[i].l) mdel(l++);
            while (r > q[i].r) mdel(r--);
            *q[i].ans = msum;
        }
        for (int i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
    }
}

inline void init() {
    read(P);
    scanf("%s", a + 1);
    n = strlen(a + 1);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    if (Task1::istask()) Task1::work();
    else Task2::work();
    fclose(stdin), fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/bzoj4542.html