Dice throw | Practice | GeeksforGeeks

Given N dice each with M faces, numbered from 1 to M, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown.
 
Example 1:
Input:
M = 6, N = 3, X = 12
Output:
25
Explanation:
There are 25 total ways to get
the Sum 12 using 3 dices with
faces from 1 to 6.
Example 2:
Input:
M = 2, N = 3, X = 6
Output:
1
Explanation:
There is only 1 way to get
the Sum 6 using 3 dices with
faces from 1 to 2. All the
dices will have to land on 2.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function noOfWays() which takes 3 Integers M,N and X as input and returns the answer.
 
Expected Time Complexity: O(M*N*X)
Expected Auxiliary Space: O(N*X)
 
Constraints:
1 <= M,N,X <= 50
0
hamidnourashraf1 month ago
recursive, too slow

class Solution:
    def throw_dice(self, M, N, X):
        if N == 0 and X == 0:
           return 1
        if N == 0 and X > 0:
           return 0
        cnt = 0
        for i in range(1, M+1):
            if X >= i:
                cnt += self.throw_dice(M, N-1, X-i)
        return cnt
        
    def noOfWays(self, M, N, X):
        return self.throw_dice(M, N, X)


recursive with memorization: Time: 0.1/1.3

class Solution:
    def throw_dice(self, M, N, X, mem):
        if N == 0 and X == 0:
            mem[N][X] = 1
            return mem[N][X]
        if N == 0 and X > 0:
            mem[N][X] = 0
            return mem[N][X]
        if mem[N][X] != -1:
            return mem[N][X]
        cnt = 0
        for i in range(1, M+1):
            if X >= i:
                cnt += self.throw_dice(M, N-1, X-i, mem)
        mem[N][X] = cnt
        return mem[N][X]
        
    def noOfWays(self, M, N, X):
        mem = [[-1]*(X+1) for i in range(N+1)]
        return self.throw_dice(M, N, X, mem)
+1
ruchitchudasama1232 months ago
 public:
  
    long long go(int m,int n,int target,vector<vector<long long>> &dp){
        if(n==0){
            return target==0?1:0;
        }
        
        if(dp[n][target]!=-1)
            return dp[n][target];
        
        long long count=0;
        for(int i=1 ; i<=m && target>=i ; i++){
            count+=go(m,n-1,target-i,dp);
        }
        return dp[n][target]=count;
    }
    long long noOfWays(int M , int N , int X) {
        vector<vector<long long>> dp(N+1,vector<long long>(X+1,-1));
        return go(M,N,X,dp);
    }
0
kiranrkuyate20212 months ago
long long noOfWays(int M , int N , int X) {
        // code here
        vector<vector<long long int>>dp(N+1,vector<long long int>(X+1,-1));
        return go(M,N,X,dp);
    }
    
    long long int go(int m,int n,int x,vector<vector<long long int>>&dp){
        if(n==0){
            return x==0?1:0;
        }
        if(dp[n][x]!=-1){
            return dp[n][x];
        }
        long long int ways=0;
        for(int dice=1;dice<=m&&x>=dice;dice++){
            ways+=go(m,n-1,x-dice,dp)*1ll;
        }
        return dp[n][x]=ways;
    }

0
harshanenwani1212 months ago
long long noOfWays(int M , int N , int X) {
       //Create a dp table of dimension N+1 and X+1 and fill up the value -1. (Number of dice is directly used as row index and sum is directly used as column index).
         
       vector<vector<long long int>> dp(N+1,vector<long long int>(X+1,-1));
      return go(M,N,X,dp);
   }
   
   long long int go(int m,int n,int x,vector<vector<long long int>>& dp){
       // when there are no dice and the sum to be made is 0
       
       if(n==0){
           return x==0?1ll:0ll;
       }
       
       if(dp[n][x]!=-1){
           return dp[n][x];
       }
       long long int ways=0;
       
       // Here ,dice should be less than equal to  x i.e( x>=dice),to have it as positive value 
    for(int dice=1;dice<=m  && x>=dice;dice++)
    {  
        // Say the first dice is taken from n dices so the ways to achieve the other dices will be n-1 and the sum(x) substracting to the dice that comes i.e(x-dice).
        
   ways=ways+0ll+go(m,n-1,x-dice,dp);
       }
       return dp[n][x]=ways;
   }
