Codeforces 908 D New Year and Arbitrary Arrangement

Discription

You are given three integers kpa 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.

Example

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 .

 

设f[i][j]为有i对ab,并且已经有j个a的期望,转移很好写,f[i][j]= (pa/(pa+pb))*f[i][j+1] + (pb/(pa+pb))*f[i+j][j] 、

但是可以发现的是如果要计算所有状态的话j显然可以无限大,,,比如全是a的序列。。。。

但是还可以发现,当i+j>=k的时候,(pb/(pa+pb))*f[i+j][j] 其实就等于 (pb/(pa+pb))*(i+j)。

这样我们等比数列错位相减一下(需要化简一大堆式子,在这就懒得写了),可以得到一个边界:f[i][j]=i+j +pa/pb    (i+j>=n)

然后f[i][0]=f[i][1],这个带第一个转移的式子就可以得到。。。。。

 

/*
    设f[i][j]为有i对ab,目前已经有了j个a的ab期望个数 
	1.f[i][j]= pa/pb + i+j ,其中i+j>=n  (这个推个式子然后生成函数一下就OJBK了)
	2.f[i][0]=f[i][1] (这个也是代换一下就好了)
	3.其他情况下,f[i][j]= (pa/(pa+pb))*f[i][j+1] + (pb/(pa+pb))*f[i+j][j] 
*/
#include<bits/stdc++.h>
#define ll long long
const int ha=1000000007;
const int maxn=1005;
int inv[2000005];
int n,pa,pb;
int f[2005][1005];

inline void init(){
	inv[1]=1;
	for(int i=2;i<=2000000;i++) inv[i]=-inv[ha%i]*(ll)(ha/i)%ha+ha;
}

inline int add(int x,int y){
	x+=y;
	if(x>=ha) return x-ha;
	else return x;
}

inline void dp(){
	int base=(pa*(ll)inv[pb]+(ll)n)%ha;
	int PA=pa*(ll)inv[pa+pb]%ha,PB=pb*(ll)inv[pa+pb]%ha;
	for(int i=n-1;i>=0;i--){
		for(int j=n-i;j<=n;j++) f[i][j]=add(base,j-n+i);
		for(int j=n-i-1;j;j--) f[i][j]=add(f[i][j+1]*(ll)PA%ha,f[i+j][j]*(ll)PB%ha);
		f[i][0]=f[i][1];
	}
}

int main(){
	init();
	scanf("%d%d%d",&n,&pa,&pb);
	dp();
	printf("%d\n",f[0][0]);
	return 0;
}

  

 

posted @ 2018-02-20 20:59  蒟蒻JHY  阅读(348)  评论(0编辑  收藏  举报