You have n dice, and each die has k faces numbered from 1 to k.
Given three integers n, k, and target, return the number of possible ways (out of the kn total ways) to roll the dice so the sum of the face-up numbers equals target. Since the answer may be very large, return it modulo 109+7.
Function Signature
def num_rolls_to_target(n: int, k: int, target: int) -> int:
...
Rules
-
The dice are distinguishable, so a roll is an ordered sequence of
n
face values. Rolling
(1, 6)
and rolling
(6, 1)
count as two different ways.
-
Return the count modulo
1000000007
. When no roll reaches
target
, return
0
.
Constraints
-
1 <= n, k <= 30
-
1 <= target <= 1000
Examples
Input: n = 1, k = 6, target = 3
Output: 1
With one six-sided die, the only way to get a sum of 3 is to roll a 3.
Input: n = 2, k = 6, target = 7
Output: 6
The six ordered rolls are (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1).
Input: n = 2, k = 6, target = 13
Output: 0
Two six-sided dice sum to at most 12, so no roll reaches 13.