0
harshanenwani121
This comment was deleted.
+1
bhargabnath6912 months ago
BACKTRACK SOLUTION USING MAP:-
Time Taken: 0.1 / 3.4
class Solution {
  public:
    unordered_map<string, long long> dp;
    long long noOfWays(int M , int N , int X) {
        // code here
        if(X<N or X>N*M) return 0;
        if(N==1) return X<=M;
        string temp = to_string(M) + to_string(N) + to_string(X);
        if(dp.find(temp) == dp.end()){
            long long sum = 0;
            for(int i=1; i<=M; i++) sum += noOfWays(M, N-1, X-i);
            dp[temp] = sum;
        }
        return dp[temp];
    }
};
+1
utkarshrdce2 months ago
// C++ O(N*X) use prefix sum to calculate the dp values
class Solution {
  public:
    #define ll long long
    ll dp[52][52];
    
    long long noOfWays(int M , int N , int X) {
        
        
        memset(dp, 0 , sizeof(dp));
        dp[0][0] = 1;
        
        for(int i = 0; i <= N; i++) {
            for(int j = 1; j <= X; j++) {
                if(i == 0) {
                    dp[i][j] += dp[i][j-1];
                } else {
                    int start = max(0, j - M);
                    dp[i][j] = dp[i - 1][j - 1] - (start >  0 ? dp[i - 1][start - 1] : 0);
                    if(i == N && j == X) return dp[i][j];
                    dp[i][j]+=dp[i][j - 1];
                }
            }
        }
        
        return dp[N][X];
        
        
    }
};
0
himanshukug19cs3 months ago
java solution
 
 if(X>M*N)           return 0;       long[][] dp  = new long[N+1][M*N+1];       for(int i=0;i<=M*N;i++){           dp[0][i]=0;       }       for(int i=0;i<=M*N;i++){           if(i>=1&&i<=M)              dp[1][i]=1;           else              dp[1][i]=0;       }       for(int i=2;i<=N;i++){           for(int j=0;j<=N*M;j++){               dp[i][j]=0;               int k=1;               while(k<=M&&j-k>=0){                   dp[i][j]+=dp[i-1][j-k];                   k++;               }           }       } return dp[N][X];
 
+2
ankitpodder4 months ago
Total Time Taken:
0.0/3.4
class Solution {
    long long recFunc(int m , int n , int x){
        if(n == 0 && x == 0) return 1;
        else if(n == 0 || x<=0) return 0;
        long long ans = 0;
        for(int i = 1 ; i <= min(x , m) ; i++){
            ans += recFunc(m , n-1 , x-i);
        }
        return ans;
    }
  public:
    long long noOfWays(int m , int n , int x) {
        // code here
        // return recFunc(M , N , X);
        vector<vector<long long>> dp(n+1 , vector<long long>(x+1 , 0));
        dp[0][0] = 1;
        for(int i = 1 ; i < n+1 ; i++){
            for(int j = 1 ; j < x+1 ; j++){
                for(int k = 1 ; k <= min(m , j) ; k++){
                    dp[i][j] += dp[i-1][j-k];
                }
            }
        }
        
        return dp[n][x];
    }
};
0
anant dahiya6 months ago
    long long noOfWays(int m , int n , int x) {              /*       * Creare a dp vector to store number a ways to get        *   all numbers using all numbers of dice       *       */       vector<vector<long long>> dp(n, vector<long long>(x+1, 0));       //Iterate through row (No. of Dice)       for(int i=0;i<n;i++){           // Iterate through sum to be achieved           for(int j=1;j<x+1;j++){               // Iterate through numbers which can come on the dice               for(int k=1;k<=m;k++){                   // For initializing the first line of the dp array                   if(k<=j && i==0){                       if(j == k){                           dp[i][j] = 1;                       }                   }else if(k<=j && i!=0){                       // For other cases except the first row                       dp[i][j] = dp[i][j]+dp[i-1][j-k];                   }               }           }       }       // return right bottom number       return dp[n-1][x];   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.