Hi! I encounter difficulties in understanding a well known fact in number theory. Given a number n, in how many ways can we write n as a sum of numbers? Exemple: 4 -> ( 4 ) ( 1 3 ) ( 2 2 ) ( 1 1 2 ) ( 1 1 1 1 ) Many thanks for those who can help me
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hi! I encounter difficulties in understanding a well known fact in number theory. Given a number n, in how many ways can we write n as a sum of numbers? Exemple: 4 -> ( 4 ) ( 1 3 ) ( 2 2 ) ( 1 1 2 ) ( 1 1 1 1 ) Many thanks for those who can help me
Name |
---|
The object is called partition.
The link is https://en.wikipedia.org/wiki/Partition_(number_theory), but Codeforces apparently dislikes the parentheses, so above is a hacky way to get there.
Related: https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)
Stars and Bars
A similar question can be rephrased as: How many ways are there to distribute n stars into k bins (possibly empty), where stars are non-distinguishable, but unlike your question, the bins/variables are distinguishable (so order matters!). If n = 4, and you have say k = 4 'bins' to distribute each star, stars and bars will count the number of ways this can be done (the number of unique k-tuples that sum to n). When order does not matter and empty bins are disallowed (eg 1 + 0 + 0 + 3 = 3 + 0 + 0 + 1 = 1 + 0 + 3 + 0 etc are counted as the same, but would all be different in stars in bars), use partitions, provided in the comment above by Gassa.
Note that stars and bars can be extended to work for inequalities: How many ways to write n as a sum that is less than or equal to n using just k = 4 bins? Just add an extra bin (k = 5) where the stars count as 0.
Thank you, these helped me, especially 'stars and bars'
Note that this problem can be solved indirectly using Combinatorics. Suppose that you have a positive integer n that you would like to partition as the summation of k numbers, where k ≥ 1. An equivalent problem is to count the number of distinct (n + k - 1) bit strings that contain exactly n zeros and k - 1 ones. The k numbers are simply the lengths of k zero segments whose total length is n and are separated by k - 1 ones in such bit strings.
The answer should be . When any of the k numbers should be strictly positive, the count should exclude the cases when the length of some segment is zero, i.e. when an n + k - 1 bit string contains two consecutive ones or when either the first bit or the last bit is one.
As already mentioned, this problem is popularly known as partition problem. It has a closed-formula, already mentioned by CodingKnight.
Here is one such problem you can solve.UVa: How do you add