博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 908 D New Year and Arbitrary Arrangement
阅读量:4923 次
发布时间:2019-06-11

本文共 2577 字,大约阅读时间需要 8 分钟。

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
#define ll long longconst 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;}

  

 

转载于:https://www.cnblogs.com/JYYHH/p/8455806.html

你可能感兴趣的文章
Console命令详解,让调试js代码变得更简单
查看>>
hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)
查看>>
Excel 导出
查看>>
拉登是我罩的队_第三周_需求改进&原型设计
查看>>
数据库got error 28 from storage engine问题
查看>>
RMQ 总结
查看>>
手撸ORM
查看>>
POJ---2406 Power Strings[求最长重复字串]
查看>>
005-(已测试成功的方案)kickstart模式实现批量安装centos7.x系统
查看>>
linux搭建haproxy
查看>>
Oracle update 日期
查看>>
【t088】倒水
查看>>
【t016】邮递员
查看>>
boost安装
查看>>
Vue与React的异同
查看>>
360:跳高游戏
查看>>
CSS3 Background-size
查看>>
Python Ethical Hacking - MAC Address & How to Change(3)
查看>>
生成验证码
查看>>
深入理解计算机系统 第2章 信息的表示和处理
查看>>