Investigating Numbers with Few Repeated Digits
How many 18-digit numbers n (without leading zeros) are there such that no digit occurs more than three times in n?
Problem Statement
This archive keeps the full statement, math, and original media on the page.
How many \(18\)-digit numbers \(n\) (without leading zeros) are there such that no digit occurs more than three times in \(n\)?
Problem 172: Investigating Numbers with Few Repeated Digits
Mathematical Development
Theorem 1 (Multinomial counting). The number of strings of length over the alphabet in which digit appears exactly times, where , is the multinomial coefficient
Proof. There are total orderings of labelled positions. For each digit , the positions receiving digit are mutually indistinguishable, producing redundant orderings. Dividing by the product of all such redundancies gives the stated formula.
Theorem 2 (Exponential generating function). The total number of length- strings over a 10-symbol alphabet with each symbol appearing at most times is
Proof. The exponential generating function (EGF) for a single symbol restricted to at most occurrences is . Since the ten symbols occupy disjoint subsets of positions, the EGF for the combined structure is (product formula for labelled combinatorial species). The coefficient of the product, multiplied by , recovers the ordinary count.
Lemma 1 (Leading-zero subtraction). Let denote the number of length- strings where digit appears at most times. The count of -digit positive integers (no leading zero) with each digit appearing at most times is
Proof. The strings counted by include those with a leading zero. A string with leading zero has its first position fixed to ; the remaining positions form a string with digit appearing at most additional times and all other digits at most times. The subtraction removes precisely these invalid strings.
Theorem 3 (Sequential digit DP). Define as the number of ways to assign values from the digits processed so far to exactly of the positions. Initialize . When processing digit with maximum frequency , the update rule is
After processing all 10 digits, equals .
Proof. By induction on the number of digits processed. When digit is placed in of the remaining positions, there are ways to choose which positions receive digit . The multiplication principle applied across all 10 digits yields summed over all valid frequency vectors with and . This follows because the telescoping product of binomial coefficients satisfies
Editorial
How many 18-digit numbers n (no leading zero) have each digit appearing at most three times? Method: Sequential digit DP using binomial coefficients.
Pseudocode
dp[0..L] = 0; dp[0] = 1
For i from 0 to 9:
dp'[0..L] = 0
For j from 0 to L:
If dp[j] == 0 then continue
For c from 0 to max_freq[i]:
If j + c > L then stop this loop
dp'[j + c] += dp[j] * C(L - j, c)
dp = dp'
Return dp[L]
T = count_strings(18, [3,3,3,3,3,3,3,3,3,3])
T0 = count_strings(17, [2,3,3,3,3,3,3,3,3,3])
answer = T - T0
Complexity Analysis
- Time: Each call to
count_stringsperforms operations. For : . A second call with adds . Total: . - Space: for the rolling DP array.
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 172: 18-digit numbers where each digit appears at most 3 times.
* Sequential digit DP: for each digit, choose how many positions it fills.
* Answer = T(18, all caps 3) - T(17, digit-0 cap 2, rest cap 3).
*/
long long compute(int L, vector<int>& maxfreq) {
vector<vector<long long>> C(L + 1, vector<long long>(L + 1, 0));
for (int i = 0; i <= L; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
vector<long long> dp(L + 1, 0);
dp[0] = 1;
for (int dig = 0; dig < 10; dig++) {
vector<long long> ndp(L + 1, 0);
for (int j = 0; j <= L; j++) {
if (dp[j] == 0) continue;
for (int c = 0; c <= maxfreq[dig] && j + c <= L; c++)
ndp[j + c] += dp[j] * C[L - j][c];
}
dp = ndp;
}
return dp[L];
}
int main() {
vector<int> mf_all(10, 3);
long long T = compute(18, mf_all);
vector<int> mf_zero(10, 3);
mf_zero[0] = 2;
long long T0 = compute(17, mf_zero);
cout << T - T0 << endl;
return 0;
}
"""
Problem 172: Investigating Numbers with Few Repeated Digits
How many 18-digit numbers n (no leading zero) have each digit appearing
at most three times?
Method: Sequential digit DP using binomial coefficients.
"""
from math import comb
def count_strings(L, max_freq):
"""Count L-length strings over {0,...,9} with digit i at most max_freq[i] times."""
dp = [0] * (L + 1)
dp[0] = 1
for dig in range(10):
ndp = [0] * (L + 1)
for j in range(L + 1):
if dp[j] == 0:
continue
for c in range(max_freq[dig] + 1):
if j + c > L:
break
ndp[j + c] += dp[j] * comb(L - j, c)
dp = ndp
return dp[L]
def solve():
T = count_strings(18, [3] * 10)
mf = [3] * 10
mf[0] = 2
T0 = count_strings(17, mf)
print(T - T0)
if __name__ == "__main__":
solve()