Problem
Description
Given three numbers \ (K, PA, Pb \) , each with a \ (\ frac {pa} { pa + pb} \) is added to the back of a probability a
with \ (\ frac {pb} { pa + pb} \) is added to the back of a probability that b
, when there is a \ (K \) th shaped as ab
a sub-sequence (not continuous) stop.
Seeking the last sub-sequence ab
number expected.
The answer to \ (9 + 10 ^ 7 \) modulo.
Sample
Input 1
1 1 1
Output 1
2
Input 2
3 1 4
Output 2
370000006
Range
\(k\le1000,p_a,p_b\le10^6\)
Algorithm
\ (DP \) , the probability and expectation
Mentality
Set \ (f_ {i, j} \) represents the currently \ (I \) a \ (A \) , \ (J \) subsequences \ (ab & \) , the sub-sequence at the end of the entire sequence \ (ab & \) of the desired number. The first dimension may be found infinite, considered backwards.
\ (f_ {i, j} \) transfer, there are two cases, one is added at the end \ (A \) , proceeds to \ (. 1 + F_ {I, J} \) , but joined \ (B \ ) was transferred to a \ (F_ {I, I + J} \) . Then push down the very obvious equation:
\ [F_ {I, J} = \ {FRAC P_A} + {P_A P_B F_ {I}. 1 +, + J} \ {FRAC P_A P_B} {+} F_ {P_B i, j + 1} \]
but the first infinite dimension of the problem is still not resolved border problems must be considered.
We found that when \ (i + j \ ge k \) when, if we add \ (b \) , the entire string will be terminated immediately.
Then for a state \ (f_ {i, j} , (i + j \ ge k) \) , a state is provided on this continuous addition \ (X \) a \ (A \) was added a \ (B \) , then:
\ [F_ {I, J} = \ FRAC {P_B} {P_A + P_B} \ sum_ {X = 0} ^ \ infty (I + J + X) (\ FRAC {P_A} {P_A + p_b}) ^ x \]
this is a geometric sequence, then we direct geometric series summation enough.
Calculated to obtain:
\ [F_ {I, J} = I + J + \ {FRAC P_A P_B} {} \]
credited directly to search on a star.
Code
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) w = ch == '-' ? -1 : 1, ch = getchar();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
const int Max_n = 1e3 + 5, mod = 1e9 + 7;
int n, a, b;
int pa, pb, pp;
int f[Max_n][Max_n];
int ksm(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) res = 1ll * res * a % mod;
return res;
}
int DP(int a, int ab) {
int &res = f[a][ab];
if (res != -1) return res;
if (a + ab >= n) {
res = (a + ab + pp) % mod;
return res;
}
return res =
(1ll * pa * DP(a + 1, ab) % mod + 1ll * pb * DP(a, ab + a) % mod) %
mod;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D.in", "r", stdin);
freopen("D.out", "w", stdout);
#endif
n = read(), a = read(), b = read();
pa = 1ll * a * ksm(a + b, mod - 2) % mod;
pb = 1ll * b * ksm(a + b, mod - 2) % mod;
pp = 1ll * a * ksm(b, mod - 2) % mod;
memset(f, -1, sizeof(f));
printf("%d\n", DP(1, 0));
}