solution
C n m = C n − 1 m − 1 + C n − 1 m C_n^m=C_{n-1}^{m-1}+C_{n-1}^{m} Cnm=Cn−1m−1+Cn−1m
d p [ i ] [ j ] = ∑ u = 0 i + u ≤ n ∑ v = 0 j + v ≤ n d p [ i + u ] [ j + v ] C i + u u p u q i C j + v v p v q j + d p [ i ] [ j ] C i 0 q i C j 0 q j + 1 dp[i][j]=\sum\limits_{u=0}^{i+u≤n}\sum\limits_{v=0}^{j+v≤n}dp[i+u][j+v]C_{i+u}^up^uq^iC_{j+v}^vp^vq^j+dp[i][j]C_{i}^0q^iC_j^0q^j+1 dp[i][j]=u=0∑i+u≤nv=0∑j+v≤ndp[i+u][j+v]Ci+uupuqiCj+vvpvqj+dp[i][j]Ci0qiCj0qj+1
P [ n ] [ m ] = C n m p m q n − m P[n][m]=C_n^mp^mq^{n-m} P[n][m]=Cnmpmqn−m
= C n − 1 m − 1 p m q n − m + C n − 1 m p m q n − m =C_{n-1}^{m-1}p^mq^{n-m}+C_{n-1}^{m}p^mq^{n-m} =Cn−1m−1pmqn−m+Cn−1mpmqn−m
= C n − 1 m − 1 p m − 1 q n − m ∗ p + C n − 1 m p m q n − m − 1 ∗ q =C_{n-1}^{m-1}p^{m-1}q^{n-m}*p+C_{n-1}^{m}p^{m}q^{n-m-1}*q =Cn−1m−1pm−1qn−m∗p+Cn−1mpmqn−m−1∗q
= P [ n − 1 ] [ m − 1 ] ∗ p + P [ n − 1 ] [ m ] ∗ q =P[n-1][m-1]*p+P[n-1][m]*q =P[n−1][m−1]∗p+P[n−1][m]∗q
d p [ i ] [ j ] = ∑ u = 0 i + u ≤ n ∑ v = 0 j + v ≤ n d p [ i + u ] [ j + v ] ∗ P [ i + u ] [ u ] ∗ P [ j + v ] [ v ] + d p [ i ] [ j ] ∗ P [ i ] [ 0 ] ∗ P [ j ] [ 0 ] + 1 dp[i][j]=\sum\limits_{u=0}^{i+u≤n}\sum\limits_{v=0}^{j+v≤n}dp[i+u][j+v]*P[i+u][u]*P[j+v][v]+dp[i][j]*P[i][0]*P[j][0]+1 dp[i][j]=u=0∑i+u≤nv=0∑j+v≤ndp[i+u][j+v]∗P[i+u][u]∗P[j+v][v]+dp[i][j]∗P[i][0]∗P[j][0]+1
d p [ i ] [ j ] = ∑ u = 0 i + u ≤ n ∑ v = 0 j + v ≤ n d p [ i + u ] [ j + v ] ∗ P [ i + u ] [ u ] ∗ P [ j + v ] [ v ] + 1 1 − P [ i ] [ 0 ] ∗ P [ j ] [ 0 ] dp[i][j]=\frac{\sum\limits_{u=0}^{i+u≤n}\sum\limits_{v=0}^{j+v≤n}dp[i+u][j+v]*P[i+u][u]*P[j+v][v]+1}{1-P[i][0]*P[j][0]} dp[i][j]=1−P[i][0]∗P[j][0]u=0∑i+u≤nv=0∑j+v≤ndp[i+u][j+v]∗P[i+u][u]∗P[j+v][v]+1
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
double p, q;
double dp[110][110];
double P[110][110];
void solve() {
while (cin >> n >> m >> p) {
q = 1.0 - p;
double _p = 1, _q = 1;
if (n + m + (int) p == 0) return;
memset(dp, 0, sizeof dp);
P[0][0] = 1;
P[1][0] = q, P[1][1] = p;
for (int i = 2; i <= n; ++i) {
P[i][0] = P[i - 1][0] * q;
P[i][i] = P[i - 1][i - 1] * p;
for (int j = 1; j < i; ++j) {
P[i][j] = P[i - 1][j] * q + P[i - 1][j - 1] * p;
}
}
for (int i = n; i >= 0; --i) {
for (int j = n; j >= 0; --j) {
double ans = 0;
if (i < m || j < m) {
for (int u = 0; i + u <= n; ++u) {
for (int v = 0; j + v <= n; ++v) {
ans += dp[i + u][j + v] * P[n - i][u] * P[n - j][v];
}
}
dp[i][j] = (ans + 1.0) / (1 - P[n - i][0] * P[n - j][0]);
}
}
}
cout << fixed << setprecision(6) << dp[0][0] << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}