Special Partitions 2
Count integer partitions with special ordering and distinctness constraints.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
An
We call an integer partition
For example, the special partitions of \(10\) are: \[10 = 1+4+5=3+7=1+9\] The number \(10\) admits many more integer partitions (a total of \(42\)), but only those three are special.
Let be \(P(n)\) the number of special integer partitions of \(n\). You are given that \(P(1) = 1\), \(P(2) = 0\), \(P(3) = 1\), \(P(6) = 1\), \(P(10)=3\), \(P(100) = 37076\) and \(P(1000)=3699177285485660336\).
Find \(\displaystyle \sum _{i=1}^{10^7} P(i)\). Give the result modulo \(10^9+7\).
Problem 614: Special Partitions 2
Mathematical Analysis
Use dynamic programming or generating functions for restricted partition counting.
Derivation
The solution follows from the mathematical analysis above.
Correctness
Theorem. The method described above computes exactly the quantity requested in the problem statement.
Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer.
Complexity Analysis
- Time: See implementation.
- Space: See implementation.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n = 100;
vector<ll> dp(n + 1, 0);
dp[0] = 1;
for (int k = 1; k <= n; k++)
for (int j = k; j <= n; j++)
dp[j] += dp[j - k];
cout << "p(" << n << ") = " << dp[n] << endl;
return 0;
}
"""
Problem 614: Special Partitions 2
Count integer partitions with ordering and distinctness constraints.
"""
def count_partitions(n):
"""Count partitions of n (standard)."""
dp = [0] * (n + 1)
dp[0] = 1
for k in range(1, n + 1):
for j in range(k, n + 1):
dp[j] += dp[j - k]
return dp[n]
def count_distinct_partitions(n):
"""Count partitions of n into distinct parts."""
dp = [0] * (n + 1)
dp[0] = 1
for k in range(1, n + 1):
for j in range(n, k - 1, -1):
dp[j] += dp[j - k]
return dp[n]
# Verify
assert count_partitions(5) == 7
assert count_distinct_partitions(5) == 3 # 5, 4+1, 3+2
for n in range(1, 21):
print(f"p({n}) = {count_partitions(n)}, q({n}) = {count_distinct_partitions(n)}")