公式

M - なめらか部分文字列 / Smooth Substring 解説 by admin

gpt-5.5-high

Overview

For each integer from \(1\) to \(N\), this problem asks us to count how many of its contiguous substrings, when converted to a numerical value, form a smooth number, and find the sum of these counts.

Since the number of digits of \(N\) can be as large as \(10^5\), which is extremely large, we cannot check each integer or each substring directly. Instead, we count them collectively using digit DP.

Analysis

Let us look at the decimal representation of an integer from left to right, and append a new digit to the end.

Since the existing substrings remain unchanged, the only new substrings created are those that end with the newly appended digit (i.e., suffixes of the new prefix).

Therefore, if we know the following information for each prefix, we can update it when one digit is appended:

  • The current sum of answers \(f\) so far.
  • The number of “smooth suffixes” ending at the current last digit.

However, in this problem, leading 0s in a substring are ignored when converting it to a numerical value.

For example, 02 is treated as the numerical value \(2\), which is always smooth.
On the other hand, if we were to look at the difference between adjacent digits of the raw string 02, i.e., \(|0-2|\), we would incorrectly identify it as non-smooth (if \(D < 2\)).

Thus, we manage the suffixes ending at the current digit by dividing them into two types:

  • \(A\): The number of smooth suffixes with a positive numerical value.
  • \(Z\): The number of suffixes consisting entirely of 0s.

For example, if we append a non-zero digit \(d\) to the end:

  • Appending \(d\) to the preceding 0, 00, … yields 0...0d. Since its numerical value is \(d\), it is always smooth.
  • A suffix that was previously a positive smooth suffix remains smooth if its difference with the last digit is at most \(D\).

On the other hand, if we append 0 to the end:

  • The number of suffixes consisting entirely of 0s increases by 1.
  • A positive smooth suffix can be extended if the difference between its last digit and 0 is at most \(D\).

Since we cannot directly iterate through integers from \(1\) to \(N\), we manage this information collectively using digit DP.

Algorithm

Let \(M\) be the number of digits of \(N\).
We perform digit DP to construct a string of length \(M\) from left to right.

Note that numbers with fewer digits than \(N\) will be treated as having virtual leading 0s.
Since we must not count substrings starting from these virtual leading 0s, we separately maintain a state representing “no non-zero digits have appeared yet.”

DP State

emp[tight]

represents the number of prefixes where no non-zero digits have appeared yet, meaning the number has not started.

dp[tight][last]

represents the prefixes where the number has already started, and the current last digit is last.

tight is the same as in standard digit DP:

  • tight = 1: The prefix matches \(N\) so far.
  • tight = 0: The prefix is already strictly smaller than \(N\).

Each dp[tight][last] stores the following values:

struct Node {
    ll cnt;
    ll a;
    ll z;
    ll ans;
};

The meanings are as follows:

  • cnt: The number of prefixes belonging to this state.
  • a: For each prefix, the sum of the number of positive smooth suffixes ending at the current digit.
  • z: For each prefix, the sum of the number of suffixes consisting entirely of 0s ending at the current digit.
  • ans: For each prefix, the sum of \(f\) so far.

Transitions

Let the current last digit be last, and the newly appended digit be d.

First, let:

\[ ok = (|last - d| \leq D) \]

Case \(d = 0\)

We append 0 to the end.

A positive smooth suffix can only be extended if the difference between the previous last digit and 0 satisfies the condition.

