Codeforces New Year and Arbitrary Arrangement

New Year and Arbitrary Arrangement

time limit per test2 seconds

You are given three integers k, pa and pb.

You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.

You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and . Print the value of .


大概就是说给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符串末尾+a,有pb/(pa+pb)的可能在字符串末尾+b,求加到组成至少k对子序列“ab"时的期望子序列“ab”数。k<=1000,pa,pb<=10^6。

(连markdown我都懒得补在原文上了233)


Input

The first line will contain three integers integer k, pa, pb (1 ≤ k ≤ 1 000, 1 ≤ pa, pb ≤ 1 000 000).

Output

Print a single integer, the answer to the problem.

Examples

input
1 1 1

output

2

input

3 1 4

output

370000006

Note

The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.

The expected amount of times that 'ab' will occur across all valid sequences is 2.

For the second sample, the answer is equal to .


emmm.... 期望要倒推。。。。正无穷啥的拿等比数列消去。。。( $A^{inf}=0\ \ \ (0 #include<bits/stdc++.h> using namespace std; const int maxn = 1e3 + 3, mod = 1e9 + 7; long long k, pa, pb, dp[maxn][maxn]; bool flag[maxn][maxn]; long long inv(int t) { int lin = mod - 2; long long tmp = t, ret = 1; while(lin){ if(lin & 1) ret = ret * tmp % mod; tmp = tmp * tmp % mod; lin >>= 1; } return ret; } long long workk(int a, int b) { if(flag[a][b]) return dp[a][b]; if(a + b >= k){ dp[a][b] = a + b + (pa * inv(pb) % mod); flag[a][b] = true; return dp[a][b]; } dp[a][b] = ((pa * inv(pa + pb) % mod) * workk(a + 1, b) % mod + (pb * inv(pa + pb) % mod) * workk(a, a + b) % mod) % mod; flag[a][b] = true; return dp[a][b]; } int main() { scanf("%I64d%I64d%I64d", &k, &pa, &pb); printf("%I64d", workk(1, 0)); return 0; }
posted @ 2018-06-07 15:07  沛霖  阅读(235)  评论(0编辑  收藏  举报