#P10262. [GESP样题 六级] 亲朋数

[GESP样题 六级] 亲朋数

Background

Related multiple-choice and true/false problems: https://ti.luogu.com.cn/problemset/1106.

Problem Description

You are given a digit string SS of length LL, consisting of digits 090 \sim 9. It is easy to see that it has L(L+1)2\frac{L(L + 1)}2 contiguous substrings in total. If the number represented by a substring (leading zeros are allowed) is a multiple of pp, then this substring is called a “friendly substring” of the digit string SS with respect to pp.

For example, if the digit string SS is “ 1234212342 ” and p=2p = 2, then among the 1515 contiguous substrings, the friendly substrings are “ 1212 ”, “ 12341234 ”, “ 1234212342 ”, “ 22 ”, “ 234234 ”, “ 23422342 ”, “ 3434 ”, “ 342342 ”, “ 44 ”, “ 4242 ”, and “ 22 ”, for a total of 1111. Note that “ 22 ” appears 22 times, but since they occur at different positions in SS, they are counted as different friendly substrings.

Now, given the digit string SS and a positive integer pp, can you compute how many friendly substrings there are?

Input Format

The first line contains a positive integer pp. It is guaranteed that 2p1282 \leq p \leq 128.
The second line contains a digit string SS of length LL. It is guaranteed that 1L1061 \leq L \leq 10^6.

Output Format

Output one line containing one integer, representing the answer.

2
102
5

2
12342
11

Hint

Sample 1 Explanation

There are 55 friendly substrings: 1010, 102102, 00, 0202, and 22.

Translated by ChatGPT 5