【hdu4652】Dice 期望dp 推公式

Dice

题目描述

题目传送门
一个骰子有 m m m 面,现在要求掷出如下情形的期望次数:

  • 连续 n n n 次结果都相同
  • 连续 n n n 次结果都不同

数据范围:

n ≤ m ≤ 1 0 6 n \le m \le 10^6 nm106

题解

没啥好说的= =就推推公式

问题1

f ( 0 ) = 1 + f ( 1 ) f ( i ) = 1 + 1 m f ( i + 1 ) + m − 1 m f ( 1 ) = 1 m + 1 m f ( i + 1 ) + ( 1 − 1 m ) f ( 0 ) ( i = 0 , 1 , . . . , n − 1 ) f ( n ) = 0 f(0)=1+f(1)\\ f(i)=1+\frac{1}{m}f(i+1)+\frac{m-1}{m}f(1)\\ =\frac1m+\frac1mf(i+1)+(1-\frac1m)f(0)\quad (i=0,1,...,n-1)\\ f(n)=0 f(0)=1+f(1)f(i)=1+m1f(i+1)+mm1f(1)=m1+m1f(i+1)+(1m1)f(0)(i=0,1,...,n1)f(n)=0

不动点法:
x = 1 m + ( 1 − 1 m ) f ( 0 ) + 1 m x x = f ( 0 ) − 1 1 − m g ( i ) = f ( i ) − f ( 0 ) + 1 1 − m g ( i ) = 1 m g ( i + 1 ) g ( 0 ) = 1 m n − 1 g ( n − 1 ) 1 1 − m = 1 m n − 1 ( f ( n − 1 ) − f ( 0 ) + 1 1 − m ) x=\frac1m+(1-\frac1m)f(0)+\frac1mx\\ x=f(0)-\frac1{1-m}\\ g(i)=f(i)-f(0)+\frac1{1-m}\\ g(i)=\frac1mg(i+1)\\ g(0)=\frac1{m^{n-1}}g(n-1)\\ \frac1{1-m}=\frac1{m^{n-1}}(f(n-1)-f(0)+\frac1{1-m})\\ x=m1+(1m1)f(0)+m1xx=f(0)1m1g(i)=f(i)f(0)+1m1g(i)=m1g(i+1)g(0)=mn11g(n1)1m1=mn11(f(n1)f(0)+1m1)
注意到:
f ( n − 1 ) = 1 m + 1 m f ( n ) + ( 1 − 1 m ) f ( 0 ) = 1 m + ( 1 − 1 m ) f ( 0 ) f(n-1)=\frac1m+\frac1mf(n)+(1-\frac1m)f(0)\\ =\frac1m+(1-\frac1m)f(0)\\ f(n1)=m1+m1f(n)+(1m1)f(0)=m1+(1m1)f(0)
因此:
1 1 − m = 1 m n − 1 ( 1 m + ( 1 − 1 m ) f ( 0 ) − f ( 0 ) + 1 1 − m ) f ( 0 ) = 1 − m n 1 − m \frac1{1-m}=\frac1{m^{n-1}}(\frac1m+(1-\frac1m)f(0)-f(0)+\frac1{1-m})\\ f(0)=\frac{1-m^n}{1-m} 1m1=mn11(m1+(1m1)f(0)f(0)+1m1)f(0)=1m1mn

问题2

首先,错位相减,消除前缀和:
f ( i ) = 1 + m − i m f ( i + 1 ) + ∑ j = 1 i 1 m f ( j ) f ( i − 1 ) = 1 + m − i + 1 m f ( i ) + ∑ j = 1 i − 1 1 m f ( j ) f(i)=1+\frac{m-i}{m}f(i+1)+\sum_{j=1}^{i}\frac{1}{m}f(j)\\ f(i-1)=1+\frac{m-i+1}{m}f(i)+\sum_{j=1}^{i-1}\frac{1}{m}f(j)\\ f(i)=1+mmif(i+1)+j=1im1f(j)f(i1)=1+mmi+1f(i)+j=1i1m1f(j)
发现配出了差分的式子:
f ( i ) − f ( i − 1 ) = m − i m f ( i + 1 ) − m − i + 1 m f ( i ) + f ( i ) m = m − i m [ f ( i + 1 ) − f ( i ) ] f(i)-f(i-1)=\frac{m-i}{m}f(i+1)-\frac{m-i+1}{m}f(i)+\frac{f(i)}{m}\\ =\frac{m-i}{m}[f(i+1)-f(i)]\\ f(i)f(i1)=mmif(i+1)mmi+1f(i)+mf(i)=mmi[f(i+1)f(i)]
再利用差分的前缀和求出答案:
a i = f ( i + 1 ) − f ( i ) a 0 = f ( 0 ) − f ( 1 ) = 1 a i = m i ∏ j = 1 i ( m − j ) = m i ( m − i − 1 ) ! ( m − 1 ) ! f ( 0 ) − f ( n ) = f ( 0 ) = ∑ i = 0 n − 1 a i a_i=f(i+1)-f(i)\\ a_0=f(0)-f(1)=1\\ a_i=\frac{m^i}{\prod_{j=1}^{i}(m-j)}=\frac{m^i(m-i-1)!}{(m-1)!}\\ f(0)-f(n)=f(0)=\sum_{i=0}^{n-1}a_i ai=f(i+1)f(i)a0=f(0)f(1)=1ai=j=1i(mj)mi=(m1)!mi(mi1)!f(0)f(n)=f(0)=i=0n1ai

你可能感兴趣的:(【hdu4652】Dice 期望dp 推公式)