Little Pony and Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 413 Accepted Submission(s): 106
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 5 6 10
Sample Output
0.49794 0.28929
Source
/***************
考虑dp[i]为恰好走到i点的概率
考虑由i-1点走到i的概率为dp[i-1]*1.0/m;
考虑能走到i-1的点必能走到i点但i-m-1点能走到i-1不能走到i
所以dp[i]的概率为 dp[i-1]-dp[i-m-1]*1.0/m+dp[i-1]*1.0/m;
****************/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define eps 1e-13
using namespace std;
const int maxn=600000;
double dp[maxn];
int sig(double n){
if(fabs(n)<eps)return 0;
return 1;
}
int main()
{
int n,m,i,j;
while(scanf("%d%d",&m,&n)!=EOF){
dp[0]=1.0;dp[1]=1.0/m;
for(i=2;i<=n;++i){
dp[i]=dp[i-1]+dp[i-1]*1.0/m;
if(i>m)dp[i]-=dp[i-m-1]*1.0/m;
if(sig(dp[i]-dp[i-1])==0){
printf("%.5lf\n",dp[i]);
break;
}
}
if(i>n){
printf("%.5lf\n",dp[n]);
}
}
return 0;
}