Partitions: a representation of a natural number as a sum of positive integers in non-increasing order.
Each positive integer in the sum is called a part.
Partition numbers: , the number of ways to partition the natural number .
Partition numbers starting from :
| n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 5 | 7 | 11 | 15 | 22 |
Partitions into k Parts
A partition of into exactly parts is called a partition into parts, denoted by .
Clearly, the number of partitions into parts, , is also the number of solutions of the following equation:
If exactly parts in this equation are nonzero, then there are exactly solutions. Therefore,
Subtracting two adjacent sums gives
If we lay out a table, the number in each cell equals the number in the upper-left cell plus the numbers in the column-number-many cells above that cell.
| k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 2 | 1 | 1 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 2 | 2 | 1 | 1 | 0 | 0 | 0 | |
| 0 | 1 | 3 | 3 | 2 | 1 | 1 | 0 | 0 | |
| 0 | 1 | 3 | 4 | 3 | 2 | 1 | 1 | 0 | |
| 0 | 1 | 4 | 5 | 5 | 3 | 2 | 1 | 1 |
Example
Computing the Number of Partitions into k Parts
Compute the number of partitions into parts, . There are multiple test cases; is at most , is at most , and all answers are taken modulo .
Looking at the table and recurrence, updating by column is more favorable for storage. The program is straightforward:
#include <cstdio>
#include <cstring>
int p[10005][1005]; /*Number of ways to partition the natural number n into k parts*/
int main() {
int n, k;
while (~scanf("%d%d", &n, &k)) {
memset(p, 0, sizeof(p));
p[0][0] = 1;
int i;
for (i = 1; i <= n; ++i) {
int j;
for (j = 1; j <= k; ++j) {
if (i - j >= 0) /*All parts of p[i-j][j] are greater than 1*/
{
p[i][j] = (p[i - j][j] + p[i - 1][j - 1]) %
1000007; /*p[i-1][j-1] has at least one part equal to 1.*/
}
}
}
printf("%d\n", p[n][k]);
}
}
Generating Function
By the formula for the sum of a geometric progression,
For partitions into parts, the generating function is slightly more complicated. Explicitly,
Ferrers Diagram
A Ferrers diagram represents each part of a partition as a row of dots. The number of dots in each row is the size of that part.
By the definition of a partition, the different rows in a Ferrers diagram are arranged in decreasing order. The longest row is at the top.
For example, the Ferrers diagram of the partition is as follows.

Flipping a Ferrers diagram along its diagonal produces a new Ferrers diagram called the conjugate of the original diagram, and the new partition is called the conjugate of the original partition. Clearly, conjugation is a symmetric relation.
For example, the conjugate of the partition above is the partition .
The number of partitions with largest part is the number of partitions of the natural number whose largest part is .
By the definition of conjugation, we immediately obtain:
The number of partitions with largest part is equal to the number of partitions into parts; both are .
Partitions into Distinct Parts
The number of partitions into distinct parts: , the number of ways to partition the natural number so that all parts are distinct. (Different)
| n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2 | 3 | 4 | 5 | 6 |
Similarly, define the number of partitions into distinct parts, , as the number of distinct partitions into exactly parts; it is the number of solutions of
Equivalently, it is the number of solutions of
The difference from the discussion above is that, because the parts are distinct, the new equation has at most one zero part. The same invariant conclusion holds: if exactly parts are not , then there are exactly solutions, where can only be or . Thus we directly obtain the recurrence
As with binomial coefficients, we can lay out a table. The number in each cell equals the numbers in the column-number-many cells above the cell in the preceding column, plus the numbers in the column-number-many cells above the cell.
| k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 3 | 1 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 3 | 2 | 0 | 0 | 0 | 0 | 0 |
Example
Computing the Number of Partitions into Distinct Parts
Compute the number of partitions into distinct parts, . There are multiple test cases; is at most , and all answers are taken modulo .
Looking at the table and recurrence, updating by column is more favorable for storage. The code saves space by reducing the latter dimension to two, retaining only adjacent terms.
#include <cstdio>
#include <cstring>
int pd[50005][2]; /*Number of distinct ways to partition the natural number n into k parts*/
int main() {
int n;
while (~scanf("%d", &n)) {
memset(pd, 0, sizeof(pd));
pd[0][0] = 1;
int ans = 0;
int j;
for (j = 1; j < 350; ++j) {
int i;
for (i = 0; i < 350; ++i) {
pd[i][j & 1] = 0; /*pd[i][j] depends only on pd[][j] and pd[][j-1]*/
}
for (i = 0; i <= n; ++i) {
if (i - j >= 0) /*All parts of pd[i-j][j] are greater than 1*/
{
pd[i][j & 1] = (pd[i - j][j & 1] + pd[i - j][(j - 1) & 1]) %
1000007; /*pd[i-j][j-1] has at least one part equal to 1.*/
}
}
ans = (ans + pd[n][j & 1]) % 1000007;
}
printf("%d\n", ans);
}
}
Partitions into Odd Parts
The number of partitions into odd parts: , the number of ways to partition the natural number so that every part is odd. (Odd)
There is an obvious identity:
The leftmost expression is the generating function for partitions into distinct parts, and the rightmost expression is the generating function for partitions into odd parts. Their corresponding coefficients are equal, so the numbers of partitions into odd parts and into distinct parts are equal:
However, the number of partitions into odd parts and the number of partitions into distinct parts are clearly different concepts, so we omit the former here.
We introduce two more concepts:
The number of partitions into distinct even parts: , the number of distinct partitions of the natural number with an even number of parts. (Even)
The number of partitions into distinct odd parts: , the number of distinct partitions of the natural number with an odd number of parts. (Odd)
Therefore,
There are corresponding notions for partitions into parts as well. They are too complicated to list here.
The Pentagonal Number Theorem
Consider separately the denominator of the generating function for partition numbers:
Expanding this expression brings to mind partitions into distinct parts and the parity of their number of parts.
Specifically, partitions into a distinct even number of parts are counted positively in the expansion, while partitions into a distinct odd number of parts are counted negatively. Therefore, each coefficient in the expansion is the difference between the two counts. That is,
We next show that in most cases the two counts above are equal, so the coefficient in the expansion is ; only at a few positions do the two counts differ by or .
Here we can construct a correspondence.
Draw the Ferrers diagram of each distinct partition. The last row is called the base of the diagram, and let (Bottom) be the number of dots on the base. The longest -degree line segment connecting the last dot of the top row to a dot in the diagram is called the slide of the diagram, and let (Slide) be the number of dots on the slide.

