概率dp题,掷骰子

Little Pony and Dice


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40    Accepted Submission(s): 8


Problem Description
  
  
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.

The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with same probability. Also she knows that each toss is independent from others.

There is n + 1 cells in total, number from 0 to n, Twilight Sparkle race her token from 0 to n according to die rolls. The game end once she move to n or over it. Help Twilight Sparkle calculated the probability she end the game exactly at n.
 
Input
  
  
Input contains multiple test cases (less than 200).
For each test case, only one line contains two integers m and n ( 1m,n109 ).
 
Output
  
  
For each case, output the corresponding result, rounded to 5 digits after the decimal point.
 
Sample Input
  
  
3 5 6 10
 
Sample Output
  
  
0.49794 0.28929
 
分析:看到n和m的取值范围时,就有点儿蒙。其实,当n变大的时候,相应的误差会变得越来越小,忽略误差,就可以应用动态规划求解。具体:dp[i]表示的是恰好在i处终结的概率,而隐含的一维状态是指走了1~k步的所有情形。那么,dp[i]=dp[i-1]/m+dp[i-2]/m+dp[i-3]/m+ . . .  + dp[i-m]/m,而dp[i-1]=dp[i-2]/m+dp[i-3]/m+ . . . + dp[i-m-1]/m。所以,化简后便有dp[i] = dp[i-1]/m + dp[i-1]。如果当i>m时,便减去dp[i-m-1]*m这一项。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值