\[ A' = \begin{cases} A & (ok) \\ 0 & (\text{otherwise}) \end{cases} \]

For suffixes consisting entirely of 0s, we can either append 0 to the existing ones or start a new suffix consisting of only 0.

\[ Z' = Z + cnt \]

Case \(d > 0\)

We append a non-zero digit to the end.

First, the suffix consisting of only d is always smooth.
Also, appending d to the preceding suffixes consisting entirely of 0s yields 0...0d, which is always smooth since its numerical value is \(d\).

There are \(Z + cnt\) such suffixes in total.

Additionally, positive smooth suffixes can be extended if ok is true.

\[ A' = Z + cnt + \begin{cases} A & (ok) \\ 0 & (\text{otherwise}) \end{cases} \]

Since the string ends with a non-zero digit, there are no suffixes consisting entirely of 0s.

\[ Z' = 0 \]

Updating the Answer

The newly added smooth substrings are only those ending at the current digit.
Their count is \(A' + Z'\).

Therefore,

\[ ans' = ans + A' + Z' \]

We add this to the next state dp[ntight][d].

Transitions from the State where the Number Has Not Started

Consider choosing a digit d from emp.

  • If \(d = 0\):
    The number has still not started, so it remains in emp.

  • If \(d > 0\):
    The number starts here.
    Since any 1-digit number is always smooth:

    • cnt += emp
    • a += emp
    • ans += emp

Finally, the sum of all dp[tight][last].ans is the answer.

The ones remaining in emp correspond to the numerical value \(0\), but since the required range is from \(1\) to \(N\), we do not add them.

Complexity

For each digit:

  • tight has \(2\) states.
  • last has \(10\) states.
  • The next digit d to place has at most \(10\) states.

Thus, the overall time complexity is linear.

  • Time Complexity: \(O(|N| \cdot 2 \cdot 10 \cdot 10) = O(|N|)\)
  • Space Complexity: \(O(2 \cdot 10) = O(1)\)

Key Implementation Details

  • To avoid counting leading virtual 0s as substrings, we manage the state where “the number has not started yet” with emp.

  • It is crucial to separate the smooth suffixes ending at the current digit into those with a positive value a and those consisting entirely of 0s z.

  • Since leading 0s of substrings are ignored, 0...0d is treated as the numerical value \(d\). Thus, when \(d > 0\), \(z + cnt\) suffixes newly become smooth.

  • Since the answer can be extremely large, perform all additions modulo \(10^9+7\).

    Source Code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll MOD = 1000000007LL;

struct Node {
    ll cnt = 0;
    ll a = 0;
    ll z = 0;
    ll ans = 0;
};

inline void addmod(ll &x, ll y) {
    x += y;
    if (x >= MOD) x -= MOD;
}

inline ll add2(ll x, ll y) {
    x += y;
    if (x >= MOD) x -= MOD;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string N;
    int D;
    cin >> N >> D;

    array<ll, 2> emp{};
    emp[1] = 1;

    array<array<Node, 10>, 2> dp{};

    for (char ch : N) {
        int ndig = ch - '0';

        array<ll, 2> nemp{};
        array<array<Node, 10>, 2> ndp{};

        for (int tight = 0; tight <= 1; tight++) {
            int lim = tight ? ndig : 9;

            if (emp[tight]) {
                for (int d = 0; d <= lim; d++) {
                    int nt = (tight && d == lim) ? 1 : 0;

                    if (d == 0) {
                        addmod(nemp[nt], emp[tight]);
                    } else {
                        Node &v = ndp[nt][d];
                        addmod(v.cnt, emp[tight]);
                        addmod(v.a, emp[tight]);
                        addmod(v.ans, emp[tight]);
                    }
                }
            }

            for (int last = 0; last <= 9; last++) {
                Node cur = dp[tight][last];
                if (cur.cnt == 0 && cur.a == 0 && cur.z == 0 && cur.ans == 0) continue;

                for (int d = 0; d <= lim; d++) {
                    int nt = (tight && d == lim) ? 1 : 0;
                    bool ok = abs(last - d) <= D;

                    ll na = 0, nz = 0;

                    if (d == 0) {
                        if (ok) na = cur.a;
                        nz = add2(cur.z, cur.cnt);
                    } else {
                        na = add2(cur.z, cur.cnt);
                        if (ok) na = add2(na, cur.a);
                    }

                    ll nans = add2(cur.ans, na);
                    nans = add2(nans, nz);

                    Node &v = ndp[nt][d];
                    addmod(v.cnt, cur.cnt);
                    addmod(v.a, na);
                    addmod(v.z, nz);
                    addmod(v.ans, nans);
                }
            }
        }

        emp = nemp;
        dp = ndp;
    }

    ll ans = 0;
    for (int tight = 0; tight <= 1; tight++) {
        for (int last = 0; last <= 9; last++) {
            addmod(ans, dp[tight][last].ans);
        }
    }

    cout << ans << '\n';
    return 0;
}

This editorial was generated by gpt-5.5-high.