New Year and Arbitrary Arrangement

time limit per test2 seconds

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 .

大概就是说给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符串末尾+a,有pb/(pa+pb)的可能在字符串末尾+b,求加到组成至少k对子序列“ab"时的期望子序列“ab”数。k<=1000,pa,pb<=10^6。

(连markdown我都懒得补在原文上了233)

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 .

emmm.... 期望要倒推。。。。正无穷啥的拿等比数列消去。。。( $A^{inf}=0\ \ \ (0
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 3, mod = 1e9 + 7;
long long k, pa, pb, dp[maxn][maxn];
bool flag[maxn][maxn];

long long inv(int t)
{
int lin = mod - 2;
long long tmp = t, ret = 1;
while(lin){
if(lin & 1) ret = ret * tmp % mod;
tmp = tmp * tmp % mod;
lin >>= 1;
}
return ret;
}

long long workk(int a, int b)
{
if(flag[a][b]) return dp[a][b];
if(a + b >= k){
dp[a][b] = a + b + (pa * inv(pb) % mod);
flag[a][b] = true; return dp[a][b];
}
dp[a][b] = ((pa * inv(pa + pb) % mod) * workk(a + 1, b) % mod + (pb * inv(pa + pb) % mod) * workk(a, a + b) % mod) % mod;
flag[a][b] = true; return dp[a][b];
}

int main()
{
scanf("%I64d%I64d%I64d", &k, &pa, &pb);
printf("%I64d", workk(1, 0));
return 0;
}

Codeforces 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. [CodeForces]908D New Year and Arbitrary Arrangement

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

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

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

  5. Codeforces 908 D New Year and Arbitrary Arrangement

    Discription You are given three integers k, pa and pb. You will construct a sequence with the follow ...

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

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

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

  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# Aspose.Words 数据写入到word,模板样式复杂(转换指定内容并返回多张图片)

    public ResultResponse<string[]> PrintStudyRecords([FromBody]StudyInfo info) { ResultResponse&l ...

  2. Codeforces 1183H DP 计算子序列数目

    题意及思路:https://blog.csdn.net/mmk27_word/article/details/93999633 第一次见这种DP,有点像退背包的思想,如果发现有可能因为字母相同和前面算 ...

  3. mac系统下安装和启动nginx

    1.在线安装 localhost:nginx-1.17.1 mhx$ sudo brew install nginx 2.查看是否安装成功 localhost:nginx-1.17.1 mhx$ ng ...

  4. 三步完成Source Insight 4.0 破解安装(转)

    转自:https://blog.csdn.net/biubiuibiu/article/details/78044232 三步完成Source Insight 4.0 破解安装   下载地址有更新,之 ...

  5. jQuery遍历之向下遍历

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  6. JS的加载和执行

    从JS的加载和执行谈性能优化 ---高性能JS读后感(第一章) 从脚本的"霸道"说起,随着浏览器的进步,js越来越听话了,所以,我们先说说以前的浏览器是怎么加载js的,以及js如何 ...

  7. <自动化测试>之<Selenium API 的用法1>

    今天,简单,举例说一下在用python+selenium中元素定位的主要方法,第一部分是单个元素的操作,第二部分是一类元素的操作,实际操作中注意区分 #!/usr/bin/env python # - ...

  8. BZOJ 4568: [Scoi2016]幸运数字(倍增+线性基)

    传送门 解题思路 异或最大值肯定线性基了,树上两点那么就倍增搞一搞,就维护每个点到各级祖先的线性基,时间复杂度\(O(nlog^3n)\),并不知道咋过去的. 代码 #include<iostr ...

  9. jmeter之--断言json响应&json path espressions的语法

    一.提取所需要断言的内容: 响应数据如下:加入需要提取id为90的值 { , "name" : "python", "url" : &quo ...

  10. 团队冲刺DAY4

    DES算法 算法概要 在DES.java当中创立两个方法分别用作加密和解密 通过 `public static byte[] encrypt(byte[] data, String sKey) 创建方 ...