hdu 4599 Dice
Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 126 Accepted Submission(s): 75
Problem Description
Given a normal dice (with 1, 2, 3, 4, 5, 6 on each face), we define:
F(N) to be the expected number of tosses until we have a number facing up for N consecutive times.
H(N) to be the expected number of tosses until we have the number '1' facing up for N consecutive times.
G(M) to be the expected number of tosses until we have the number '1' facing up for M times.
Given N, you are supposed to calculate the minimal M1 that G (M1) >= F (N) and the minimal M2 that G(M2)>=H(N)
Input
The input contains multiple cases.
Each case has a positive integer N in a separated line. (1<=N<=1000000000)
The input is terminated by a line containing a single 0.
Output
For each case, output the minimal M1 and M2 as required in a single line, separated by a single space.
Since the answer could be very large, you should output the answer mod 2011 instead.
Sample Input
1
2
0
Sample Output
1 1
2 7
Source
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
Recommend
liuyiding
引用 http://www.cnblogs.com/xin-hua/p/3281959.html
思路:
1.求f[n];dp[i]表示i个连续相同时的期望
则 dp[0]=1+dp[1]
dp[1]=1+(5dp[1]+dp[2])/6
……
dp[i]=1+(5dp[1]+dp[i+1])/6
……
dp[n]=0
可以求得f[n]=(6^n-1)/5.
2.求h[n];dp[i]表示i个连续相同的1时的期望
则 dp[0]=1+(5dp[0]+dp[1])/6
dp[1]=1+(5dp[0]+dp[2])/6
……
dp[i]=1+(5dp[0]+dp[i+1])/6
……
dp[n]=0
可以求得h[n]=(6^(n+1)-6)/5.
3.求g[m];dp[i]表示i个1时的期望
则 dp[0]=1+(5dp[0]+dp[1])/6
dp[1]=1+(5dp[1]+dp[2])/6
……
dp[i]=1+(5dp[i]+dp[i+1])/6
……
dp[n]=0
可以求得g[m]=6*m.
这样就有 m1>=(6^n-1)/30;m2>=(6^n-1)/5
分析易知(6^n-1)/30不能整除,所以m1=(6^n+24)/30 ; m2=(6^n-1)/5 .
代码:
1 /* 2 31MS 268K 517 B C++ 3 概率DP 4 f(n)=(6^n-1)/30 5 h(n)=(6^n-1)/5 6 g(m)=6*m 7 */ 8 #include<stdio.h> 9 #define N 2011 10 int n5=1609; //5的模N逆元 11 int n30=1944; //30的模N逆元 12 int Pow(int x) 13 { 14 int ans=1; 15 int a=6; 16 while(x){ 17 if(x&1) ans=ans*a%N; 18 a=a*a%N; 19 x>>=1; 20 } 21 return ans; 22 } 23 int main(void) 24 { 25 int n; 26 while(scanf("%d",&n),n) 27 { 28 int temp=Pow(n); 29 int temp1=(temp+24+N)%N; 30 int temp2=(temp-1+N)%N; 31 printf("%d %d\n",temp1*n30%N,temp2*n5%N); 32 } 33 return 0; 34 }