Dice Throw | DP | Java program to find number of ways to get sum ' x ' with ' n ' dice where every dice has ' m ' faces ; The main function that returns the number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; Create a table to store the results of subproblems . One extra row and column are used for simplicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for only one dice ; Fill rest of the entries in table using recursive relation i : number of dice , j : sum ; Return value ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static long findWays ( int m , int n , int x ) { long [ ] [ ] table = new long [ n + 1 ] [ x + 1 ] ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= x ; j ++ ) { for ( int k = 1 ; k < j && k <= m ; k ++ ) table [ i ] [ j ] += table [ i - 1 ] [ j - k ] ; } } return table [ n ] [ x ] ; } public static void main ( String [ ] args ) { System . out . println ( findWays ( 4 , 2 , 1 ) ) ; System . out . println ( findWays ( 2 , 2 , 3 ) ) ; System . out . println ( findWays ( 6 , 3 , 8 ) ) ; System . out . println ( findWays ( 4 , 2 , 5 ) ) ; System . out . println ( findWays ( 4 , 3 , 5 ) ) ; } }