HDU-4599 Dice (概率DP)

77 篇文章 0 订阅

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)


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

题目大意:设F(n)表示连续n次出现同一个数字时掷骰子次数的期望,H(n)表示连续n次出现数字1时掷骰子次数的期望,G(n)表示总共出现n次数字1时掷骰子次数的期望,求最小的m1使G(m1)>=F(n),最小的m2使G(m2)>=H(n) ?


设dp[i]表示已经连续掷出n次同一个数字时,离目标状态还需掷骰子次数的期望,则dp[n]=0,dp[0]=dp[1]+1

状态转移方程为:dp[i]=1/6*(dp[i+1]+1)+5/6*(dp[1]+1);                        ①


有两种方法可求得dp[0]

方法一:

设参数a[i],b[i],使得 dp[i]=a[i]*dp[1]+b[i];                                                        ②

②带入①右边化简得:dp[i]=(1/6*a[i+1]+5/6)*dp[1]+1/6*b[i+1]+1;          ③

对比①③参数可得:a[i]=1/6*a[i+1]+5/6;

                                    b[i]=1/6*b[i+1]+1;

且a[n]=b[n]=0;

用数列递推公式求通项公式的方法,可得通项公式(i<=n):

a[i]=1-(1/6)^(n-i);

b[i]=6/5-6/5*(1/6)^(n-i);

又:dp[1]=(1/6*a[2]+5/6)*dp[1]+1/6*b[2]+1;

将a[2],b[2]代入上式解得:dp[1]=1/5*6^n-6/5;

则:dp[0]=dp[1]+1=1/5*6^n-1/5=1/5*((6^n)-1);

【找到的题解都写成:(6^n-1)/5,我一直看成(6^(n-1))/5,导致不停地算了一天,总“算不对”。。。】

方法二:

发现这样求解更简单

由①可得:dp[i+1]=1/6*(dp[i+2]+1)+5/6*(dp[1]+1);                                  ④
①-④得:dp[i]-dp[i+1]=1/6*(dp[i+1]-dp[i+2]);
即: dp[i+1]-dp[i+2]=6*(dp[i]-dp[i+1]);
又:dp[0]-dp[1]=1;
则:dp[1]-dp[2]=6;
…dp[i]-dp[i+1]=6^i;
…dp[n-1]-dp[n]=6^(n-1);
对上述等式两边分别求和得:dp[0]-dp[n]=6^0+6^1+…+6^(n-1)=1/5*((6^n)-1);
则dp[0]=1/5*((6^n)-1);

则: F(n)=dp[0]=1/5*((6^n)-1);

同理:设dp[i]表示已经连续掷出n次数字1,离目标状态还需掷骰子次数的期望,则dp[n]=0

状态转移方程为:dp[i]=1/6*(dp[i+1]+1)+5/6*(dp[0]+1);

可求得通项公式为:H(n)=dp[0]=6/5*((6^n)-1);


设dp[i]表示共出现n次数字1,离目标状态还需掷骰子次数的期望,则dp[m]=0

状态转移方程为:dp[i]=1/6*(dp[i+1]+1)+5/6*(dp[i]+1);

可求得通项公式为:G(m)=dp[0]=6*m;


令G(m1)>=F(n),解得:m1>=1/30*((6^n)-1);

令G(m2)>=H(n),解得:m2>=1/5*((6^n)-1);

又:((6^x)-1)%30==(6-1)%30==5恒成立,则 1/30*((6^n)-1)必定为小数,又(6^x+24)%30==(6+24)%30==0恒成立,则大于1/30*((6^n)-1)的第一个整数为1/30*((6^n)+24)

而:((6^x)-1)%5==(6-1)%5==0恒成立,则大于等于1/5*((6^n)-1)的第一个整数为1/5*((6^n)-1)

则得:m1=(1/30*((6^n)+24))%2011,m2=(1/5*((6^n)-1))%2011;


由于存在除法,所以需要求出30和5关于2011的乘法逆元,可以用扩展欧几里德求解,再进行模运算即可。


#include <cstdio>

using namespace std;

const int MOD=2011;

int n,m1,m2,e1,e2,t;

int ex_gcd(int a,int b,int& x,int& y) {
	int d;
	if(b==0) {
		x=1;
		y=0;
		return a;
	}
	d=ex_gcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int quickpow(int a,int n) {
    int b=1;
    while(n!=0) {
        if((n&1)!=0) {
            b=(a*b)%MOD;
        }
        a=(a*a)%MOD;
        n>>=1;
    }
    return b;
}

int main() {
    ex_gcd(30,MOD,e1,t);
    ex_gcd(5,MOD,e2,t);
    e1=(e1%MOD+MOD)%MOD;
    e2=(e2%MOD+MOD)%MOD;
    while(scanf("%d",&n),n!=0) {
        t=quickpow(6,n);
        m1=(t+24)%MOD;
        m2=(t-1+MOD)%MOD;
        m1=(m1*e1)%MOD;
        m2=(m2*e2)%MOD;
        printf("%d %d\n",m1,m2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值