HDU 4987(概率dp)
来源:互联网 发布:想找个网络兼职 编辑:程序博客网 时间:2024/05/17 22:54
Little Pony and Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 489 Accepted Submission(s): 139
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.
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 (1<=m, n<=10^9).
For each test case, only one line contains two integers m and n (1<=m, n<=10^9).
Output
For each case, output the corresponding result, rounded to 5 digits after the decimal point.
Sample Input
3 56 10
Sample Output
0.497940.28929
Source
BestCoder Round #7
题目链接:
题目描述:
大富翁游戏,多组输入m,n两个值,m表示筛子的最大点数,n表示格子数的最大编号(从0开始,总共n+1个各自)。当走到格子编号大于或等于n时则胜利。
求出当胜利时刚好到达n点时的概率。
解题思路:
这道题是概率dp,dp[i]表示刚好到达i时的概率。dp[0]=1,能到达dp[i-1]的点除了i-1-m外都能到达dp[i]。dp[i]=dp[i-1]+dp[i-1]*1.0/m,当i>m时dp[i]-=dp[i-m-1]*1.0/m。
当n的值越大时,dp[i-1]与dp[i]的值相当接近,所以可以用一个定值来表示。代码中用前后差值小于1e-13来表示非常小。
AC代码:
#include <iostream>#include <stdio.h>#include <cmath>#include <cstring>#include <algorithm>#define eps 1e-13#define maxn 703000//#define zero(x) (fabs(x)<eps?0:x)using namespace std;typedef long long LL;double zero(double x){ return ((fabs(x)<eps)? 0:x);}double dp[maxn];int main(){ LL m,n; while(cin >> m >> n) { dp[0]=1.0;//初始位置概率为1 dp[1]=1.0/m; int i; for(i=2;i<=n;i++) { dp[i]=dp[i-1]+dp[i-1]*1.0/m;//转移公式 if(i>m) dp[i]=dp[i]-dp[i-m-1]*1.0/m;//将i-m-1概率的贡献删去 if((zero(dp[i]-dp[i-1]))==0)//当i足够大师直接输出 { printf("%.5lf\n",dp[i]); break; } } if(i>n) printf("%.5lf\n",dp[n]); } return 0;}
0 0
- HDU 4987(概率dp)
- hdu 4652(概率dp)
- HDU Walk (概率DP)
- HDU 4405(概率dp)
- hdu 3583(概率dp)
- hdu 4576(概率dp)
- hdu - 4649 - Professor Tian(概率dp)
- hdu 4405 Aeroplane chess(概率dp)
- HDU 4576 Robot (概率dp)
- HDU 4576 Robot (概率DP)
- HDU 4405 Aeroplane chess (概率dp)
- hdu 4089 Activation(概率dp)
- hdu 4405 Aeroplane chess (概率dp)
- hdu 4089 Activation(概率dp)
- (概率+树形)dp HDU 4035 Maze
- HDU 4035 Maze (树状dp + 概率)
- HDU 4576 Robot(概率dp)
- HDU 4652 Dice (概率DP)
- Ubuntu系统vi编辑器上下左右键变ABCD的解决方法
- 编程之美 - 只考加法的算术题
- python 获取 中国证券网 的公告
- Mock方法介绍
- html网页中显示浏览器标题栏小图标
- HDU 4987(概率dp)
- Java单元测试(Junit+Mock+代码覆盖率)
- centos7重启apache、nginx、mysql、php-fpm命令
- ScrollView 往上滑动,里面的一个View停在某个位置的思路
- Spring Cache抽象详解
- 郁闷的C小加(三)
- B - The 3n + 1 problem
- 在TestNG中运行WebDriver测试用例
- 通过demo搞懂encode_utf8和decode_utf8