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
The key observation is that we never need the exact number of subsets once that number reaches 2. For each subset size and each possible sum, it is enough to remember whether the sum is unreachable, achievable in exactly one way, or achievable in at least two ways. That turns the problem into a 0/1 knapsack-style dynamic program with a three-state counter instead of an unbounded integer counter.
We process the squares in increasing order and update the table in reverse order of subset size and sum so that each square is used at most once. Whenever a new transition contributes to a state, we clamp the count at 2. After all 100 squares have been processed, the sums with dp[50][s] = 1 are exactly the values in , and adding those sums gives the answer.
Pseudocode
Set squares to [1^2, 2^2, ..., 100^2].
Let K = 50.
Compute S_min = 1^2 + 2^2 + ... + 50^2.
Compute S_max = 51^2 + 52^2 + ... + 100^2.
Create a table dp[0..K][0..S_max], initialized to 0.
Interpret dp[j][s] as:
0 if no j-element subset has sum s,
1 if exactly one j-element subset has sum s,
2 if at least two j-element subsets have sum s.
Set dp[0][0] = 1.
For each square a_i in increasing order:
For subset size j from min(i, K) down to 1:
Restrict the sum loop to the values that are feasible for j - 1 chosen
squares among the first i - 1 terms.
For each such previous sum t, scanned in descending order:
If dp[j - 1][t] is nonzero:
Update dp[j][t + a_i] to min(2, dp[j][t + a_i] + dp[j - 1][t]).
answer = 0
For s from S_min to S_max:
If dp[K][s] = 1:
answer += s
Return answer
Complexity Analysis
- Time: In worst-case form the DP is , with , , and . The implementation prunes each inner loop to the sum interval that is actually feasible at that stage.
- Space: for the three-state table.
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() {
const int N = 100;
const int K = 50;
vector<int> squares(N);
for (int i = 0; i < N; i++) {
squares[i] = (i + 1) * (i + 1);
}
vector<int> prefix(N + 1, 0);
for (int i = 0; i < N; i++) {
prefix[i + 1] = prefix[i] + squares[i];
}
int minSum = prefix[K];
int maxSum = prefix[N] - prefix[N - K];
// dp[j][s] is 0, 1, or 2 depending on whether there are
// zero, one, or at least two j-element subsets with sum s.
vector<vector<uint8_t>> dp(K + 1, vector<uint8_t>(maxSum + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= N; i++) {
int value = squares[i - 1];
int previousTotal = prefix[i - 1];
for (int size = min(i, K); size >= 1; size--) {
int minPrev = prefix[size - 1];
int maxPrev = previousTotal - prefix[i - size];
auto& curr = dp[size];
const auto& prev = dp[size - 1];
for (int prevSum = maxPrev; prevSum >= minPrev; prevSum--) {
if (prev[prevSum]) {
int newSum = prevSum + value;
int total = curr[newSum] + prev[prevSum];
curr[newSum] = static_cast<uint8_t>(min(total, 2));
}
}
}
}
long long answer = 0;
for (int sum = minSum; sum <= maxSum; sum++) {
if (dp[K][sum] == 1) {
answer += sum;
}
}
cout << answer << '\n';
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.
For each subset size we maintain two bitsets:
- once[j]: sums achievable in exactly one way
- many[j]: sums achievable in at least two ways
"""
def solve():
n = 100
k = 50
elements = [i * i for i in range(1, n + 1)]
prefix = [0]
for value in elements:
prefix.append(prefix[-1] + value)
min_sum = prefix[k]
max_sum = prefix[n] - prefix[n - k]
mask = (1 << (max_sum + 1)) - 1
once = [0] * (k + 1)
many = [0] * (k + 1)
once[0] = 1 # Only sum 0 is achievable with 0 elements.
for value in elements:
for size in range(k, 0, -1):
generated_once = (once[size - 1] << value) & mask
generated_many = (many[size - 1] << value) & mask
new_many = many[size] | generated_many | (once[size] & generated_once)
new_once = (once[size] ^ generated_once) & ~new_many
many[size] = new_many
once[size] = new_once & mask
answer = 0
bits = once[k]
for total in range(min_sum, max_sum + 1):
if (bits >> total) & 1:
answer += total
print(answer)
if __name__ == "__main__":
solve()