#P3245. [HNOI2016] 大数

[HNOI2016] 大数

Description

Xiao B has a very large number SS of length nn; this number can be regarded as a string of digits and may have leading 00s, for example, 00009312345. Xiao B also has a prime pp. Now, Xiao B proposes mm queries. For each query, within a substring of SS, count how many of its substrings are multiples of pp (00 is also considered a multiple of pp). For example, when SS is 0077, its substring 007 has 66 substrings: 0,0,7,00,07,007; clearly, all 66 substrings of 007 are multiples of the prime 77.

Input Format

The first line contains an integer pp.

The second line contains a digit string SS.

The third line contains an integer mm. Then mm lines follow, each containing two integers fr,tofr, to, representing a query on the substring S[frto]S[fr\dots to] of SS. Note: the leftmost digit of SS has index 11; for example, if SS is 213567, then S[13]S[1\dots 3] is 213.

Output Format

Output mm lines, each containing one integer. The ii-th line is the answer to the ii-th query.

11
121121
3
1 6
1 5
1 4
5
3
2

Hint

Sample 1 Explanation

The first query asks about the entire string. The substrings that satisfy the condition are: 121121,2112,11,121,121.

Constraints

For 100%100\% of the testdata, 1n,m2×1051\le n,m\le 2\times 10^5, 2p1092\le p\le 10^9, SS contains only digit characters, and pp is prime.

Translated by ChatGPT 5