HDU4987 Little Pony and Dice

题目

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 (1<=m, n<=10^9).

Output

 
For each case, output the corresponding result, rounded to 5 digits after the decimal point.

Simple input

3 5
6 10

Simple output

0.49794
0.28929

题目分析

概率dp,dp[i]=dp[i-1]/m+dp[i-2]/m+…..+dp[i-m-1]/m;推导的时候用dp[i]=dp[i-1]+dp[i-1]/3

AC代码

6268K/171MS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
void fre()
{

freopen("c://test//input.in", "r", stdin);
freopen("c://test//output.out", "w", stdout);
}
#define MP(x,y) make_pair(x,y)
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
#define debug puts("----------")
#define INF 0x3f3f3f3f
#define maxn 1050
const long long mod = 1e9+7;
using namespace std;
double ans[600000];
int main()
{

long long m,n;
while(scanf("%lld%lld",&m,&n)!=EOF)
{
memset(ans,0,sizeof(ans));
ans[0]=1;
ans[1]=1.0/(double)m;
int flag=0;
for(int i=2;i<=n;i++)
{
ans[i] = ans[i-1]+ans[i-1]/(double)m;
if(i>m)
ans[i]-=ans[i-m-1]/(double)m;
if(fabs(ans[i]-ans[i-1])<1e-13)
{
flag=1;
printf("%.5lf\n",ans[i]);
break;
}
}
if(flag==0)
printf("%.5lf\n",ans[n]);
}
}

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=4987