題目傳送門
dalao講解
其實其實比較水,我們只需要列出一個總式子就可以了,用dp[i]表示拼出長度爲i的多米諾骨牌需要至少多少次,於是我們枚舉一箇中間點作爲拼成i塊多米諾的最後一塊,於是我們用El表示拼好左邊長度的期望最少次數,Er表示拼好右邊長度的期望最少次數,則顯然如果我現在不倒的話,有拼好i塊長度爲Ei
則
Ei = El + Er + 1
這點是顯然的,然而無論倒不倒,左邊倒或者右邊倒,這個El + Er + 1都是必須要用的,在用了這個之後,還需要加上左邊倒了重建的期望乘以概率和右邊倒了重建的期望乘以概率
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const double inf = 0x3f3f3f3f;
double pl, pr, dp[1000010];
int n;
int main( ) {
while( scanf( "%d", &n ) != EOF && n ) {
scanf( "%lf%lf", &pl, &pr );
for( register int i = 1; i <= n; i++ ) {
dp[i] = inf;
for( register int j = 0; j < i; j++ )
dp[i] = min( dp[i], ( dp[j] * ( 1.0 - pl ) / ( 1.0 - pl - pr ) + dp[ i - 1 - j ] * ( 1 - pr ) / ( 1 - pl - pr ) ) );
dp[i] += ( 1.0 / ( 1.0 - pl - pr ) );
}
printf( "%.2lf\n", dp[n] );
}
return 0;
}