M - なめらか部分文字列 / Smooth Substring Editorial by admin Editorial for printing
gpt-5.5-highOverview
For each \(x \leq N\), we count the number of contiguous substrings in its decimal representation that represent a “smooth number” after being converted to numerical values, denoted as \(f(x)\), and find their sum.
Since \(N\) can have up to \(10^5\) digits, we cannot enumerate the numbers themselves. Instead, we use digit DP to process all numbers from \(1\) to \(N\) together.
Analysis
If we naively examine all substrings for each \(x\), we cannot even enumerate the numbers because \(N\) is extremely large.
Furthermore, even for a single number of length \(L\), there are \(O(L^2)\) substrings, making direct counting difficult.
Therefore, we consider constructing the numbers digit by digit from left to right.
When we append a new digit \(d\) to the end of a decimal string \(S\) to form \(Sd\), the existing substrings remain unchanged.
The newly added substrings are only the suffixes ending with the last digit \(d\), i.e., the suffixes of \(Sd\).
Therefore, it is sufficient if we can quickly compute:
\[ f(Sd) = f(S) + \text{"number of smooth substrings ending at the end of } Sd\text{"} \]
The challenge here is when a substring contains leading 0s.
For example, 023 is numerically \(23\), so the smoothness check is performed on 23.
When appending a digit \(d\) to the end, we consider the suffixes of the preceding string \(S\):
- If a suffix consists entirely of
0s, appending \(d\) results in a numerical value of either \(d\) or \(0\), which is always smooth. - If a suffix represents a non-zero number, it is smooth if and only if the suffix itself is smooth and the absolute difference between its last digit and \(d\) is at most \(D\).
In other words, if we maintain the following information for each string \(S\), we can compute the increment when a digit is appended:
- The length of \(S\)
- \(f(S)\)
- The last digit
- Auxiliary information regarding smooth suffixes
Algorithm
We perform digit DP.
The states are broadly divided into two types:
0: already smaller than the corresponding prefix of \(N\)1: still matching the prefix of \(N\)
Additionally, we manage states where we have not yet started a positive number (i.e., we have only read leading zeros) as in0 and in1.
Suffix Management Information
Let \(S\) be the positive decimal string currently being constructed, and \(m\) be its length.
We define the following for \(S\):
- \(A(S)\): the number of suffixes of \(S\) consisting entirely of
0s - \(B(S)\): the number of suffixes of \(S\) representing non-zero smooth numbers
Consider appending a new digit \(d\) to the end.
Let \(j\) be the last digit of \(S\):
- If \(|j-d| \leq D\), non-zero smooth suffixes can be extended.
- If \(|j-d| > D\), non-zero suffixes cannot be extended.
Therefore, we maintain the following auxiliary quantities:
\[ P(S) = m - (1 + A(S) + B(S)) \]
\[ Z(S) = m - (1 + A(S)) \]
Then, the number of newly added smooth substrings when appending digit \(d\) to \(S\) is:
- When \(|j-d| \leq D\):
\[ m - P(S) \]
- When \(|j-d| > D\):
\[ m - Z(S) \]
Here, the \(1\) represents the substring consisting of only the newly appended single digit.
A single-digit number is always smooth, so it is always counted.
DP Array
For each tight/limit state, we maintain the following for each last digit \(0,\dots,9\):
c[d]: the number of current stringsf[d]: the sum of \(f(S)\) for those stringsl[d]: the sum of their lengths \(|S|\)p[d]: the sum of \(P(S)\) for those stringsz[d]: the sum of \(Z(S)\) for those strings
When appending a new digit \(d\), for all possible last digits \(j\), we compute:
\[ q(d) = \sum_{|j-d|\leq D} p[j] + \sum_{|j-d|>D} z[j] \]
This represents the sum of either \(P(S)\) or \(Z(S)\) chosen for each string depending on its compatibility with the appended digit \(d\).
Let the total count, the sum of \(f\), and the sum of lengths be:
\[ C = \sum_j c[j] \]
\[ F = \sum_j f[j] \]
\[ L = \sum_j l[j] \]
The updates after appending digit \(d\) are as follows:
\[ c'[d] \mathrel{+}= C \]
\[ f'[d] \mathrel{+}= F + L - q(d) \]
\[ l'[d] \mathrel{+}= L + C \]
\[ p'[d] \mathrel{+}= q(d) \]
Additionally, the update for \(Z(S)\) is as follows:
- When the appended digit is \(0\), the sequence of trailing
0s simply extends by one, so:
\[ z'[0] \mathrel{+}= \sum_j z[j] \]
- When the appended digit is non-zero, the end is no longer
0, meaning that suffixes consisting entirely of0s no longer exist. Thus:
\[ z'[d] \mathrel{+}= L \]
Starting a Number
When starting from a state where no positive number has begun, choosing a non-zero digit \(d\) starts a new single-digit number.
Since a single-digit number is always smooth, we have:
- \(c=1\)
- \(f=1\)
- \(l=1\)
Also, at this moment,
\[ A=0,\quad B=1 \]
which gives:
\[ P = 1 - (1+0+1) = -1 \]
\[ Z = 1 - (1+0) = 0 \]
In the code, this corresponds to the part where a new number starts:
nc[d] += v
nf[d] += v
np[d] -= v
nl[d] += v
Restricting to at most \(N\)
As in standard digit DP, at each digit, we transition as follows:
- If we are already in the “less” state, the next digit can be chosen freely from \(0\) to \(9\).
- If we are in the “equal” state, the next digit must be at most the corresponding digit of \(N\).
Finally, the sum of f from both the less and equal states is the answer.
Complexity
- Time Complexity: \(O(|N|)\)
- Space Complexity: \(O(1)\)
For each digit, the length of the arrays we handle is only \(10\), so it can be processed in constant time.
Key Implementation Details
- \(P(S)\) can be negative. For example, \(P = -1\) for a single-digit number.
Therefore, make sure to take modulo \(10^9+7\) after updates. lo[d]andhi[d]represent the range of the preceding digit that can be adjacent to digit \(d\).- Since
q(d)can be rewritten as:
\[ q(d)=\sum p[j]+\sum_{|j-d|>D}(z[j]-p[j]) \]
we can compute it quickly in the code using prefix sums of z[j] - p[j].
- The state where the number has not yet started eventually corresponds to the number \(0\). Since the problem asks for the sum from \(1\) to \(N\), we do not include in0 and in1 in the final answer.
Source Code
import sys
MOD = 1000000007
def main():
data = sys.stdin.buffer.read().split()
N = data[0]
D = int(data[1])
lo = [0] * 10
hi = [0] * 10
for d in range(10):
lo[d] = max(0, d - D)
hi[d] = min(10, d + D + 1)
c0 = [0] * 10
f0 = [0] * 10
p0 = [0] * 10
z0 = [0] * 10
l0 = [0] * 10
c1 = [0] * 10
f1 = [0] * 10
p1 = [0] * 10
z1 = [0] * 10
l1 = [0] * 10
in0 = 0
in1 = 1
for ch in N:
lim = ch - 48
nc0 = [0] * 10
nf0 = [0] * 10
np0 = [0] * 10
nz0 = [0] * 10
nl0 = [0] * 10
nc1 = [0] * 10
nf1 = [0] * 10
np1 = [0] * 10
nz1 = [0] * 10
nl1 = [0] * 10
ni0 = 0
ni1 = 0
if in0:
v = in0
ni0 += v
for d in range(1, 10):
nc0[d] += v
nf0[d] += v
np0[d] -= v
nl0[d] += v
if in1:
v = in1
if lim == 0:
ni1 += v
else:
ni0 += v
for d in range(1, lim):
nc0[d] += v
nf0[d] += v
np0[d] -= v
nl0[d] += v
if lim >= 1:
nc1[lim] += v
nf1[lim] += v
np1[lim] -= v
nl1[lim] += v
TC = TF = TP = TZ = TL = 0
s = 0
pref = [0] * 11
for j in range(10):
TC += c0[j]
TF += f0[j]
pv = p0[j]
zv = z0[j]
TP += pv
TZ += zv
TL += l0[j]
s += zv - pv
pref[j + 1] = s
full = s
baseF = TF + TL
baseLen = TL + TC
for d in range(10):
q = TP + pref[lo[d]] + full - pref[hi[d]]
nc0[d] += TC
nf0[d] += baseF - q
np0[d] += q
nz0[d] += TZ if d == 0 else TL
nl0[d] += baseLen
TC = TF = TP = TZ = TL = 0
s = 0
pref = [0] * 11
for j in range(10):
TC += c1[j]
TF += f1[j]
pv = p1[j]
zv = z1[j]
TP += pv
TZ += zv
TL += l1[j]
s += zv - pv
pref[j + 1] = s
full = s
baseF = TF + TL
baseLen = TL + TC
for d in range(lim):
q = TP + pref[lo[d]] + full - pref[hi[d]]
nc0[d] += TC
nf0[d] += baseF - q
np0[d] += q
nz0[d] += TZ if d == 0 else TL
nl0[d] += baseLen
d = lim
q = TP + pref[lo[d]] + full - pref[hi[d]]
nc1[d] += TC
nf1[d] += baseF - q
np1[d] += q
nz1[d] += TZ if d == 0 else TL
nl1[d] += baseLen
for j in range(10):
nc0[j] %= MOD
nf0[j] %= MOD
np0[j] %= MOD
nz0[j] %= MOD
nl0[j] %= MOD
nc1[j] %= MOD
nf1[j] %= MOD
np1[j] %= MOD
nz1[j] %= MOD
nl1[j] %= MOD
c0, f0, p0, z0, l0 = nc0, nf0, np0, nz0, nl0
c1, f1, p1, z1, l1 = nc1, nf1, np1, nz1, nl1
in0 = ni0 % MOD
in1 = ni1 % MOD
print((sum(f0) + sum(f1)) % MOD)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.
posted:
last update: