Total ways of choosing X men and Y women from a total of M men and W women | C ++ implementation of the approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ; Driver code
#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 totalWays ( int X , int Y , int M , int W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } int main ( ) { int X = 4 , Y = 3 , M = 6 , W = 5 ; cout << totalWays ( X , Y , M , W ) ; return 0 ; }