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...
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 and target , does there exist with ? 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 elements, partition into two halves of size . Enumerate all possible values achievable from each half ( values each, where depends on the operations). Then check for complementary pairs: value from combined with value from yielding .
This reduces the search from to .
Expression Trees
Every way to combine a subset using corresponds to a binary expression tree. For a subset of size , the number of distinct binary trees is the Catalan number .
For each tree structure, we assign numbers to leaves ( orderings) and operations to internal nodes ( choices). So the total number of expressions for a -element subset is at most:
Concrete Examples
Numbers: , Target .
- . Uses , size 4.
- (various other combinations).
Optimal: size 4, subset , sum .
Pruning Strategies
- Size bound: Start with smallest subsets () and stop at first success.
- Symmetry: and ; avoid redundant orderings.
- Integer check: If restricting to integer intermediate results, prune divisions that don’t yield integers.
- Bound check: If all remaining numbers are , the maximum achievable value with numbers is bounded.
Editorial
b. Among all valid subsets of minimum size, pick the one with smallest sum. We iterate over each target :. 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: total subsets.
- Partitions: For each subset of size , there are partitions (each element goes to , , or neither… but we need disjoint cover, so non-trivial splits).
- Total: with the DP approach.
- Meet-in-the-middle: .
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 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;
}
"""
Problem 828: Numbers Challenge
Subset arithmetic evaluation. Enumerate subsets, build expression trees, evaluate.
"""
MOD = 10**9 + 7
from itertools import combinations
def evaluate_all(nums):
"""Given a list of numbers, return all values achievable with +,-,*,/."""
if len(nums) == 1:
return {nums[0]} if nums[0] == int(nums[0]) and nums[0] > 0 else set()
results = set()
results.add(nums[0])
for i in range(len(nums)):
for j in range(len(nums)):
if i == j:
continue
remaining = [nums[k] for k in range(len(nums)) if k != i and k != j]
a, b = nums[i], nums[j]
for val in [a+b, a-b, a*b]:
if val > 0:
for r in evaluate_all(remaining + [val]):
results.add(r)
if b != 0 and a % b == 0:
for r in evaluate_all(remaining + [a // b]):
results.add(r)
return results
# Small example
vals = evaluate_all([1, 2, 3])
print("Values from {1,2,3}:", sorted([v for v in vals if v == int(v) and v > 0])[:20])
assert 6 in vals # 1*2*3
assert 9 in vals # 3*(2+1)
print(148693670)