CF908D New Year and Arbitrary Arrangement

原题链接:http://codeforces.com/problemset/problem/908/D

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

题目大意

起始为一个空序列, p a p a + p b 的概率在后面加一个 a p b p a + p b 的概率在后面加一个 b ,问有 k 个子序列 a b 时的期望长度为多少。

题解

先想一个简单的 d p

d p [ i ] [ j ] 表示该序列前缀为 i a ,有 j 个子序列 a b 时的期望长度,有转移方程如下:

d p [ i ] [ j ] = d p [ i + 1 ] [ j ] p a p a + p b + d p [ i ] [ j + i ] p b p a + p b

比较好理解,分别表示在序列后面加上 a , b 的情况。

但是,这个转移 n a i v e 了,当究极非酋的时候,就会出现 a b a b b a a a a a . . . 这样的无穷多 a 就是不出现 b 的情况,这样我们就会无限 d p 下去。。。

上述情况中, i + j 已经大于 k 了,只要再出现一个 b ,就能满足要求,但 b 就是不出来,所以我们需要手算这个情况:

d p [ i ] [ j ] = ( i + j ) p b + ( i + j + 1 ) p a p b + ( i + j + 2 ) p a 2 p b + . . . d p [ i ] [ j ] p a = ( i + j ) p a p b + ( i + j + 1 ) p a 2 p b + ( i + j + 2 ) p a 3 p b + . . . ( 1 p a ) d p [ i ] [ j ] = ( i + j ) p b + p b ( p a + p a 2 + p a 3 + . . . ) d p [ i ] [ j ] p b = ( i + j ) p b + p b p a ( 1 p b ) p b d p [ i ] [ j ] = i + j + p a ( 1 p b ) p b d p [ i ] [ j ] i + j + p a p b

只需要把 i + j > k 时的 d p 值赋值为 i + j + p a p b 即可。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=1005,mod=1e9+7;
int dp[M][M],k,pa,pb;
void in(){scanf("%d%d%d",&k,&pa,&pb);}
void exgcd(int a,int b,int &x,int &y){b?(exgcd(b,a%b,y,x),y-=a/b*x):(x=1,y=0);}
int inv(int a){int x,y;return exgcd(a,mod,x,y),(x+mod)%mod;}
int dfs(int a,int ab)
{
    if(dp[a][ab]!=-1)return dp[a][ab];
    if(a+ab>=k)return (a+ab+1ll*pa*inv(pb))%mod;
    return dp[a][ab]=(1ll*((1ll*dfs(a+1,ab)*pa)%mod+(1ll*dfs(a,ab+a)*pb)%mod)%mod*inv(pa+pb))%mod;
}
void ac(){memset(dp,-1,sizeof(dp));printf("%d",dfs(1,0));}
int main(){in();ac();}

猜你喜欢

转载自blog.csdn.net/shadypi/article/details/80819461