Number of ways to get a given sum with n number of m | C ++ function to calculate the number of ways to achieve sum x in n no of throws ; Function to calculate recursively the number of ways to get sum in given throws and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throws - 1 no of throws left ; Inserting present values in dp ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000007 NEW_LINE int dp [ 55 ] [ 55 ] ; int NoofWays ( int face , int throws , int sum ) { if ( sum == 0 && throws == 0 ) return 1 ; if ( sum < 0 throws == 0 ) return 0 ; if ( dp [ throws ] [ sum ] != -1 ) return dp [ throws ] [ sum ] ; int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throws - 1 , sum - i ) ; } return dp [ throws ] [ sum ] = ans ; } int main ( ) { int faces = 6 , throws = 3 , sum = 12 ; memset ( dp , -1 , sizeof dp ) ; cout << NoofWays ( faces , throws , sum ) << endl ; return 0 ; }