M - Smooth Substring Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

整数 D(0 \leq D \leq 9)が与えられます。まず、D を用いて非負整数に対する「なめらかな数」を定義します。

非負整数 v の標準的な十進表記とは、v \geq 1 のときは先頭に 0 を付けない十進表記、v = 0 のときは 0 を指します。非負整数 v がなめらかな数であるとは、v の標準的な十進表記において、隣り合うどの2桁についても、その差の絶対値が D 以下であることを指します。標準的な十進表記が1桁である非負整数(0 以上 9 以下)は、隣り合う2桁の組が存在しないため、常になめらかな数です。

次に、正の整数 x に対して f(x) を次のように定めます。

x の標準的な十進表記を文字列 S とみなします。S の長さを L とすると、S の連続部分文字列(S の連続する1文字以上を取り出して得られる文字列)は、切り出す開始位置と終了位置の組によって区別され、全部で L(L+1)/2 個あります。例えば x = 1023 のとき S = 1023 であり、連続部分文字列は 1, 10, 102, 1023, 0, 02, 023, 2, 23, 3 の 10 個です。

これら L(L+1)/2 個の連続部分文字列それぞれについて、次の手順でなめらかな数であるかを判定します。

  1. その連続部分文字列を十進法の非負整数として解釈します。先頭に 0 がある場合はそれを無視して数値化します(例えば 023 は 23、00 は 0 と解釈します)。
  2. 得られた非負整数がなめらかな数であるかを判定します。なめらかな数の判定は、その非負整数の標準的な十進表記に対して行います。すなわち、元の部分文字列ではなく、数値に変換した後の標準的な十進表記における隣接桁の差で判定します。

f(x) は、L(L+1)/2 個の連続部分文字列のうち、上の手順でなめらかな数と判定されるものの個数です。

補足: 異なる位置から切り出された連続部分文字列は、数値として同じ値になる場合でも、それぞれ別々に数えます。例えば x = 121 のとき S = 121 から、1文字目の 1 と3文字目の 1 はどちらも値 1(なめらかな数)ですが、これらは別々に数えて 2 個とします。

正整数 N が十進表記の文字列として与えられます。f(1) + f(2) + \cdots + f(N) を 10^9 + 7 で割った余りを求めてください。

制約

  • N は先頭に不要な 0 を含まない正整数の十進表記である
  • 1 \leq |N| \leq 10^5
  • ここで |N| は N の十進表記の桁数を表す
  • 0 \leq D \leq 9
  • D は整数である

入力

N
D
  • 1 行目には、正整数 N が十進表記の文字列として与えられる。
  • 2 行目には、なめらかな数の条件における隣接桁の差の絶対値の上限 D が整数で与えられる。

出力

f(1) + f(2) + \cdots + f(N) を 10^9 + 7 で割った余りを1行で出力せよ。


入力例 1

20
1

出力例 1

34

入力例 2

101
0

出力例 2

206

入力例 3

3141592653589793238462643383279
2

出力例 3

584892474

入力例 4

12345678901234567890123456789012345678901234567890123456789012345678901234567890
4

出力例 4

940477512

入力例 5

1
9

出力例 5

1

Score : 500 pts

Problem Statement

You are given an integer D (0 \leq D \leq 9). First, we define a "smooth number" for non-negative integers using D.

The standard decimal representation of a non-negative integer v is the decimal representation without leading zeros if v \geq 1, and 0 if v = 0. A non-negative integer v is a smooth number if, in its standard decimal representation, the absolute difference between any two adjacent digits is at most D. Non-negative integers with a 1-digit standard decimal representation (0 to 9) are always smooth numbers because there are no adjacent pairs of digits.

Next, for a positive integer x, we define f(x) as follows.

Consider the standard decimal representation of x as a string S. Let L be the length of S. There are L(L+1)/2 contiguous substrings of S (obtained by extracting one or more consecutive characters of S), distinguished by their starting and ending positions. For example, if x = 1023, then S = 1023, and the 10 contiguous substrings are 1, 10, 102, 1023, 0, 02, 023, 2, 23, and 3.

For each of these L(L+1)/2 contiguous substrings, we determine if it represents a smooth number using the following procedure:

  1. Interpret the contiguous substring as a non-negative integer in base 10. If there are leading zeros, ignore them to obtain the numerical value (for example, 023 is interpreted as 23, and 00 as 0).
  2. Determine if the resulting non-negative integer is a smooth number. Note that this determination is based on the standard decimal representation of that non-negative integer. That is, it is judged by the differences between adjacent digits in its standard decimal representation after converting to a number, not in the original substring itself.

f(x) is the number of contiguous substrings among the L(L+1)/2 ones that are determined to be smooth numbers by the above procedure.

Note: Contiguous substrings extracted from different positions are counted separately, even if they have the same numerical value. For example, if x = 121, then from S = 121, the 1 at the 1st character and the 1 at the 3rd character both have the value 1 (a smooth number), but they are counted separately as 2 occurrences.

You are given a positive integer N as a string in decimal representation. Find the sum f(1) + f(2) + \cdots + f(N) modulo 10^9 + 7.

Constraints

  • N is the decimal representation of a positive integer without leading zeros.
  • 1 \leq |N| \leq 10^5
  • Here, |N| denotes the number of digits in the decimal representation of N.
  • 0 \leq D \leq 9
  • D is an integer.

Input

N
D
  • The first line contains a positive integer N as a string in decimal representation.
  • The second line contains an integer D, the maximum allowed absolute difference between adjacent digits in the condition for smooth numbers.

Output

Print the sum f(1) + f(2) + \cdots + f(N) modulo 10^9 + 7 in a single line.


Sample Input 1

20
1

Sample Output 1

34

Sample Input 2

101
0

Sample Output 2

206

Sample Input 3

3141592653589793238462643383279
2

Sample Output 3

584892474

Sample Input 4

12345678901234567890123456789012345678901234567890123456789012345678901234567890
4

Sample Output 4

940477512

Sample Input 5

1
9

Sample Output 5

1