Numbers for Which No Three Consecutive Digits Have a Sum Greater Than a Given Value
How many 20-digit numbers n (without any leading zeros) exist such that no three consecutive digits of n have a sum greater than 9?
Problem Statement
This archive keeps the full statement, math, and original media on the page.
How many \(20\) digit numbers \(n\) (without any leading zero) exist such that no three consecutive digits of \(n\) have a sum greater than \(9\)?
Problem 164: Numbers for Which No Three Consecutive Digits Have a Sum Greater Than a Given Value
Mathematical Foundation
Theorem 1 (Recurrence). Let denote the number of -digit suffixes (allowing leading zeros) such that the first two digits are and , and every three consecutive digits sum to at most 9. Then:
Proof. For , the suffix is exactly the two digits and there are no three-consecutive-digit windows, so .
For , the suffix is where is the third digit. The constraint on the first window requires , i.e., . Since (otherwise no valid suffix exists at all, and the sum is empty), we have . Choosing and continuing from the pair with remaining digits gives the recurrence.
Theorem 2 (Answer formula). The number of 20-digit numbers with no three consecutive digits summing above 9 is:
Proof. The first digit ranges over (no leading zeros). The second digit must satisfy (otherwise the pair cannot begin any valid triple), so . The remaining 18 digits are counted by .
Lemma 1 (State space size). The number of valid states with and is exactly .
Proof. We count pairs with :
Lemma 2 (Matrix exponentiation alternative). Define the transition matrix where rows and columns are indexed by valid states , and if (i.e., ), and 0 otherwise. Then . This allows computation in time.
Proof. Each multiplication by extends the suffix by one digit. After multiplications, we sum over all terminal states.
Editorial
Count 20-digit numbers where no three consecutive digits sum > 9. Dynamic programming with state = (last two digits). We use dynamic programming with a rolling array. We then state: (last_two_digits) = (a, b) with a + b <= 9. Finally, initialize for length 2.
Pseudocode
DP with rolling array
State: (last_two_digits) = (a, b) with a + b <= 9
Initialize for length 2
Extend from length 2 to length 20
Sum over starting digits a >= 1
Complexity Analysis
- Time: where (digit positions), (states), and (digit choices per state). This gives .
- Space: with a rolling array (two layers of DP).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
// Problem 164: No three consecutive digits sum > 9
// DP with state (last two digits)
int main() {
const int DIGITS = 20;
// dp[a][b] = count of valid numbers ending in digits a, b
long long dp[10][10] = {};
// First digit: 1-9, second digit: 0 to 9-first
for (int a = 1; a <= 9; a++)
for (int b = 0; b <= 9 - a; b++)
dp[a][b] = 1;
// Process digits 3 through 20
for (int pos = 3; pos <= DIGITS; pos++) {
long long ndp[10][10] = {};
for (int a = 0; a <= 9; a++) {
for (int b = 0; b <= 9 - a; b++) {
if (dp[a][b] == 0) continue;
int maxc = 9 - a - b;
for (int c = 0; c <= maxc; c++) {
ndp[b][c] += dp[a][b];
}
}
}
memcpy(dp, ndp, sizeof(dp));
}
long long ans = 0;
for (int a = 0; a <= 9; a++)
for (int b = 0; b <= 9; b++)
ans += dp[a][b];
cout << ans << endl;
return 0;
}
"""
Problem 164: Numbers for Which No Three Consecutive Digits Have a Sum Greater Than 9
Count 20-digit numbers where no three consecutive digits sum > 9.
Dynamic programming with state = (last two digits).
"""
def solve():
DIGITS = 20
# dp[a][b] = count of valid numbers ending in digits a, b
dp = [[0]*10 for _ in range(10)]
# Initialize: first two digits
for a in range(1, 10): # no leading zero
for b in range(0, 10 - a):
dp[a][b] = 1
# Fill digits 3 through 20
for pos in range(3, DIGITS + 1):
ndp = [[0]*10 for _ in range(10)]
for a in range(10):
for b in range(10 - a):
if dp[a][b] == 0:
continue
for c in range(10 - a - b):
ndp[b][c] += dp[a][b]
dp = ndp
ans = sum(dp[a][b] for a in range(10) for b in range(10))
print(ans)
solve()