PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000147. Đếm chuỗi con chia hết (Divisible Substring)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm chuỗi con chia hết (Divisible Substring)
Divisible Substring
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Takahashi has a decimal digit string of length and a prime number . For each pair , consider the contiguous substring as a base-10 integer.
Count the pairs whose integer value is divisible by . Leading zeros are allowed. Substrings at different positions count separately, even if they have the same contents or numeric value.
Input
The first line contains . The next line contains exactly characters of S, each a digit from 0 to 9.
Output
Print the number of qualifying contiguous substrings.
Subtasks
- Subtask 1 (20 points): ; ; is prime.
- Subtask 2 (30 points): ; ; is prime.
- Subtask 3 (50 points): ; ; is prime.
Examples
Input
4 3
3543
Output
6
Explanation
The ten nonempty substrings are 3, 35, 354, 3543, 5, 54, 543, 4, 43, 3. Exactly six are divisible by : the first 3, 354, 3543, 54, 543, and the last 3.