- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Number of Dice Rolls With Target Sum in Python

Suppose we have d dice, and each die has f number of faces numbered 1, 2, ..., f. We have to find the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of the face up numbers equal to the target. So if the input is like d = 2, f = 6, target = 7, then the output will be 6. So if we throw each dice with 6 faces, then there are 6 ways to get sum 6, as 1 + 6, 2 + 5, 3 + 3, 4 + 3, 5 + 2, 6 + 1.

To solve this, we will follow these steps −

- m := 1e9 + 7
- make a table dp of order d x (t + 1), and fill this with 0
- for i in range 0 to d – 1
- for j in range 0 to t
- if i = 0, then dp[i, j] := 1 when j in range 1 to f, otherwise 0
- otherwise
- for l in range 1 to f
- if j – l > 0, then dp[i, j] := dp[i, j] + dp[i – 1, j - l], and dp[i,j] := dp[i, j] mod m

- for l in range 1 to f

- for j in range 0 to t
- return dp[d – 1, t] mod m

## Example(Python)

Let us see the following implementation to get better understanding −

class Solution(object): def numRollsToTarget(self, d, f, t): mod = 1000000000+7 dp =[[0 for i in range(t+1)] for j in range(d)] for i in range(d): for j in range(t+1): if i == 0: dp[i][j] = 1 if j>=1 and j<=f else 0 else: for l in range(1,f+1): if j-l>0: dp[i][j]+=dp[i-1][j-l] dp[i][j]%=mod return dp [d-1][t] % mod ob = Solution() print(ob.numRollsToTarget(2,6,7))

## Input

2 6 7

## Output

6

- Related Articles
- C++ code to count number of dice rolls to get target x
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Program to find number of sublists whose sum is given target in python
- Program to find number of distinct quadruple that forms target sum in python
- Number of Submatrices That Sum to Target in C++
- Target Sum in C++
- Program to find two non-overlapping sub-arrays each with target sum using Python
- Program to find lowest sum of pairs greater than given target in Python
- Program to check number of triplets from an array whose sum is less than target or not Python
- Program to find size of smallest sublist whose sum at least target in Python
- Program to find number of combinations of coins to reach target in Python
- Program to find number of unique four indices where they can generate sum less than target from four lists in python
- Sum of Mutated Array Closest to Target in C++
- Program to find number of given operations required to reach Target in Python
- Program to find sum of two numbers which are less than the target in Python

Advertisements