# Number of ways to cut a stick of length N into K pieces

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.

** Examples : **

Input : N = 5 K = 2 Output : 4 Input : N = 15 K = 5 Output : 1001

Solving this question is equivalent to solving the mathematics equation ** x _{1} + x_{2} + ….. + x_{K} = 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

**, where**

^{(N – 1)}C_{(K – 1)}^{N}C

_{K}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 10^{7} + 7 to mod the answer. We can calculate nCr % p by using Lucas Theorem.

However, python can handle large values without overflow.

## C++

`// C++ program to calculate the number of ways ` `// to divide a stick of length n into k pieces ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to generate nCk or nChoosek ` `unsigned ` `long` `long` `nCr(unsigned ` `long` `long` `n, ` ` ` `unsigned ` `long` `long` `r) ` `{ ` ` ` `if` `(n < r) ` ` ` `return` `0; ` ` ` ` ` `// Reduces to the form n! / n! ` ` ` `if` `(r == 0) ` ` ` `return` `1; ` ` ` ` ` `// nCr has been simplified to this form by ` ` ` `// expanding numerator and denominator to ` ` ` `// the form n(n - 1)(n - 2)...(n - r + 1) ` ` ` `// ----------------------------- ` ` ` `// (r!) ` ` ` `// in the above equation, (n - r)! is cancelled ` ` ` `// out in the numerator and denominator ` ` ` ` ` `unsigned ` `long` `long` `numerator = 1; ` ` ` `for` `(` `int` `i = n; i > n - r; i--) ` ` ` `numerator = (numerator * i); ` ` ` ` ` `unsigned ` `long` `long` `denominator = 1; ` ` ` `for` `(` `int` `i = 1; i < r + 1; i++) ` ` ` `denominator = (denominator * i); ` ` ` ` ` `return` `(numerator / denominator); ` `} ` ` ` `// Returns number of ways to cut ` `// a rod of length N into K pieces. ` `unsigned ` `long` `long` `countWays(unsigned ` `long` `long` `N, ` ` ` `unsigned ` `long` `long` `K) ` `{ ` ` ` `return` `nCr(N - 1, K - 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `unsigned ` `long` `long` `N = 5; ` ` ` `unsigned ` `long` `long` `K = 2; ` ` ` `cout << countWays(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of ways in which ` `// a stick of length n can be divided into K pieces ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// function to generate nCk or nChoosek ` ` ` `public` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `if` `(n < r) ` ` ` `return` `0` `; ` ` ` ` ` `// Reduces to the form n! / n! ` ` ` `if` `(r == ` `0` `) ` ` ` `return` `1` `; ` ` ` ` ` `// nCr has been simplified to this form by ` ` ` `// expanding numerator and denominator to ` ` ` `// the form n(n - 1)(n - 2)...(n - r + 1) ` ` ` `// ----------------------------- ` ` ` `// (r!) ` ` ` `// in the above equation, (n-r)! is cancelled ` ` ` `// out in the numerator and denominator ` ` ` ` ` `int` `numerator = ` `1` `; ` ` ` `for` `(` `int` `i = n ; i > n - r ; i--) ` ` ` `numerator = (numerator * i); ` ` ` ` ` `int` `denominator = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i < r + ` `1` `; i++) ` ` ` `denominator = (denominator * i); ` ` ` ` ` `return` `(numerator / denominator); ` ` ` `} ` ` ` ` ` `// Returns number of ways to cut ` ` ` `// a rod of length N into K peices ` ` ` `public` `static` `int` `countWays(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `return` `nCr(N - ` `1` `, K - ` `1` `); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `int` `K = ` `2` `; ` ` ` `System.out.println(countWays(N, K)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the number ` `# of ways in which a stick of length ` `# n can be divided into K pieces ` ` ` `# function to generate nCk or nChoosek ` `def` `nCr(n, r): ` ` ` ` ` `if` `(n < r): ` ` ` `return` `0` ` ` ` ` `# reduces to the form n! / n! ` ` ` `if` `(r ` `=` `=` `0` `): ` ` ` `return` `1` ` ` ` ` `# nCr has been simplified to this form by ` ` ` `# expanding numerator and denominator to ` ` ` `# the form n(n - 1)(n - 2)...(n - r + 1) ` ` ` `# ----------------------------- ` ` ` `# (r!) ` ` ` `# in the above equation, (n-r)! is cancelled ` ` ` `# out in the numerator and denominator ` ` ` ` ` `numerator ` `=` `1` ` ` `for` `i ` `in` `range` `(n, n ` `-` `r, ` `-` `1` `): ` ` ` `numerator ` `=` `numerator ` `*` `i ` ` ` ` ` `denominator ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, r ` `+` `1` `): ` ` ` `denominator ` `=` `denominator ` `*` `i ` ` ` ` ` `return` `(numerator ` `/` `/` `denominator) ` ` ` `# Returns number of ways to cut ` `# a rod of length N into K peices. ` `def` `countWays(N, K) : ` ` ` `return` `nCr(N ` `-` `1` `, K ` `-` `1` `); ` ` ` `# Driver code ` `N ` `=` `5` `K ` `=` `2` `print` `(countWays(N, K)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of ` `// ways in which a stick of length n ` `// can be divided into K pieces ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to generate nCk or nChoosek ` ` ` `public` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `if` `(n < r) ` ` ` `return` `0; ` ` ` ` ` `// Reduces to the form n! / n! ` ` ` `if` `(r == 0) ` ` ` `return` `1; ` ` ` ` ` `// nCr has been simplified to this form by ` ` ` `// expanding numerator and denominator to ` ` ` `// the form n(n - 1)(n - 2)...(n - r + 1) ` ` ` `// ----------------------------- ` ` ` `// (r!) ` ` ` `// in the above equation, (n-r)! is cancelled ` ` ` `// out in the numerator and denominator ` ` ` ` ` `int` `numerator = 1; ` ` ` `for` `(` `int` `i = n; i > n - r; i--) ` ` ` `numerator = (numerator * i); ` ` ` ` ` `int` `denominator = 1; ` ` ` `for` `(` `int` `i = 1; i < r + 1; i++) ` ` ` `denominator = (denominator * i); ` ` ` ` ` `return` `(numerator / denominator); ` ` ` `} ` ` ` ` ` `// Returns number of ways to cut ` ` ` `// a rod of length N into K pieces ` ` ` `public` `static` `int` `countWays(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `return` `nCr(N - 1, K - 1); ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `K = 2; ` ` ` `Console.Write(countWays(N, K)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to calculate the ` `// number of ways to divide a ` `// stick of length n into k pieces ` ` ` ` ` `// function to generate nCk or nChoosek ` `function` `nCr(` `$n` `, ` `$r` `) ` `{ ` ` ` `if` `(` `$n` `< ` `$r` `) ` ` ` `return` `0; ` ` ` ` ` `// Reduces to the form n! / n! ` ` ` `if` `(` `$r` `== 0) ` ` ` `return` `1; ` ` ` ` ` `// nCr has been simplified to this form by ` ` ` `// expanding numerator and denominator to ` ` ` `// the form n(n - 1)(n - 2)...(n - r + 1) ` ` ` `// ----------------------------- ` ` ` `// (r!) ` ` ` `// in the above equation, (n - r)! is cancelled ` ` ` `// out in the numerator and denominator ` ` ` ` ` `$numerator` `= 1; ` ` ` `for` `(` `$i` `= ` `$n` `; ` `$i` `> ` `$n` `- ` `$r` `; ` `$i` `--) ` ` ` `$numerator` `= (` `$numerator` `* ` `$i` `); ` ` ` ` ` `$denominator` `= 1; ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$r` `+ 1; ` `$i` `++) ` ` ` `$denominator` `= (` `$denominator` `* ` `$i` `); ` ` ` ` ` `return` `(` `floor` `(` `$numerator` `/ ` `$denominator` `)); ` `} ` ` ` `// Returns number of ways to cut ` `// a rod of length N into K peices. ` `function` `countWays(` `$N` `, ` `$K` `) ` `{ ` ` ` `return` `nCr(` `$N` `- 1, ` `$K` `- 1); ` `} ` ` ` `// Driver code ` `$N` `= 5; ` `$K` `= 2; ` `echo` `countWays(` `$N` `, ` `$K` `); ` `return` `0; ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

**Exercise : **

Extend the above problem with 0 length pieces allowed. Hint : The number of solutions can similarly be found by writing each x_{i} as y_{i} – 1, and we get an equation ** y _{1} + y_{2} + ….. + y_{K} = 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 contribute@geeksforgeeks.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.

## Recommended Posts:

- Print all possible strings of length k that can be formed from a set of n characters
- Factorial of a large number
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Paper Cut into Minimum Number of Squares
- Ways to represent a number as a sum of 1's and 2's
- Number of distinct permutation a String can have
- All possible strings of any length that can be formed from a given string
- Ways to sum to N using array elements with repetition allowed
- Count number of strings (made of R, G and B) using given combination
- Maximum number of 3-person teams formed from two groups
- Get the kth smallest number using the digits of the given number
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Number of bitonic arrays of length n and consisting of elements from 1 to n
- Rearrangement of a number which is also divisible by it