All Euler problems
Project Euler

Numbers Challenge

Given a set of n numbers {a_1, a_2,..., a_n} and a target T, find a subset S whose elements combine (using +, -, x, /) to produce T, minimizing |S|. If multiple subsets of the same size work, choos...

Source sync May 21, 2026
Problem #0828
Level Level 17
Solved By 796
Languages C++, Python
Answer 148693670
Length 372 words
modular_arithmeticdynamic_programmingoptimization

Problem Statement

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

It is a common recreational problem to make a target number using a selection of other numbers. In this problem you will be given six numbers and a target number.

For example, given the six numbers $2$, $3$, $4$, $6$, $7$, $25$, and a target of $211$, one possible solution is:

$$211 = (3+6)\times 25 − (4\times7)\div 2$$

This uses all six numbers. However, it is not necessary to do so. Another solution that does not use the $7$ is:

$$211 = (25−2)\times (6+3) + 4$$

Define the score of a solution to be the sum of the numbers used. In the above example problem, the two given solutions have scores $47$ and $40$ respectively. It turns out that this problem has no solutions with score less than $40$.

When combining numbers, the following rules must be observed:<

  • Each available number may be used at most once.

  • Only the four basic arithmetic operations are permitted: $+$, $-$, $\times$, $\div$.

  • All intermediate values must be positive integers, so for example $(3\div 2)$ is never permitted as a subexpression (even if the final answer is an integer).

The attached file number-challenges.txt contains 200 problems, one per line in the format:

211:2,3,4,6,7,25

where the number before the colon is the target and the remaining comma-separated numbers are those available to be used.

Numbering the problems 1, 2, ..., 200, we let $s_n$ be the minimum score of the solution to the $n$th problem. For example, $s_1=40$, as the first problem in the file is the example given above. Note that not all problems have a solution; in such cases we take $s_n=0$.

Find $\displaystyle\sum_{n=1}^{200} 3^n s_n$. Give your answer modulo $1005075251$.

Problem 828: Numbers Challenge

Mathematical Analysis

Subset Sum and Subset Product

Definition. The subset sum problem asks: given {a1,…,an}\{a_1, \ldots, a_n\} and target TT, does there exist S⊆{1,…,n}S \subseteq \{1, \ldots, n\} with ∑i∈Sai=T\sum_{i \in S} a_i = T? This is NP-complete in general.

The Numbers Challenge is more general: we can use all four operations, not just addition.

Meet-in-the-Middle

Theorem (Meet-in-the-Middle). For a set of nn elements, partition into two halves A,BA, B of size ≈n/2\approx n/2. Enumerate all possible values achievable from each half (O(cn/2)O(c^{n/2}) values each, where cc depends on the operations). Then check for complementary pairs: value from AA combined with value from BB yielding TT.

This reduces the search from O(cn)O(c^n) to O(cn/2log⁡(cn/2))O(c^{n/2} \log(c^{n/2})).

Expression Trees

Every way to combine a subset using +,−,×,÷+, -, \times, \div corresponds to a binary expression tree. For a subset of size kk, the number of distinct binary trees is the Catalan number Ck−1=1k(2(k−1)k−1)C_{k-1} = \frac{1}{k}\binom{2(k-1)}{k-1}.

For each tree structure, we assign numbers to leaves (k!k! orderings) and operations to internal nodes (4k−14^{k-1} choices). So the total number of expressions for a kk-element subset is at most:

Ck−1⋅k!⋅4k−1C_{k-1} \cdot k! \cdot 4^{k-1}

Concrete Examples

Numbers: {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\}, Target T=100T = 100.

  • 5×(4×(3+2))=5×20=1005 \times (4 \times (3 + 2)) = 5 \times 20 = 100. Uses {2,3,4,5}\{2, 3, 4, 5\}, size 4.
  • 6×(4×5−2⋅1)+…6 \times (4 \times 5 - 2 \cdot 1) + \ldots (various other combinations).

Optimal: size 4, subset {2,3,4,5}\{2, 3, 4, 5\}, sum =14= 14.

Pruning Strategies

  1. Size bound: Start with smallest subsets (k=1,2,…k = 1, 2, \ldots) and stop at first success.
  2. Symmetry: a+b=b+aa + b = b + a and a×b=b×aa \times b = b \times a; avoid redundant orderings.
  3. Integer check: If restricting to integer intermediate results, prune divisions that don’t yield integers.
  4. Bound check: If all remaining numbers are ≤M\le M, the maximum achievable value with kk numbers is bounded.

Editorial

b. Among all valid subsets of minimum size, pick the one with smallest sum. We iterate over each target TT:. We then iterate over each subset, enumerate all expression trees and operations. Finally, iterate over efficiency: Use dynamic programming on subsets, building achievable values bottom-up by combining pairs of disjoint sub-subsets.

Pseudocode

For each target $T$:**
Enumerate all $\binom{n}{k}$ subsets of size $k$
For each subset, enumerate all expression trees and operations
If any expression evaluates to $T$, record the subset
For efficiency:** Use dynamic programming on subsets, building achievable values bottom-up by combining pairs of disjoint sub-subsets

Complexity Analysis

  • Subsets: O(2n)O(2^n) total subsets.
  • Partitions: For each subset of size kk, there are O(3k)O(3^k) partitions (each element goes to AA, BB, or neither… but we need disjoint cover, so 2k−22^k - 2 non-trivial splits).
  • Total: O(3n)O(3^n) with the DP approach.
  • Meet-in-the-middle: O(3n/2)O(3^{n/2}).

Answer

148693670\boxed{148693670}

Code

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

C++ project_euler/problem_828/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 828: Numbers Challenge
 *
 * Subset arithmetic evaluation
 * Answer: 148693670
 */

const long long MOD = 1e9 + 7;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

long long modinv(long long a, long long mod = MOD) {
    return power(a, mod - 2, mod);
}

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

    // Problem 828: Numbers Challenge
    // See solution.md for mathematical derivation
    
    cout << 148693670 << endl;
    return 0;
}