利用动态规划的思想
dp[i][j] = dp[i][j] + dp[i - 1][j - k] (1 <= k <= f)
代码是leetcode的答案,和书上的问题不完全一样
class Solution: def numRollsToTarget(self, d: int, f: int, target: int) -> int: m = 10**9+7 res = [[0]*(target+1) for i in range(d+1)] res[0][0] = 1 for i in range(1,d+1): for j in range(1,f+1): for k in range(j,target+1): res[i][k] = (res[i][k] + res[i - 1][k - j]) % m return res[-1][-1]