All Euler problems
Project Euler

Matchstick Equations

Each digit 0-9 can be formed using matchsticks with the following costs: | Digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:| | Cost | 6 | 2 | 5 | 5 | 4 |...

Source sync Apr 19, 2026
Problem #0882
Level Level 35
Solved By 207
Languages C++, Python
Answer 15800662276
Length 458 words
dynamic_programminggreedymodular_arithmetic

Problem Statement

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

Dr. One and Dr. Zero are playing the following partisan game.

The game begins with one $1$, two $2's$, three $3's, \ldots, n n$'s. Starting with Dr.One, they make moves in turn.

Dr.One chooses a number and changes it by removing a $1$ from its binary expansion.

Dr.Zero chooses a number and changes it by removing a $0$ from its binary expansion.

The player that is unable to move loses.

Note that leading zeros are not allowed in any binary expansion; in particular nobody can make a move on the number $0$.

They soon realize that Dr.Zero can never win the game. In order to make it more interesting, Dr.Zero is allowed to "skip the turn" several time, i.e passing the turn back to Dr. One without making a move.

For example, when $n = $, Dr.Zero can win the game if allowed to skip $2$ turns. A sample game: $$[1,2,2] \xrightarrow{\text{Dr. One}} [1,0,2] \xrightarrow{\text{Dr. Zero}} [1,0,1] \xrightarrow{\text{Dr. One}} [1,0,0] \xrightarrow[\text{Dr. Zero}]{\text{skip}} [1, 0, 0] \xrightarrow{\text{Dr. One}} [1, 0, 0] \xrightarrow[\text{Dr. Zero}]{\text{skip}} [0, 0, 0]$$ Let $S(n)$ be the minimal number of skips needed so that Dr. Zero has a wining strategy. For example, $S(2) = 2$, $S(5) = 17$, $S(10) = 64$.

Find $S(10^5)$.

Problem 882: Matchstick Equations

Mathematical Analysis

Theorem 1 (Maximum Number with mm Matchsticks)

To maximize the numeric value using exactly mm matchsticks, use as many digits as possible. Since digit 1 costs 2 matchsticks (minimum cost), the maximum number of digits is m/2\lfloor m/2 \rfloor.

  • If mm is even: use m/2m/2 ones, giving 111m/2\underbrace{11\cdots1}_{m/2}.
  • If mm is odd: replace one “1” with “7” (cost 3), giving 7111(m3)/27\underbrace{11\cdots1}_{(m-3)/2}.

Theorem 2 (DP Formulation)

Let f(s)f(s) = maximum number formable using exactly ss matchsticks. Then:

f(s)=maxd{0,,9}{10f(sc(d))+d}f(s) = \max_{d \in \{0,\ldots,9\}} \left\{ 10 \cdot f(s - c(d)) + d \right\}

where c(d)c(d) is the matchstick cost of digit dd, with base case f(0)=0f(0) = 0.

Theorem 3 (Counting Valid Equations)

For equations of the form A+B=CA + B = C using exactly mm matchsticks total (including ++ and ==, each costing 2 matchsticks):

The number of valid equations is:

N(m)=sA+sB+sC=m4A,B[A+B=C][cost(A)=sA][cost(B)=sB][cost(C)=sC]N(m) = \sum_{s_A + s_B + s_C = m - 4} \sum_{A, B} [A + B = C] \cdot [\text{cost}(A) = s_A] \cdot [\text{cost}(B) = s_B] \cdot [\text{cost}(C) = s_C]

Concrete Numerical Examples

Maximum Number by Matchstick Count

mmMax numberRepresentation
211
377
41111
57171
6111111
7711711
811111111
101111111111
12111111111111

Digit Efficiency

DigitCostValue/Cost
120.50
732.33
441.00
250.40
350.60
551.00
060.00
661.00
961.50
871.14

Small Equations (A+B=CA + B = C)

With total matchstick budget = 12 (including 4 for + and =):

  • 1+1=21 + 1 = 2: cost =2+2+5+4=13= 2 + 2 + 5 + 4 = 13 (too many)
  • 0+7=70 + 7 = 7: cost =6+3+3+4=16= 6 + 3 + 3 + 4 = 16 (too many)

With budget = 16: 1+1=21 + 1 = 2 costs 2+2+5+4=132+2+5+4=13… We need cost(A) + cost(B) + cost(C) = budget - 4.

DP Solution Details

The dynamic programming approach maintains:

  1. State: number of matchsticks remaining
  2. Transition: try each digit, recurse on remaining sticks
  3. Memoization: cache results by remaining matchstick count

For equations, we enumerate all possible allocations of matchsticks among AA, BB, CC, and verify A+B=CA + B = C.

Complexity Analysis

MethodTimeSpace
Max number with mm sticksO(m)O(m)O(m)O(m)
Count equations, budget mmO(m10m/2)O(m \cdot 10^{m/2}) brutevaries
DP for numbers of cost ssO(m10)O(m \cdot 10)O(m)O(m)

Answer

15800662276\boxed{15800662276}

Code

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

C++ project_euler/problem_882/solution.cpp
/*
 * Problem 882: Matchstick Equations
 * Matchstick costs: 0->6,1->2,2->5,3->5,4->4,5->5,6->6,7->3,8->7,9->6
 * Max number with m sticks: greedy (all 1s, or 7 + 1s for odd m).
 * DP for counting valid equations A + B = C.
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int COST[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int matchstick_cost(int n) {
    if (n == 0) return COST[0];
    int total = 0;
    while (n > 0) { total += COST[n % 10]; n /= 10; }
    return total;
}

string max_number_greedy(int m) {
    if (m < 2) return "";
    if (m % 2 == 0) return string(m / 2, '1');
    return "7" + string((m - 3) / 2, '1');
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cout << "=== Digit Costs ===" << endl;
    for (int d = 0; d <= 9; d++)
        cout << "cost(" << d << ") = " << COST[d] << endl;

    cout << "\n=== Max Number ===" << endl;
    for (int m = 2; m <= 20; m++)
        cout << "m=" << m << ": " << max_number_greedy(m) << endl;

    cout << "\n=== Cost Verification ===" << endl;
    for (int n : {0, 1, 7, 11, 71, 111, 1234}) {
        cout << "cost(" << n << ") = " << matchstick_cost(n) << endl;
    }

    // Count small equations
    cout << "\n=== Equations A+B=C ===" << endl;
    for (int budget = 10; budget <= 30; budget++) {
        int rem = budget - 4;
        int count = 0;
        for (int A = 0; A < 200; A++) {
            int cA = matchstick_cost(A);
            if (cA >= rem) continue;
            for (int B = A; B < 200; B++) {
                int cB = matchstick_cost(B);
                if (cA + cB >= rem) continue;
                int C = A + B;
                int cC = matchstick_cost(C);
                if (cA + cB + cC == rem) count++;
            }
        }
        if (count > 0)
            cout << "budget=" << budget << ": " << count << " equations" << endl;
    }

    return 0;
}