HDU4652 Dice(期望dp推式子)

issue敲膩害發表於2020-10-12

題目連結

第一問,求連續 n n n個數字相同的期望

定義 d p [ i ] dp[i] dp[i]是已經 i i i個不相同,到達目標狀態的期望

d p [ n − 1 ] = 1 m ∗ d p [ n ] + m − 1 m ∗ d p [ 1 ] + 1 dp[n-1]=\frac{1}{m}*dp[n]+\frac{m-1}{m}*dp[1]+1 dp[n1]=m1dp[n]+mm1dp[1]+1

d p [ n − 2 ] = 1 m ∗ d p [ n − 1 ] + m − 1 m ∗ d p [ 1 ] + 1 dp[n-2]=\frac{1}{m}*dp[n-1]+\frac{m-1}{m}*dp[1]+1 dp[n2]=m1dp[n1]+mm1dp[1]+1

d p [ i ] = 1 m ∗ d p [ i + 1 ] + m − 1 m ∗ d p [ 1 ] + 1 dp[i]=\frac{1}{m}*dp[i+1]+\frac{m-1}{m}*dp[1]+1 dp[i]=m1dp[i+1]+mm1dp[1]+1

那麼 d p [ i ] − d p [ i − 1 ] = 1 m ( d p [ i + 1 ] − d p [ i ] ) dp[i]-dp[i-1]=\frac{1}{m}(dp[i+1]-dp[i]) dp[i]dp[i1]=m1(dp[i+1]dp[i])

那麼如果令 d i = d p [ i ] − d p [ i − 1 ] d_i=dp[i]-dp[i-1] di=dp[i]dp[i1],不就是公比為 m m m的等比數列嗎?

d n = d p [ 1 ] − d p [ 0 ] = − 1 d_n=dp[1]-dp[0]=-1 dn=dp[1]dp[0]=1

∑ i = 1 n d i = − d p [ 0 ] \sum\limits_{i=1}^{n}d_i=-dp[0] i=1ndi=dp[0](錯位相減法)

所以可以等比數列求和快速解得



第二問,求連續n個數不相同的概率

定義 d p [ i ] dp[i] dp[i]為已經 i i i個不相同,到達目標的期望

d p [ i ] = m − i m ∗ d p [ i + 1 ] + 1 m ∑ j = 1 i d p [ j ] dp[i]=\frac{m-i}{m}*dp[i+1]+\frac{1}{m}\sum\limits_{j=1}^{i}dp[j] dp[i]=mmidp[i+1]+m1j=1idp[j]

d p [ i + 1 ] = m − i − 1 m ∗ d p [ i + 2 ] + 1 m ∑ j = 1 i + 1 d p [ j ] dp[i+1]=\frac{m-i-1}{m}*dp[i+2]+\frac{1}{m}\sum\limits_{j=1}^{i+1}dp[j] dp[i+1]=mmi1dp[i+2]+m1j=1i+1dp[j]

d p [ i ] − d p [ i + 1 ] dp[i]-dp[i+1] dp[i]dp[i+1]得到

d p [ i ] − d p [ i + 1 ] = m − i m ∗ d p [ i + 1 ] − m − i + 1 m ∗ d p [ i + 2 ] − 1 m ∗ d p [ i + 1 ] dp[i]-dp[i+1]=\frac{m-i}{m}*dp[i+1]-\frac{m-i+1}{m}*dp[i+2]-\frac{1}{m}*dp[i+1] dp[i]dp[i+1]=mmidp[i+1]mmi+1dp[i+2]m1dp[i+1]

d p [ i ] − d p [ i + 1 ] = m − i − 1 m ( d p [ i + 1 ] − d p [ i + 2 ] ) dp[i]-dp[i+1]=\frac{m-i-1}{m}(dp[i+1]-dp[i+2]) dp[i]dp[i+1]=mmi1(dp[i+1]dp[i+2])

d p [ 0 ] − d p [ 1 ] = 1 dp[0]-dp[1]=1 dp[0]dp[1]=1

d p [ 1 ] − d p [ 2 ] = m m − 1 dp[1]-dp[2]=\frac{m}{m-1} dp[1]dp[2]=m1m

d p [ 2 ] − d p [ 3 ] = m m − 1 ∗ m m − 2 dp[2]-dp[3]=\frac{m}{m-1}*\frac{m}{m-2} dp[2]dp[3]=m1mm2m

把上面所有等式相加得到

d p [ 0 ] − d p [ n ] = 1 + m m − 1 + m m − 1 ∗ m m − 2 . . . . dp[0]-dp[n]=1+\frac{m}{m-1}+\frac{m}{m-1}*\frac{m}{m-2}.... dp[0]dp[n]=1+m1m+m1mm2m....

#include <bits/stdc++.h>
using namespace std;
int t;
int quick_pow(int x,int n)
{
	int ans=1;
	while( n )
	{
		if( n&1 )	ans=ans*x;
		n>>=1;
		x=x*x;
	}	return ans;
}
int main()
{
	while( cin >> t )
	{
		while( t-- )
		{
			int ok,m,n;
			cin >> ok >> m >> n;
			if( ok )
			{
				double ans=1,temp=1;
				for(int i=1;i<=n-1;i++)
				{
					temp*=m*1.0/(m-i);
					ans+=temp;
				}
				printf("%.6lf\n",ans);
			}
			else
			{
				double ans=(1.0-quick_pow(m,n))/(1.0-m);
				printf("%.6lf\n",ans);
			}
		}
	}
}

相關文章