Number of ways of distributing N identical objects in R distinct groups | C ++ implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } int NoOfDistributions ( int N , int R ) { return ncr ( N + R - 1 , R - 1 ) ; } int main ( ) { int N = 4 , R = 3 ; cout << NoOfDistributions ( N , R ) ; return 0 ; }