- Related Questions & Answers
- Program to count number of ways we can distribute coins to workers in Python
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to count number of ways we can place nonoverlapping edges to connect all nodes in C++
- Program to count number of strings we can make using grammar rules in Python
- Program to count number of words we can generate from matrix of letters in Python
- Program to count number of stepping numbers of n digits in python
- Program to count how many ways we can divide the tree into two trees in Python
- Program to count how many ways we can cut the matrix into k pieces in python
- Program to count number of unique palindromes we can make using string characters in Python
- Program to check number of ways we can move k times and return back to first place in python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a number n, number of faces, and a total value, we have to find the number of ways it is possible to throw n dice with faces each to get total. If the answer is very large mod the result by 10**9 + 7.

So, if the input is like n = 2 faces = 6 total = 8, then the output will be 5, as there are 5 ways to make 8 with 2 6-faced dice: (2 and 6), (6 and 2), (3 and 5), (5 and 3), (4 and 4).

To solve this, we will follow these steps −

m := 10^9 + 7

dp := a list of size (total + 1) then fill with 0

for face in range 1 to minimum of faces, update in each step by total + 1, do

dp[face] := 1

for i in range 0 to n - 2, do

for j in range total to 0, decrease by 1, do

dp[j] := sum of all dp[j - f] for f in range 1 to faces + 1 when j - f >= 1

return last element of dp mod m

Let us see the following implementation to get better understanding −

class Solution: def solve(self, n, faces, total): m = 10 ** 9 + 7 dp = [0] * (total + 1) for face in range(1, min(faces, total) + 1): dp[face] = 1 for i in range(n - 1): for j in range(total, 0, -1): dp[j] = sum(dp[j - f] for f in range(1, faces + 1) if j - f >= 1) return dp[-1] % m ob = Solution() n = 2 faces = 6 total = 8 print(ob.solve(n, faces, total))

2,6,8

5

Advertisements