問題一覧 > 通常問題

No.372 It's automatic

レベル : / AI推定難易度 : 1325 / 実行時間制限 : 1ケース 6.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 89
作問者 : koba-e964 / テスター : 🍡yurahuna
お気に入りにしたユーザー ProblemId : 598 / 自分の提出
問題文最終更新日: 2026-06-25 04:40:12
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 137 (順位表) の他の問題:

Note

Note: The intended solution is implemented in C++ and takes about 1 to 2 seconds in the worst case (the time limit is 6 seconds). If implemented in other languages, it is likely to exceed the time limit.
In Java it takes 3667ms.
The tester's solution in Python (PyPy) gets TLE.

Problem Statement

You are given a string $S$ consisting of characters from '0' to '9', and a positive integer $M$.
Output, mod 1,000,000,007$=10^9 + 7$, the total number of subsequences of one or more characters that, when interpreted as a decimal number, are multiples of $M$.
Here, a subsequence of a string $S$ is a string that can be expressed as $S[a_1]S[a_2]\cdots S[a_k]$ using a sequence $(a_i)_i$ such that
$1 \le a_1 < a_2 < \cdots < a_k\le |S|$
holds. ($S[i]$ is the $i$-th character of $S$.)
Subsequences that are the same as strings but differ in positions are treated as different. For example, when $S="110"$, the subsequence "10" formed by the 1st and 3rd characters and the subsequence "10" formed by the 2nd and 3rd characters are counted separately.
Also, subsequences of two or more characters that start with 0 are not counted. For example, when $S="02", M = 2$, "02" is not regarded as a multiple of 2.

Input

$S$
$M$

$1 \le |S| \le 10,000$
$1 \le M \le 20,000$
Each character of $S$ is between '0' and '9'.
Among the sample cases, those whose file names start with "testcase-small1-" satisfy $1 \le |S| \le 16, 1 \le M \le 1,000 $.
Among the sample cases, those whose file names start with "testcase-small2-" satisfy $1 \le |S| \le 10,000, 1 \le M \le 1,000 $.

Output

Output the result. Print a newline at the end.

Sample

Sample 1
Input
012
3
Output
2

There are 2 such subsequences: 0 and 12. 012 starts with 0 and has two or more characters, so it is not counted.

Sample 2
Input
220
2
Output
7

There are 7 such subsequences: 2 (1st character), 2 (2nd character), 0, 22, 20 (1st and 3rd characters), 20 (2nd and 3rd characters), and 220.

Sample 3
Input
123456789123456789
1
Output
262143

$262143 = 2^{18} - 1$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。