Number of ways to get a given sum with n number of mfaced dices
Given n dices each with m faces, numbered from 1 to m, find the number of ways to get a given sum X. X is the summation of values on each face when all the dice are thrown.
Examples:
Input : faces = 4 throws = 2 sum =4
Output : 3
Ways to reach sum equal to 4 in 2 throws can be { (1, 3), (2, 2), (3, 1) }Input : faces = 6 throws = 3 sum = 12
Output : 25
Approach:
Basically, it is asked to achieve sum in n number of operations using the values in the range [1…m].
Use dynamic programming top down methodology for this problem. The steps are:
 Base Cases:

If (sum == 0 and noofthrowsleft ==0) return 1 . It means that sum x has
been achieved.  If (sum < 0 and noofthrowsleft ==0) return 0.It means that sum x has not
been achieved in all throws.

If (sum == 0 and noofthrowsleft ==0) return 1 . It means that sum x has
 If present sum with present noofthrowsleft is already achieved then return it from table instead of re computation.
 Then we will loop through all the values of faces from i=[1..m] and recursively moving to achieve sumi and also decrease the noofthrowsleft by 1.
 Finally we will store current values in the dp array
Below is the implementation of the above method:
C++
// C++ function to calculate the number of // ways to achieve sum x in n no of throws #include <bits/stdc++.h> using namespace std; #define mod 1000000007 int dp[55][55]; // Function to calculate recursively the // number of ways to get sum in given // throws and [1..m] values int NoofWays( int face, int throws, int sum) { // Base condition 1 if (sum == 0 && throws == 0) return 1; // Base condition 2 if (sum < 0  throws == 0) return 0; // If value already calculated dont // move into recomputation if (dp[throws][sum] != 1) return dp[throws][sum]; int ans = 0; for ( int i = 1; i <= face; i++) { // Recusively moving for sumi in // throws1 no of throws left ans += NoofWays(face, throws  1, sum  i); } // Inserting present values in dp return dp[throws][sum] = ans; } // Driver function int main() { int faces = 6, throws = 3, sum = 12; memset (dp, 1, sizeof dp); cout << NoofWays(faces, throws, sum) << endl; return 0; } 
Python3
# Python3 function to calculate the number of # ways to achieve sum x in n no of throws import numpy as np mod = 1000000007 ; dp = np.zeros(( 55 , 55 )); # Function to calculate recursively the # number of ways to get sum in given # throws and [1..m] values def NoofWays(face, throws, sum ) : # Base condition 1 if ( sum = = 0 and throws = = 0 ) : return 1 ; # Base condition 2 if ( sum < 0 or throws = = 0 ) : return 0 ; # If value already calculated dont # move into recomputation if (dp[throws][ sum ] ! =  1 ) : return dp[throws][ sum ]; ans = 0 ; for i in range ( 1 , face + 1 ) : # Recusively moving for sumi in # throws1 no of throws left ans + = NoofWays(face, throws  1 , sum  i); # Inserting present values in dp dp[throws][ sum ] = ans; return ans; # Driver function if __name__ = = "__main__" : faces = 6 ; throws = 3 ; sum = 12 ; for i in range ( 55 ) : for j in range ( 55 ) : dp[i][j] =  1 print (NoofWays(faces, throws, sum )) ; # This code is contributed by AnkitRai01 
25
Time complexity : O(throws*faces)
Space complexity : O(faces*sum)
Recommended Posts:
 Number of ways to represent a number as sum of k fibonacci numbers
 Find the number of ways to divide number into four parts such that a = c and b = d
 Count number of ways to divide a number in 4 parts
 Number of ways to choose a pair containing an even and an odd number from 1 to N
 Number of ways to pair people
 Number of ways to reach the end of matrix with nonzero AND value
 Count ways to express even number ‘n’ as sum of even integers
 Ways to express a number as product of two different factors
 Number of ways to insert a character to increase the LCS by one
 Count number of ways to jump to reach end
 Count number of ways to partition a set into k subsets
 Count number of ways to cover a distance
 Number of ways to arrange N items under given constraints
 Total number of ways to place X and Y at n places such that no two X are together
 Count the number of ways to traverse a Matrix
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01