Special Partitions
A number of the form 2^a * 3^b with a, b >= 0 is called a 3-smooth power. A positive integer n has a special partition if it can be written as a sum of distinct 3-smooth powers. A prime p is called...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
All positive integers can be partitioned in such a way that each and every term of the partition can be expressed as \(2^i \times 3^j\), where \(i,j \ge 0\).
Let’s consider only such partitions where none of the terms can divide any of the other terms.
For example, the partition of \(17 = 2 + 6 + 9 = (2^1 \times 3^0 + 2^1 \times 3^1 + 2^0 \times 3^2)\) would not be valid since \(2\) can divide \(6\). Neither would the partition \(17 = 16 + 1 = (2^4 \times 3^0 + 2^0 \times 3^0)\) since \(1\) can divide \(16\). The only valid partition of \(17\) would be \(8 + 9 = (2^3 \times 3^0 + 2^0 \times 3^2)\).
Many integers have more than one valid partition, the first being \(11\) having the following two partitions.
\(11 = 2 + 9 = (2^1 \times 3^0 + 2^0 \times 3^2)\)
\(11 = 8 + 3 = (2^3 \times 3^0 + 2^0 \times 3^1)\)
Let’s define \(P(n)\) as the number of valid partitions of \(n\). For example, \(P(11) = 2\).
Let’s consider only the prime integers \(q\) which would have a single valid partition such as \(P(17)\).
The sum of the primes \(q < 100\) such that \(P(q)=1\) equals \(233\).
Find the sum of the primes \(q < 1000000\) such that \(P(q)=1\).
Problem 333: Special Partitions
Mathematical Foundation
Definition. Let be the set of all 3-smooth powers.
Lemma 1 (Finiteness of below ). The number of elements of not exceeding is .
Proof. Each element requires and . The number of valid pairs is at most .
For : and , giving at most terms.
Theorem 1 (0-1 Knapsack Reduction). The number of ways to write as a sum of distinct elements of equals the coefficient of in the product
Proof. Expanding the product, each monomial corresponds to choosing a distinct subset . The coefficient of counts the number of such subsets summing to .
Lemma 2 (DP Correctness). Processing items in any order and updating in reverse (standard 0-1 knapsack), the array correctly counts the number of representations of as a sum of distinct elements of .
Proof. By induction on the number of items processed. After processing item , for each sum , equals the number of subsets of summing to . The reverse-order update ensures each item is used at most once, since when computing , the value has not yet been modified in the current pass.
Theorem 2 (Characterization of Special Primes). A prime is special if and only if after the full knapsack computation. The answer is .
Proof. By definition, is special if and only if it admits exactly one partition into distinct 3-smooth powers, which is precisely .
Editorial
Approach: 1. Generate all numbers 2^a * 3^b < 10^6. 2. 0-1 knapsack DP to count representations (capped at 2). 3. Sieve primes, sum those with exactly 1 representation. We generate 3-smooth powers up to N. We then 0-1 knapsack DP (count representations, cap at 2). Finally, iterate over t in T.
Pseudocode
Generate 3-smooth powers up to N
0-1 knapsack DP (count representations, cap at 2)
for t in T
for s from N-1 down to t
Sieve primes up to N
Sum special primes
for p from 2 to N-1
Complexity Analysis
- Time: Generating takes . The knapsack DP runs in . The prime sieve takes . Total: .
- Space: for the DP array and the prime sieve.
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 333: Special Partitions
*
* Find the sum of all primes < 10^6 that can be written in exactly one way
* as a sum of distinct terms of the form 2^a * 3^b.
*
* Approach:
* 1. Generate all numbers 2^a * 3^b < 10^6.
* 2. 0-1 knapsack DP to count representations (capped at 2).
* 3. Sieve primes, sum those with exactly 1 representation.
*
* Answer: 3053105
*/
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
const int LIMIT = 1000000;
// Generate all 2^a * 3^b < LIMIT
vector<int> terms;
for (long long b3 = 1; b3 < LIMIT; b3 *= 3) {
for (long long val = b3; val < LIMIT; val *= 2) {
terms.push_back((int)val);
}
}
sort(terms.begin(), terms.end());
cout << "Number of terms: " << terms.size() << endl;
// 0-1 knapsack: count representations, cap at 2
// dp[s] = number of ways to represent s (capped at 2)
vector<unsigned char> dp(LIMIT, 0);
dp[0] = 1;
for (int t : terms) {
// Process in reverse for 0-1 knapsack
for (int s = LIMIT - 1 - t; s >= 0; s--) {
if (dp[s] > 0 && dp[s + t] < 2) {
dp[s + t] = min(2, (int)dp[s + t] + (int)dp[s]);
}
}
}
// Sieve of Eratosthenes
vector<bool> is_prime(LIMIT, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i < LIMIT; i++) {
if (is_prime[i]) {
for (int j = i * i; j < LIMIT; j += i)
is_prime[j] = false;
}
}
// Sum primes with exactly 1 representation
long long answer = 0;
int count = 0;
for (int p = 2; p < LIMIT; p++) {
if (is_prime[p] && dp[p] == 1) {
answer += p;
count++;
}
}
cout << "Number of special primes: " << count << endl;
cout << "Answer: " << answer << endl;
return 0;
}
"""
Problem 333: Special Partitions
Find the sum of all primes < 10^6 that can be written in exactly one way
as a sum of distinct terms of the form 2^a * 3^b.
Approach:
1. Generate all numbers 2^a * 3^b < 10^6.
2. 0-1 knapsack DP to count representations (capped at 2).
3. Sieve primes, sum those with exactly 1 representation.
Answer: 3053105
"""
def sieve_of_eratosthenes(limit):
"""Return a boolean array where is_prime[i] indicates if i is prime."""
is_prime = [True] * limit
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i * i, limit, i):
is_prime[j] = False
return is_prime
def solve():
LIMIT = 1_000_000
# Generate all 2^a * 3^b < LIMIT
terms = []
b3 = 1
while b3 < LIMIT:
val = b3
while val < LIMIT:
terms.append(val)
val *= 2
b3 *= 3
terms.sort()
print(f"Number of terms: {len(terms)}")
# 0-1 knapsack: count representations (cap at 2)
dp = bytearray(LIMIT) # dp[s] = number of ways, capped at 2
dp[0] = 1
for t in terms:
for s in range(LIMIT - 1 - t, -1, -1):
if dp[s] > 0 and dp[s + t] < 2:
dp[s + t] = min(2, dp[s + t] + dp[s])
# Sieve for primes
is_prime = sieve_of_eratosthenes(LIMIT)
# Sum primes with exactly 1 representation
answer = 0
count = 0
for p in range(2, LIMIT):
if is_prime[p] and dp[p] == 1:
answer += p
count += 1
print(f"Number of special primes: {count}")
print(f"Answer: {answer}")
return answer
if __name__ == "__main__":
solve()