1. 程式人生 > >Codeforces 908 D New Year and Arbitrary Arrangement

Codeforces 908 D New Year and Arbitrary Arrangement

ble other have span pri res ould com rmi

Discription

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.

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;
}

  

Codeforces 908 D New Year and Arbitrary Arrangement