# codeforces 908d New Year and Arbitrary Arrangement

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 .
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 .

dp[i][j]=pa/(pa+pb)*dp[i+1][j]+pb/(pa+pb)*dp[i][i+j]

ans=pbpa+pbinfj=0(papa+pb)j(k+j)$ans=\frac{pb}{pa+pb}*\sum_{j=0}^{inf}(\frac{pa}{pa+pb})^{j}*(k+j)$哇顺带学一学latex qwq 蒟蒻的颓废之旅

=i+j+papb$=i+j+\frac{pa}{pb}$

dp[0][0]=(pa∗dp[1][0]+pb∗dp[0][0])/(pa+pb)

dp[0][0]就等于dp[1][0]!所以答案就是dp[1][0]即可。


#include<cstdio>
#define ll long long
#define mod 1000000007
ll k,pa,pb,dp[1100][1100];
int main(){
freopen("cf.in","r",stdin);
scanf("%lld%lld%lld",&k,&pa,&pb);
ll invb=1,base=pb;for (int t=mod-2;t;t>>=1,(base*=base)%=mod) if (t&1) (invb*=base)%=mod;
ll inva_b=1;base=pa+pb;for (int t=mod-2;t;t>>=1,(base*=base)%=mod) if (t&1) (inva_b*=base)%=mod;
ll invab=pa*invb%mod;pa*=inva_b;pa%=mod;pb*=inva_b;pb%=mod;
for (int i=k;~i;--i)
for (int j=k;~j;--j)
if (i+j>=k) dp[i][j]=(i+j+invab)%mod;
else dp[i][j]=pa*dp[i+1][j]%mod+pb*dp[i][i+j]%mod,dp[i][j]%=mod;
printf("%lld",dp[1][0]);
return 0;
}



• 评论

• 上一篇
• 下一篇