To construct a correspondence between partitions into a distinct even number of parts and partitions into a distinct odd number of parts, define transformations that change the number of rows by while preserving distinctness:
Transformation A: when is less than or equal to , move the base to the right, making it a new slide.
Transformation B: when is greater than , move the slide to the bottom, making it a new base.
For any distinct partition of most , exactly one of these two transformations can be performed, thereby constructing a bijection between partitions into a distinct even number of parts and partitions into a distinct odd number of parts. Since a bijection has been constructed, the numbers of the two types of partitions are equal, and the coefficient of the -th term in the expansion is .
However, for some , there is exactly one distinct partition for which neither of the transformations above can be performed.
- Case 1: when and the base and slide have one common point, Transformation A cannot be performed. In this case,
The -th term of the expansion depends on the parity of the number of parts in the partition and is .
- Case 2: when and the base and slide have one common point, Transformation B cannot be performed. In this case,
The -th term of the expansion is .
Using in place of the in the formula above gives , where is a negative integer, and the -th term of the expansion is still .
Since the two cases cannot occur for the same , we can combine the two conditions. The condition on is
We have therefore proved:
Recall that this expression is the reciprocal of the generating function for partition numbers, so multiplying it by the generating function for partition numbers gives . Rearranging and comparing the coefficients on both sides gives the recurrence for the sequence of partition numbers.
This recurrence has infinitely many terms, but if we define the number of partitions of a negative number to be (the number of partitions of has already been defined as ), it simplifies to finitely many terms.
Example
Computing Partition Numbers
Compute the partition number . There are multiple test cases; is at most , and all answers are taken modulo .
Use the pentagonal number theorem. The code is as follows:
#include <cstdio>
long long a[100010];
long long p[50005];
int main() {
p[0] = 1;
p[1] = 1;
p[2] = 2;
int i;
for (i = 1; i < 50005;
i++) /*Recurrence coefficients 1,2,5,7,12,15,22,26...i*(3*i-1)/2,i*(3*i+1)/2*/
{
a[2 * i] = i * (i * 3 - 1) / 2; /*Pentagonal numbers are 1,5,12,22...i*(3*i-1)/2*/
a[2 * i + 1] = i * (i * 3 + 1) / 2;
}
for (
i = 3; i < 50005;
i++) /*p[n]=p[n-1]+p[n-2]-p[n-5]-p[n-7]+p[12]+p[15]-...+p[n-i*[3i-1]/2]+p[n-i*[3i+1]/2]*/
{
p[i] = 0;
int j;
for (j = 2; a[j] <= i; j++) /*The result may be negative; add 1000007 in the formula*/
{
if (j & 2) {
p[i] = (p[i] + p[i - a[j]] + 1000007) % 1000007;
} else {
p[i] = (p[i] - p[i - a[j]] + 1000007) % 1000007;
}
}
}
int n;
while (~scanf("%d", &n)) {
printf("%lld\n", p[n]);
}
}