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 Development
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
The natural state is the last two digits already placed. Once those are known, the next digit is allowed to range only up to , so the rest of the prefix no longer matters. That makes this a small dynamic program on ordered pairs rather than on full 20-digit strings.
The implementation starts by enumerating all valid first-two-digit pairs, respecting the no-leading-zero rule. It then extends the number one digit at a time with a rolling DP table: each state (a, b) pushes its count to all next states (b, c) satisfying . After the twentieth digit has been placed, summing the remaining state counts gives the answer.
Pseudocode
Create a DP table indexed by the last two digits.
Initialize all two-digit prefixes:
the first digit must be between 1 and 9,
the second digit must allow at least one valid continuation,
so only pairs with $a+b \le 9$ receive count 1.
For each remaining digit position from 3 through 20:
build a fresh table.
For every current pair $(a,b)$ with nonzero count:
try every digit $c$ from 0 up to $9-a-b$.
Add the count from $(a,b)$ into the next state $(b,c)$.
Replace the old table with the new one.
Return the sum of all counts in the final table.
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()