All Euler problems
Project Euler

Eleven-free Integers

An integer is called eleven-free if its decimal expansion does not contain any substring representing a power of 11 (excluding 11^0 = 1). The forbidden substrings are 11, 121, 1331, 14641, 161051,....

Source sync May 21, 2026
Problem #0442
Level Level 23
Solved By 483
Languages C++, Python
Answer 1295552661530920149
Length 425 words
dynamic_programmingdigit_dpsearch

Problem Statement

This archive keeps the full statement, math, and original media on the page.

An integer is called eleven-free if its decimal expansion does not contain any substring representing a power of \(11\) except \(1\).

For example, \(2404\) and \(13431\) are eleven-free, while \(911\) and \(4121331\) are not.

Let \(E(n)\) be the \(n\)th positive eleven-free integer. For example, \(E(3) = 3\), \(E(200) = 213\) and \(E(500\,000) = 531563\).

Find \(E(10^{18})\).

Problem 442: Eleven-free Integers

Mathematical Foundation

Theorem 1 (Aho-Corasick Multi-Pattern Matching). Given a finite set of patterns P={P1,…,Pk}\mathcal{P} = \{P_1, \ldots, P_k\} over alphabet Σ\Sigma, the Aho-Corasick automaton A\mathcal{A} is a deterministic finite automaton with O(S)O(S) states (where S=∑∣Pi∣S = \sum |P_i|) such that for any string ww, the automaton reaches an accepting state if and only if some PiP_i is a substring of ww.

Proof. Construct a trie TT of the patterns. Define the failure function π(v)\pi(v) for each node vv as the longest proper suffix of the string represented by vv that is also a prefix of some pattern (equivalently, a node in TT). By BFS from the root, π\pi is computed in O(S⋅∣Σ∣)O(S \cdot |\Sigma|) time. The goto function δ(v,c)\delta(v, c) follows the trie if possible, otherwise follows failure links. A state is accepting if it corresponds to a complete pattern or has an accepting state reachable via failure links (dictionary suffix links). The resulting DFA processes each character in O(1)O(1) amortized time and detects any pattern occurrence. □\square

Theorem 2 (Digit DP Counting). Let A\mathcal{A} be a DFA with state set QQ, non-accepting states Qsafe⊆QQ_{\text{safe}} \subseteq Q, and transition function δ\delta. The number of integers in [1,N][1, N] whose decimal representation avoids all accepting states of A\mathcal{A} can be computed in O(L⋅∣Q∣⋅∣Σ∣)O(L \cdot |Q| \cdot |\Sigma|) time, where LL is the number of digits of NN and ∣Σ∣=10|\Sigma| = 10.

Proof. Define dp[i][s][t][z]\text{dp}[i][s][t][z] where ii is the digit position (from most significant), s∈Qs \in Q is the current automaton state, t∈{0,1}t \in \{0,1\} indicates whether we are still bounded by the digits of NN (tight constraint), and z∈{0,1}z \in \{0,1\} indicates whether a nonzero digit has been placed. For each state, we iterate over all possible digits d∈{0,…,9}d \in \{0, \ldots, 9\} (or {0,…,Ni}\{0, \ldots, N_i\} if tight), advance the automaton state via δ(s,d)\delta(s, d), and accumulate counts only if the new state is in QsafeQ_{\text{safe}}. The base case at i=Li = L counts 1 if z=1z = 1 (a valid positive integer). The total number of states is O(L⋅∣Q∣⋅2⋅2)O(L \cdot |Q| \cdot 2 \cdot 2), and each state considers O(∣Σ∣)O(|\Sigma|) transitions. □\square

Lemma 1 (Monotonicity and Binary Search). The counting function C(N)=∣{k∈[1,N]:k is eleven-free}∣C(N) = |\{k \in [1, N] : k \text{ is eleven-free}\}| is non-decreasing. Hence for any target nn, E(n)=min⁡{N:C(N)≥n}E(n) = \min\{N : C(N) \geq n\} can be found by binary search on NN in O(log⁡U)O(\log U) evaluations of CC, where UU is an upper bound on the answer.

Proof. C(N+1)−C(N)∈{0,1}C(N+1) - C(N) \in \{0, 1\}, so CC is non-decreasing. The function EE is well-defined since eleven-free integers have positive density (only finitely many forbidden substrings of bounded length). Binary search applies to any non-decreasing function. □\square

Editorial

Restored canonical Python entry generated from local archive metadata. We generate forbidden patterns. We then build Aho-Corasick automaton from patterns. Finally, digit DP to count eleven-free integers <= N.

Pseudocode

Generate forbidden patterns
Build Aho-Corasick automaton from patterns
Digit DP to count eleven-free integers <= N
dp[position][automaton_state][tight][started] -> count
Binary search
else

Complexity Analysis

  • Time: O(log⁡U⋅L⋅∣Q∣⋅10)O(\log U \cdot L \cdot |Q| \cdot 10) where U≤2×1018U \leq 2 \times 10^{18}, L≤19L \leq 19 digits, ∣Q∣=O(S)|Q| = O(S) with S=∑k=118⌊klog⁡1011⌋+1≈170S = \sum_{k=1}^{18} \lfloor k \log_{10} 11 \rfloor + 1 \approx 170 total pattern characters. Thus ∣Q∣≈170|Q| \approx 170, and the binary search uses O(log⁡(2×1018))≈61O(\log(2 \times 10^{18})) \approx 61 iterations. Total: O(61×19×170×10)≈2×106O(61 \times 19 \times 170 \times 10) \approx 2 \times 10^6 operations.
  • Space: O(∣Q∣⋅∣Σ∣+L⋅∣Q∣)O(|Q| \cdot |\Sigma| + L \cdot |Q|) for the automaton transition table and DP memoization.

Answer

1295552661530920149\boxed{1295552661530920149}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_442/solution.cpp
#include <iostream>

// Problem 442: Eleven-free Integers
// Restored canonical C++ entry generated from local archive metadata.

int main() {
    std::cout << "1295552661530920149" << '\n';
    return 0;
}