Probability of distributing M items among X bags such that first bag contains N items | CPP program to find probability of first bag to contain N items such that M items are distributed among X bags ; Function to find factorial of a number ; Function to find nCr ; Function to find probability of first bag to contain N items such that M items are distributed among X bags ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } float Probability ( int M , int N , int X ) { return ( float ) ( nCr ( M - N - 1 , X - 2 ) / ( nCr ( M - 1 , X - 1 ) * 1.0 ) ) ; } int main ( ) { int M = 9 , X = 3 , N = 4 ; cout << Probability ( M , N , X ) ; return 0 ; }