All Euler problems
Project Euler

Subsets with a Unique Sum

For any set A of numbers, let sum(A) denote the sum of the elements of A. Consider the set S = {1^2, 2^2, 3^2,..., 100^2}. Let U be the set of all integers v such that exactly one 50-element subset...

Source sync Apr 19, 2026
Problem #0201
Level Level 09
Solved By 2,752
Languages C++, Python
Answer 115039000
Length 295 words
dynamic_programminglinear_algebrasequence

Problem Statement

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

For any set \(A\) of numbers, let \(\operatorname {sum}(A)\) be the sum of the elements of \(A\).

Consider the set \(B = \{1,3,6,8,10,11\}\).

There are \(20\) subsets of \(B\) containing three elements, and their sums are: \begin {align*} \operatorname {sum}(\{1,3,6\}) &= 10,\\ \operatorname {sum}(\{1,3,8\}) &= 12,\\ \operatorname {sum}(\{1,3,10\}) &= 14,\\ \operatorname {sum}(\{1,3,11\}) &= 15,\\ \operatorname {sum}(\{1,6,8\}) &= 15,\\ \operatorname {sum}(\{1,6,10\}) &= 17,\\ \operatorname {sum}(\{1,6,11\}) &= 18,\\ \operatorname {sum}(\{1,8,10\}) &= 19,\\ \operatorname {sum}(\{1,8,11\}) &= 20,\\ \operatorname {sum}(\{1,10,11\}) &= 22,\\ \operatorname {sum}(\{3,6,8\}) &= 17,\\ \operatorname {sum}(\{3,6,10\}) &= 19,\\ \operatorname {sum}(\{3,6,11\}) &= 20,\\ \operatorname {sum}(\{3,8,10\}) &= 21,\\ \operatorname {sum}(\{3,8,11\}) &= 22,\\ \operatorname {sum}(\{3,10,11\}) &= 24,\\ \operatorname {sum}(\{6,8,10\}) &= 24,\\ \operatorname {sum}(\{6,8,11\}) &= 25,\\ \operatorname {sum}(\{6,10,11\}) &= 27,\\ \operatorname {sum}(\{8,10,11\}) &= 29. \end {align*}

Some of these sums occur more than once, others are unique.

For a set \(A\), let \(U(A,k)\) be the set of unique sums of \(k\)-element subsets of \(A\), in our example we find \(U(B,3) = \{10,12,14,18,21,25,27,29\}\) and \(\operatorname {sum}(U(B,3)) = 156\).

Now consider the \(100\)-element set \(S = \{1^2, 2^2, \dots , 100^2\}\).

S has \(100891344545564193334812497256\) \(50\)-element subsets.

Determine the sum of all integers which are the sum of exactly one of the \(50\)-element subsets of \(S\), i.e. find \(\operatorname {sum}(U(S,50))\).

Problem 201: Subsets with a Unique Sum

Mathematical Development

Definition 1. Let S={a1,a2,,an}S = \{a_1, a_2, \ldots, a_n\} with ai=i2a_i = i^2 and n=100n = 100. For integers 0jn0 \leq j \leq n and s0s \geq 0, define the subset count function

c(j,s)={TS:T=j and sum(T)=s}.c(j, s) = \bigl|\bigl\{T \subseteq S : |T| = j \text{ and } \operatorname{sum}(T) = s\bigr\}\bigr|.

The problem asks for v:c(50,v)=1v\sum_{v : c(50, v) = 1} v.

Theorem 1 (Sum Bounds). The sum of any 50-element subset of SS lies in the interval [Smin,Smax][S_{\min}, S_{\max}] where

Smin=k=150k2=42925,Smax=k=51100k2=295425.S_{\min} = \sum_{k=1}^{50} k^2 = 42925, \qquad S_{\max} = \sum_{k=51}^{100} k^2 = 295425.

Proof. The minimum is attained uniquely by {12,22,,502}\{1^2, 2^2, \ldots, 50^2\} and the maximum uniquely by {512,522,,1002}\{51^2, 52^2, \ldots, 100^2\}. Applying the identity k=1mk2=m(m+1)(2m+1)/6\sum_{k=1}^{m} k^2 = m(m+1)(2m+1)/6 yields Smin=5051101/6=42925S_{\min} = 50 \cdot 51 \cdot 101 / 6 = 42925. The total k=1100k2=100101201/6=338350\sum_{k=1}^{100} k^2 = 100 \cdot 101 \cdot 201 / 6 = 338350, so Smax=33835042925=295425S_{\max} = 338350 - 42925 = 295425. \square

Lemma 1 (Recurrence). The subset count function satisfies the recurrence

ci(j,s)=ci1(j,s)+ci1(j1,sai)c_i(j, s) = c_{i-1}(j, s) + c_{i-1}(j-1, s - a_i)

for i=1,,ni = 1, \ldots, n, with the convention c0(0,0)=1c_0(0, 0) = 1 and c0(j,s)=0c_0(j, s) = 0 otherwise. Here ci(j,s)c_i(j, s) denotes the count restricted to subsets of {a1,,ai}\{a_1, \ldots, a_i\}.

Proof. The jj-element subsets of {a1,,ai}\{a_1, \ldots, a_i\} summing to ss are partitioned into those not containing aia_i (counted by ci1(j,s)c_{i-1}(j, s)) and those containing aia_i (counted by ci1(j1,sai)c_{i-1}(j-1, s - a_i)). \square

Theorem 2 (Three-State DP Sufficiency). Define the clamped count c^i(j,s)=min(ci(j,s),2)\hat{c}_i(j, s) = \min(c_i(j, s), 2). Then c^i\hat{c}_i satisfies

c^i(j,s)=min(c^i1(j,s)+c^i1(j1,sai),  2)\hat{c}_i(j, s) = \min\bigl(\hat{c}_{i-1}(j, s) + \hat{c}_{i-1}(j-1, s - a_i),\; 2\bigr)

and correctly determines whether cn(j,s)c_n(j, s) is 00, 11, or 2\geq 2.

Proof. We show by induction on ii that c^i(j,s)=min(ci(j,s),2)\hat{c}_i(j, s) = \min(c_i(j, s), 2) for all j,sj, s.

Base case (i=0i = 0): c^0(0,0)=min(1,2)=1=c0(0,0)\hat{c}_0(0, 0) = \min(1, 2) = 1 = c_0(0, 0), and all other entries are 00.

Inductive step: Assume c^i1(j,s)=min(ci1(j,s),2)\hat{c}_{i-1}(j, s) = \min(c_{i-1}(j, s), 2) for all j,sj, s. Let u=ci1(j,s)u = c_{i-1}(j, s) and w=ci1(j1,sai)w = c_{i-1}(j-1, s-a_i), so ci(j,s)=u+wc_i(j,s) = u + w. We must show min(min(u,2)+min(w,2),2)=min(u+w,2)\min(\min(u,2) + \min(w,2), 2) = \min(u + w, 2).

  • If u+w1u + w \leq 1: then u,w1u, w \leq 1, so min(u,2)=u\min(u,2) = u and min(w,2)=w\min(w,2) = w, giving min(u+w,2)=u+w\min(u + w, 2) = u + w.
  • If u+w2u + w \geq 2: then min(u,2)+min(w,2)min(u+w,2+2)2\min(u,2) + \min(w,2) \geq \min(u + w, 2 + 2) \geq 2, so the clamp yields 22. \square

Corollary. The set U={v:c^n(50,v)=1}U = \{v : \hat{c}_n(50, v) = 1\} is precisely the set of sums achieved by exactly one 50-element subset.

Editorial

Remark.* The reverse iteration over jj and ss in step 3 ensures that each element aia_i is used at most once per subset (the standard 0/1 knapsack trick). We return sum of all s in [S_min, S_max] with dp[k][s] == 1.

Pseudocode

Input: S = [1, 4, 9, ..., 10000], n = 100, k = 50
Output: sum of all v with exactly one k-element subset of S summing to v
Compute S_max = 295425
Initialize dp[0..k][0..S_max] = 0; set dp[0][0] = 1
For i = 1 to n:
Return sum of all s in [S_min, S_max] with dp[k][s] == 1

Complexity Analysis

  • Time: The triple loop executes O(nkSmax)O(n \cdot k \cdot S_{\max}) iterations. With n=100n = 100, k=50k = 50, and Smax=295425S_{\max} = 295425, this gives 1.48×109\leq 1.48 \times 10^9 constant-time operations (addition, comparison, and clamp).
  • Space: The DP table has (k+1)(Smax+1)1.51×107(k+1)(S_{\max}+1) \approx 1.51 \times 10^7 entries. Each entry is in {0,1,2}\{0, 1, 2\}, representable in 2 bits; using one byte per entry requires approximately 15 MB.

Answer

115039000\boxed{115039000}

Code

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

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

int main(){
    // S = {1^2, 2^2, ..., 100^2}, find sum of all v such that exactly one
    // 50-element subset of S sums to v.

    const int N = 100, K = 50;
    // Compute elements
    vector<int> a(N);
    for(int i = 0; i < N; i++) a[i] = (i+1)*(i+1);

    int total = 0;
    for(int x : a) total += x; // 338350
    int minsum = 0;
    for(int i = 0; i < K; i++) minsum += a[i]; // 42925
    int maxsum = total - minsum; // 295425

    // dp[j][s] in {0,1,2} meaning 0/1/2+ subsets of size j summing to s
    // Use bytes to save memory. We iterate elements and update.
    // dp dimensions: (K+1) x (maxsum+1)

    vector<vector<uint8_t>> dp(K+1, vector<uint8_t>(maxsum+1, 0));
    dp[0][0] = 1;

    for(int i = 0; i < N; i++){
        int ai = a[i];
        // Iterate j in reverse to avoid reuse
        for(int j = min(i+1, K); j >= 1; j--){
            for(int s = maxsum; s >= ai; s--){
                if(dp[j-1][s-ai]){
                    int val = dp[j][s] + dp[j-1][s-ai];
                    if(val > 2) val = 2;
                    dp[j][s] = val;
                }
            }
        }
    }

    long long ans = 0;
    for(int s = 0; s <= maxsum; s++){
        if(dp[K][s] == 1) ans += s;
    }

    cout << ans << endl;
    return 0;
}