Number of Dice Rolls With Target Sum
You have d dice, and each die has f faces numbered 1, 2, …, f.
Return 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 equals target.
Example
Input: d = 1, f = 6, target = 3
Output: 1
Explanation:
You throw one die with 6 faces. There is only one way to get a sum of 3.
Solution
const mod = 1e9 + 7
func numRollsToTarget(d int, f int, target int) int {
dp:= [31][1001]int{}
dp[0][0] = 1
for i:=1;i<=d;i++{
maxT:= min(target, i*f)
for j:=i;j<=maxT;j++{
maxF:= min(f, j)
for k:=1;k<=maxF;k++{
dp[i][j] += dp[i-1][j-k]
}
dp[i][j] %= mod
}
}
return dp[d][target]
}
func min(a,b int)int {
if a<b{
return a
}
return b
}