公式

B - 連番チケットの当選 / Winning of Consecutive Number Tickets 解説 by admin

gpt-5.5-high

Summary

We find the number of integers \(x\) such that the digit sums of three consecutive integers \(x-1, x, x+1\) all leave the same remainder \(R\) when divided by \(K\).

The key point is that if \(K \neq 1\), no such \(x\) exists, and if \(K = 1\), all \(x\) satisfy the condition.

Analysis

Let us consider the change in the digit sum when transitioning from an integer \(n\) to \(n+1\).

For example:

  • If the last digit is not \(9\), like \(123 \to 124\), the digit sum increases by \(1\).
    • \(\mathrm{digitsum}(124) = \mathrm{digitsum}(123) + 1\)
  • If the last digit is \(9\), like \(129 \to 130\), a carry occurs.
    • \(\mathrm{digitsum}(129) = 12\)
    • \(\mathrm{digitsum}(130) = 4\)

Now, consider three consecutive integers \(x-1, x, x+1\).

If \(x-1\) does not end with \(9\), no carry occurs, so:

\[ \mathrm{digitsum}(x) = \mathrm{digitsum}(x-1) + 1 \]

If the remainders of these two digit sums modulo \(K\) are equal, we must have:

\[ 1 \equiv 0 \pmod K \]

This can only hold when \(K=1\).

On the other hand, if \(x-1\) ends with \(9\), then \(x\) must end with \(0\).

Examples:

  • \(19 \to 20\)
  • \(129 \to 130\)
  • \(999 \to 1000\)

That is, since \(x\) does not end with \(9\), no carry occurs in the transition \(x \to x+1\).

Therefore,

\[ \mathrm{digitsum}(x+1) = \mathrm{digitsum}(x) + 1 \]

In this case as well, \(K=1\) is required for the remainders of the two digit sums to be equal.

Thus, when \(K \neq 1\), no such \(x\) exists.

Conversely, when \(K=1\), the remainder of any integer’s digit sum modulo \(1\) is always \(0\).

Since the constraints state \(R=0\), all \(x\) satisfy the condition.

The range of \(x\) is

\[ 2 \leq x \leq N-1 \]

so the count is

\[ N-2 \]

However, \(N\) can be extremely large and may not fit in standard integer types. Therefore, we read it as a string and calculate \(N \bmod (10^9+7)\).

Algorithm

  1. Input \(N, K, R\).
  2. If \(K \neq 1\), the answer is \(0\).
  3. If \(K = 1\), the answer is \(N-2\).
  4. Since \(N\) is given as a string, look at each digit from left to right and calculate \(N \bmod (10^9+7)\) as follows:

\[ \text{nmod} = (\text{nmod} \times 10 + \text{digit}) \bmod (10^9+7) \]

  1. Output

\[ (N-2) \bmod (10^9+7) \]

as the answer.

Complexity

  • Time Complexity: \(O(|N|)\)
  • Space Complexity: \(O(1)\)

where \(|N|\) is the length of the decimal representation of \(N\).

Key Implementation Points

Since \(N\) can have up to \(5 \times 10^6\) digits, it must not be converted to a standard integer type.

Also, when calculating \((N-2) \bmod MOD\), to avoid negative values, compute it as:

\[ (\text{nmod} - 2 + MOD) \bmod MOD \]

Source Code

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

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

    string N;
    int K, R;
    cin >> N >> K >> R;

    const long long MOD = 1000000007LL;

    if (K != 1) {
        cout << 0 << '\n';
        return 0;
    }

    long long nmod = 0;
    for (char c : N) {
        nmod = (nmod * 10 + (c - '0')) % MOD;
    }

    cout << (nmod - 2 + MOD) % MOD << '\n';
    return 0;
}

This editorial was generated by gpt-5.5-high.

投稿日時:
最終更新: