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...
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 with and . For integers and , define the subset count function
The problem asks for .
Theorem 1 (Sum Bounds). The sum of any 50-element subset of lies in the interval where
Proof. The minimum is attained uniquely by and the maximum uniquely by . Applying the identity yields . The total , so .
Lemma 1 (Recurrence). The subset count function satisfies the recurrence
for , with the convention and otherwise. Here denotes the count restricted to subsets of .
Proof. The -element subsets of summing to are partitioned into those not containing (counted by ) and those containing (counted by ).
Theorem 2 (Three-State DP Sufficiency). Define the clamped count . Then satisfies
and correctly determines whether is , , or .
Proof. We show by induction on that for all .
Base case (): , and all other entries are .
Inductive step: Assume for all . Let and , so . We must show .
- If : then , so and , giving .
- If : then , so the clamp yields .
Corollary. The set is precisely the set of sums achieved by exactly one 50-element subset.
Editorial
Remark.* The reverse iteration over and in step 3 ensures that each element 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 iterations. With , , and , this gives constant-time operations (addition, comparison, and clamp).
- Space: The DP table has entries. Each entry is in , representable in 2 bits; using one byte per entry requires approximately 15 MB.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 201: Subsets with a Unique Sum
For S = {1^2, 2^2, ..., 100^2}, find the sum of all integers that are the
sum of exactly one subset of S of size 50.
We use a 3-state DP: 0 = unreachable, 1 = exactly one subset, 2 = two or more.
"""
def solve():
N = 100
K = 50
elements = [(i+1)**2 for i in range(N)]
max_sum = sum(elements[K:]) # sum of 51^2..100^2 = 295425
# dp[j][s] in {0, 1, 2}
# Use a list of bytearray for efficiency
dp = [bytearray(max_sum + 1) for _ in range(K + 1)]
dp[0][0] = 1
for i, ai in enumerate(elements):
# Iterate j in reverse
for j in range(min(i + 1, K), 0, -1):
for s in range(max_sum, ai - 1, -1):
if dp[j-1][s - ai]:
val = dp[j][s] + dp[j-1][s - ai]
dp[j][s] = min(val, 2)
ans = sum(s for s in range(max_sum + 1) if dp[K][s] == 1)
print(ans)
if __name__ == "__main__":
solve()