先求初始状态 dp[1,1] => dp[1,6],dp[x,y]中x代表骰子数量,y代表点数和。dp[x,y]=dp[x-1,y+1]+...+dp[x-1,y+6]
dp存储x个骰子和为y的解数。
class Solution:
def dicesProbability(self, n: int) -> List[float]:
res=[]
dp={}
for i in range(1,7):
dp[1,i]=1
for i in range(1,n):
for j in range(i,6*i+1):
for k in range(1,7):
# print(dp,i,j,k)
if (i+1,j+k) in dp:
dp[i+1,j+k]+=dp[i,j]
else:
dp[i+1,j+k]=dp[i,j]
for i in range(n,6*n+1):
res.append(dp[n,i]/(6**n))
return res
网友评论