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

  

Codeforces 908 D New Year and Arbitrary Arrangement的更多相关文章

  1. Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)

    题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...

  2. 【CodeForces】908 D. New Year and Arbitrary Arrangement

    [题目]Good Bye 2017 D. New Year and Arbitrary Arrangement [题意]给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符 ...

  3. CF 908 D New Year and Arbitrary Arrangement —— 期望DP

    题目:http://codeforces.com/contest/908/problem/D 首先,设 f[i][j] 表示有 i 个 a,j 个 ab 组合的期望,A = pa / (pa + pb ...

  4. [CodeForces]908D New Year and Arbitrary Arrangement

    设状态f[i][j]表示有i个a,j个ab的期望 发现如果i+j>=k的话就再来一个b就行了. #include <iostream> #include <cstdio> ...

  5. Codeforces New Year and Arbitrary Arrangement

    New Year and Arbitrary Arrangement time limit per test2 seconds You are given three integers k, pa a ...

  6. Codeforces 908D New Year and Arbitrary Arrangement(概率DP,边界条件处理)

    题目链接  Goodbye 2017 Problem D 题意  一个字符串开始,每次有$\frac{pa}{pa+pb}$的概率在后面加一个a,$\frac{pb}{pa+pb}$的概率在后面加一个 ...

  7. CF 908D New Year and Arbitrary Arrangement——期望dp

    题目:http://codeforces.com/contest/908/problem/D 注意是子序列.加一个a对ab个数无影响:加一个b使ab个数多出它前面的a那么多个.所以状态里记录有多少个a ...

  8. Good Bye 2017 D. New Year and Arbitrary Arrangement

    看了别人的题解 首先这题是一个dp dp[i][j] i是当前有多少个a j是当前有多少个ab子序列 dp[i][j] = dp[i+1][j]*Pa + dp[i][i+j]*Pb; i,j 时加一 ...

  9. CF908D Arbitrary Arrangement

    题目大意: 给定三个数\(k\) , \(p_a\) , \(p_b\) 每次有\(\frac{p_a}{p_a+p_b}\)的概率往后面添加一个'a' 每次有\(\frac{p_b}{p_a+p_b ...

随机推荐

  1. C#帮助类

    1.集合操作 // <summary> /// 判断一个集合是否包含某个值 /// </summary> /// <typeparam name="T" ...

  2. 设计模式之模板方法模式 templateMethod

    代码实现 public abstract class BankTemplateMethod { //具体方法 public void takeNumber(){ System.out.println( ...

  3. 【现代程序设计】homework-02

    迟交了这么久,一定是0分了.可是我再怎么挣扎,还是不会.交了一维和二维的,这里说说思路吧.. 对于二维的情况,主要的思路就是将二维数组求矩形最大子数组的情况转化为一维的情况.因为所求的是矩形,我们就可 ...

  4. Thread 线程池

    Thread 线程池: 当使用多个较短存活期的线程有利时,运用线程池技术可以发挥作用.运用这一技术时,不是为每个任务创建一个全新的线程,而可以从线程池中抽出线程,并分配给任务.当线程完成任务后,再把它 ...

  5. XML快速入门

    XML是什么 Extensible Markup Language 自定义标签: 用来传输数据: 可扩展标记语言,是一种类似超文本标记语言的标记语言. 与HTML的比较: 1.不是用来替代HTML的: ...

  6. 【DNS】- 域名解析中A记录、CNAME、MX记录、NS记录的区别和联系

    1. A记录 又称IP指向,用户可以在此设置子域名并指向到自己的目标主机地址上,从而实现通过域名找到服务器.说明:·指向的目标主机地址类型只能使用IP地址: 附加说明: 1) 泛域名解析即将该域名所有 ...

  7. 开头什么的肯定要自我介绍然后把它扔到置顶咯>_<~

    大家嚎,我是NanoApe~ 现在高一,是个OIer.音游狗和一个爱着二次元的萌汉子妹子,欢迎前来勾搭>_<~ 最近就是要冲省队啦~~~~加油! 扣扣号:879006461 Weibo:伪 ...

  8. Statement [倍增+线段树]

    题面 思路 首先,可以确定的是,本题因为每个点只有一条入边,所以整个图肯定是一个基环外向树森林 那么我们首先考虑树上的情况: 我们考虑一个真点,它会对它的子树里面的所有假点产生贡献 一个真点对一个假点 ...

  9. webpack watch模式产生*.hot-update.json文件

    webpack --watch会产生*.hot-update.json文件,解决方法如下: output: { path: path.join(root, "dist"), fil ...

  10. DLP与上网行为管理的差别总结

    参考网康和深信服的上网行为管理手册,总结了一下DLP与上网行为管理的差别: DLP与上网行为管理在功能和使用目的上有很大不同.主要体现在: 上网行为管理主要是对用户访问目标URL过滤,应用端口限制,上 ...