B - 連番チケットの当選 / Winning of Consecutive Number Tickets Editorial by admin
or-glm5.2-highOverview
This problem asks us to find the number of integers \(x\) such that the sum of digits of three consecutive integers \(x-1, x, x+1\) modulo \(K\) are all equal to \(R\).
Analysis
First, let’s focus on the change in the sum of digits of consecutive integers. Let \(D(n)\) be the sum of digits of an integer \(n\). When we add 1 to \(n\), how does the sum of digits change? If the last digit (ones place) of \(n\) is not 9, no carry-over occurs, and the last digit simply increases by 1. Thus, \(D(n+1) = D(n) + 1\). On the other hand, if the last digit of \(n\) is 9, a carry-over occurs. The last digit changes from 9 to 0 (\(-9\)), and the next higher digit increases by 1 (\(+1\)), resulting in a net change of \(-8\) (or \(-9k + 1\) if there are \(k\) consecutive 9s at the end).
Based on this property, we look for \(x\) that satisfies \(D(x-1) \equiv D(x) \equiv D(x+1) \pmod K\). Let \(d\) be the last digit of \(x\). We can consider the following cases:
When \(d\) is between \(1\) and \(8\) inclusive No carry-over or borrow occurs in the transition from \(x-1\) to \(x\), nor from \(x\) to \(x+1\). Therefore, \(D(x) - D(x-1) = 1\) and \(D(x+1) - D(x) = 1\). For these to be multiples of \(K\), we must have \(K = 1\).
When \(d = 9\) A carry-over occurs in the transition from \(x\) to \(x+1\), but no carry-over occurs from \(x-1\) to \(x\) because the last digit changes from 8 to 9. Thus, \(D(x) - D(x-1) = 1\). For this to be a multiple of \(K\), we must have \(K = 1\).
When \(d = 0\) A borrow (carry-down) occurs in the transition from \(x-1\) to \(x\), but no carry-over occurs from \(x\) to \(x+1 because the last digit changes from 0 to 1. Thus, \)D(x+1) - D(x) = 1\(. For this to be a multiple of \)K\(, we must have \)K = 1$.
In all cases, when \(K \geq 2\), there is always at least one adjacent pair whose difference in the sum of digits is \(\pm 1\), which can never be congruent modulo \(K\). Therefore, no such \(x\) exists when \(K \geq 2\).
When \(K = 1\), we have \(R = 0\) from the constraints. Since the sum of digits of any integer modulo \(1\) is always \(0\), all integers \(x\) satisfying the condition \(2 \leq x \leq N-1\) satisfy the condition. The number of such integers is \(N - 2\).
Algorithm
- Read the input. If \(K\) is not \(1\), print
0and terminate. - If \(K = 1\), calculate \(N - 2 \pmod{10^9+7}\).
- Since \(N\) is given as a string representing a very large integer (up to \(5 \times 10^6\) digits), we read it character by character while computing its remainder modulo \(10^9+7\).
- Subtract \(2\) from the calculated remainder, add \(10^9+7\) to avoid negative values, and print the result modulo \(10^9+7\).
Complexity
- Time Complexity: \(O(L)\) (where \(L\) is the number of digits in \(N\))
- Space Complexity: \(O(L)\) (required to store the input string)
Implementation Points
Since \(N\) is extremely large, it cannot be directly converted to a standard numeric type. It must be processed as a string, taking the modulo digit by digit.
After finding \(N \bmod (10^9+7)\), subtracting \(2\) might result in a negative value. Thus, we should adjust it like
(n_mod - 2 + MOD) % MOD.Source Code
#include <iostream>
#include <string>
using namespace std;
const long long MOD = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string N;
long long K, R;
if (!(cin >> N >> K >> R)) return 0;
if (K != 1) {
cout << 0 << "\n";
return 0;
}
long long n_mod = 0;
for (char c : N) {
n_mod = (n_mod * 10 + (c - '0')) % MOD;
}
long long ans = (n_mod - 2 + MOD) % MOD;
cout << ans << "\n";
return 0;
}
This editorial was generated by or-glm5.2-high.
posted:
last update: