1. 程式人生 > >【hdu4652】Dice 期望dp 推公式

【hdu4652】Dice 期望dp 推公式

Dice

題目描述

題目傳送門 一個骰子有 mm 面,現在要求擲出如下情形的期望次數:

  • 連續 nn 次結果都相同
  • 連續 nn 次結果都不同

資料範圍:

nm106n \le m \le 10^6

題解

沒啥好說的= =就推推公式

問題1

f(0)=1+f(1)f(i)=1+1mf(i+1)+m1mf(1)=1m+1mf(i+1)+(11m)f(0)(i=0,1,...,n1)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

不動點法: x=1m+(11m)f(0)+1mxx=f(0)11mg(i)=f(i)f(0)+11mg(i)=1mg(i+1)g(0)=1mn1g(n1)11m=1mn1(f(n1)f(0)+11m) 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})\\

注意到: f(n1)=1m+1mf(n)+(11m)f(0)=1m+(11m)f(0) f(n-1)=\frac1m+\frac1mf(n)+(1-\frac1m)f(0)\\ =\frac1m+(1-\frac1m)f(0)\\ 因此: 11m=1mn1(1m+(11m)f(0)
f(0)+11m)f(0)=1mn1m \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}

問題2

首先,錯位相減,消除字首和: f(i)=1+mimf(i+1)+j=1i1mf(j)f(i1)=1+mi+1mf(i)+j=1i11mf(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)f(i1)=mimf(i+1)mi+1mf(i)+f(i)m=mim[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)]\\ 再利用差分的字首和求出答案: ai=f(i+1)f(i)a0=f(0)f(1)=1ai=mij=1i(mj)=mi(mi1)!(m1)!f(0)f(n)=f(0)=i=0n1ai 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