Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)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)
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.
Since the answer could be very large, you should output the answer mod 2011 instead.
1 2 0
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]表示已经连续掷出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;
}