Given a stick of size N, find the number of ways in which it can be cut into K pieces such that length of every piece is greater than 0.
Input : N = 5 K = 2 Output : 4 Input : N = 15 K = 5 Output : 1001
Solving this question is equivalent to solving the mathematics equation x1 + x2 + ….. + xK = N
We can solve this by using the bars and stars method in Combinatorics, from which we obtain the fact that the number of positive integral solutions to this equation is (N – 1)C(K – 1), where NCK is N! / ((N – K) ! * (K!)), where ! stands for factorial.
In C++ and Java, for large values of factorials, there might be overflow errors. In that case we can introduce a large prime number such as 107 + 7 to mod the answer. We can calculate nCr % p by using Lucas Theorem.
However, python can handle large values without overflow.
Extend the above problem with 0 length pieces allowed. Hint : The number of solutions can similarly be found by writing each xi as yi – 1, and we get an equation y1 + y2 + ….. + yK = N + K. The number of solutions to this equation is (N + K – 1)C(K – 1)
This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Probability that the pieces of a broken stick form a n sided polygon
- Paper Cut into Minimum Number of Squares
- Number of ways of choosing K equal substrings of any length for every query
- Ways of dividing a group into two halves such that two elements are in different groups
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Count ways to split N! into two distinct co-prime factors
- Find all possible ways to Split the given string into Primes
- Count ways to split array into K non-intersecting subsets
- Count ways to generate N-length array with 0s, 1s, and 2s such that sum of all adjacent pairwise products is K
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Ways to represent a number as a sum of 1's and 2's
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to form a heap with n distinct integers
- Number of ways to merge two arrays such retaining order
- Number of ways to form an array with distinct adjacent elements
- Number of ways to reach Nth floor by taking at-most K leaps
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles