Dice throw
You are given D dice, each having F faces numbered 1 to F, both inclusive. The task is to find the possible number of ways to roll the dice together such that the sum of face-up numbers equal the given target S.
Note :
As the answer can be large, return your answer modulo 10^9 + 7.
Follow Up :
Can you solve this using not more than O(S) extra space?
Input format :
The first line of input contains an integer T denoting the number of test cases.
The first and the only line of every test case contains 3 integers D, F and S representing the number of dices, the number of faces on each die and the target sum respectively.
Output format :
For each test case, print the number of ways to get the sum S in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= D, F <= 50
1 <= S <= 10^3
Time limit: 1 sec
CodingNinjas
author
2y
I used dp to solve this problem with the Number of dice directly used as row index and the sum is directly used as the column index. In the end, I returned the last element of dp table.
CodingNinjas
author
2y
Brute force
- The idea is to use recursion to reduce the big problem into several small subproblems.
- We will call a helper function that returns us the number of combinations that sum upto S and store it ...read more
CodingNinjas
author
2y
Memoization
Let’s understand by an example
Suppose we have 3 dice each with 6 faces and we have to find the number of ways to achieve the sum 8.
The below figure shows some of the recursive calls are ...read more
CodingNinjas
author
2y
DP
- The idea is to create a 2-D DP with row size D+1 and column size S+1.
- Initially all the elements of the DP table will be 0.
- Now, the value DP[i][j] means the possible number of combinations with i di...read more
CodingNinjas
author
2y
Optimized DP
- The idea is to create a 1-D DP of size S+1.
- Initially, all the elements of the DP table will be 0.
- We can make only S = 0 with 0 dice, so dp[0] = 1,
- The detailed algorithm to fill the DP tabl...read more
Add answer anonymously...
Top Microsoft Corporation Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Microsoft Corporation Software Developer Intern
>
Microsoft Corporation Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app