# bzoj4542 [Hnoi2016]大数 莫队+同余

## 题目传送门

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

## 题解

$\frac{f_l - f_{r+1}}{10^{n-r}} = 0 \pmod P$

#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];

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);

while (m--) {
int l, r;
printf("%lld\n", ss[r] - ss[l - 1] - (l - 1ll) * (s[r] - s[l - 1]));
}
}
}

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;
}

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();
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) 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() {
scanf("%s", a + 1);
n = strlen(a + 1);
}

int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();