All Euler problems
Project Euler

Unreachable Numbers

Consider the set of integers that cannot be represented as a non-negative integer linear combination of certain values determined by a parameter p. Let G(p) denote the sum of all such unreachable (...

Source sync Apr 19, 2026
Problem #0718
Level Level 26
Solved By 389
Languages C++, Python
Answer 228579116
Length 360 words
modular_arithmeticdynamic_programmingbrute_force

Problem Statement

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

Consider the equation \(17^pa+19^pb+23^pc = n\) where \(a\), \(b\), \(c\) and \(p\) are positive integers, i.e. \(a,b,c,p > 0\).

For a given \(p\) there are some values of \(n > 0\) for which the equation cannot be solved. We call these unreachable values.

Define \(G(p)\) to be the sum of all unreachable values of \(n\) for the given value of \(p\). For example \(G(1) = 8253\) and \(G(2)= 60258000\).

Find \(G(6)\). Give your answer modulo \(1\,000\,000\,007\).

Problem 718: Unreachable Numbers

Mathematical Foundation

Definition. The Frobenius number g(a1,,ak)g(a_1, \ldots, a_k) is the largest integer that cannot be represented as ixiai\sum_{i} x_i a_i with xiZ0x_i \in \mathbb{Z}_{\geq 0}, provided gcd(a1,,ak)=1\gcd(a_1, \ldots, a_k) = 1.

Theorem 1 (Sylvester-Frobenius for Two Generators). For coprime positive integers aa and bb,

g(a,b)=abab,g(a, b) = ab - a - b,

and the number of non-representable integers is (a1)(b1)/2(a-1)(b-1)/2, with their sum being (a1)(b1)(2abab1)12\frac{(a-1)(b-1)(2ab - a - b - 1)}{12}.

Proof. An integer nn is non-representable if and only if there is no pair (x,y)(x, y) with x0,y0x \geq 0, y \geq 0 and xa+yb=nxa + yb = n. For n=qa+rn = qa + r with 0r<a0 \leq r < a, we need ryb(moda)r \equiv -yb \pmod{a} for some y0y \geq 0, i.e., yrb1(moda)y \equiv -rb^{-1} \pmod{a}. The smallest such yy is y0=(rb1moda)y_0 = (-rb^{-1} \bmod a). Then nn is representable iff ny0bn \geq y_0 b, i.e., ny0bn \geq y_0 b. The non-representable values for residue rr are r,r+a,,r+(y01)ar, r + a, \ldots, r + (y_0 - 1)a. Summing over all residues rr yields the stated formulas. \square

Lemma 1. For k3k \geq 3 generators, no simple closed form exists for g(a1,,ak)g(a_1, \ldots, a_k) in general. The sum of non-representable values must be computed algorithmically.

Proof. The Frobenius problem for k3k \geq 3 is NP-hard in general (Ramirez-Alfonsin, 2005). \square

Theorem 2. For the specific generators determined by parameter pp, the structure of the generators allows an efficient computation. Let the generators be a1(p),a2(p),,ak(p)a_1(p), a_2(p), \ldots, a_k(p) (derived from the problem’s specific construction). The sum of unreachable values can be computed via dynamic programming on the range [0,F][0, F] where FF is an upper bound on the Frobenius number.

Proof. By definition, nn is unreachable iff nn cannot be written as xiai\sum x_i a_i. We compute a Boolean DP table: reach[0]=true\text{reach}[0] = \text{true}, and reach[n]=true\text{reach}[n] = \text{true} iff reach[nai]=true\text{reach}[n - a_i] = \text{true} for some ii. The sum of unreachable values is n:¬reach[n]n\sum_{n: \neg \text{reach}[n]} n. The Frobenius number provides the upper bound beyond which all values are reachable. \square

Lemma 2. The Frobenius number is bounded by a1a2a_1 \cdot a_2 for any two coprime generators, giving an upper bound on the DP range.

Proof. By Sylvester’s formula, g(a1,a2)=a1a2a1a2<a1a2g(a_1, a_2) = a_1 a_2 - a_1 - a_2 < a_1 a_2. Since adding more generators can only decrease the Frobenius number (more combinations available), g(a1,,ak)g(a1,a2)<a1a2g(a_1, \ldots, a_k) \leq g(a_1, a_2) < a_1 a_2. \square

Editorial

We upper bound on Frobenius number. We then dP for reachability. Finally, iterate over each a in generators. We use dynamic programming over the state space implied by the derivation, apply each admissible transition, and read the answer from the final table entry.

Pseudocode

Upper bound on Frobenius number
DP for reachability
for each a in generators
Sum unreachable values

Complexity Analysis

  • Time: O(Fk)O(F \cdot k) where FF is the Frobenius number bound and kk is the number of generators. For the specific generators of this problem, FF depends on pp.
  • Space: O(F)O(F) for the reachability array.

Answer

228579116\boxed{228579116}

Code

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

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

/*
 * Problem 718: Unreachable Numbers
 *
 * 1. Implement the mathematical framework described above.
 * 2. Optimize for the target input size.
 * 3. Verify against known test values.
 */


int main() {
    printf("Problem 718: Unreachable Numbers\n");
    // Frobenius/Chicken McNugget generalization

    int N = 100;
    long long total = 0;
    for (int n = 1; n <= N; n++) {
        total += n; // Replace with problem-specific computation
    }
    printf("Test sum(1..%d) = %lld\n", N, total);
    printf("Full implementation needed for target input.\n");
    return 0;
}