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. poj1285 Combinations, Once Again(泛化背包)

    题目传送门 Combinations, Once Again Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1897   A ...

  2. 使用批处理发布 QT 的程序

    1. 将 QT Creator 生成的 exe 文件拷贝到一个目录中 :C:\Users\zyy\Desktop\qtrelease 2.新建 bat 文件:qt.bat . 编辑文件,注意替换 QT ...

  3. Windows开发,关于通过写代码加载PDB的那些事

    最近,接到一个活,要写一个程序,用来批量分析一堆dll和对应的PDB, 其实工作很简单,就是根据一堆偏移,通过PDB文件,找到对应dll里面对应位置的明文符号, 简单的需求,实现起来,通常都很麻烦, ...

  4. vue证明题五,组件传值与绑定

    上文中写了一个input组件,该组件需要复用,但是并不是每个组件都相同的 比如我定义了一个组件,是个矿泉水瓶子,这个瓶子分为大中小三个号,定义了三种瓶子的容积,定义了必须有瓶盖,瓶口,瓶子质地 但是瓶 ...

  5. Docker基础(下)

    Docker基础(下) 链接:https://pan.baidu.com/s/1u8Tg5qB4ZZHEK6GqCJkjwg 提取码:u8hb 复制这段内容后打开百度网盘手机App,操作更方便哦 5. ...

  6. vs2010管理员运行

    VS2010  Configuation->Linker->Manifest File->UAC Execution Level-> requireAdministrator

  7. Intellij idea创建javaWeb:实现JSP/Servlet(转)

    转自:https://www.jianshu.com/p/9684e90cf7b5 Intellij idea创建javaWeb:实现JSP/Servlet by_love唯念 关注 2016.12. ...

  8. java 三元运算符

    /* 一元运算符:只需要一个数据就可以进行操作的运算符 如:取反! 自增++ 自减 -- 二元运算符:需要两个数据才可以进行操作的运算符 如:加法+ 赋值= 三元运算符: 需要三个数据才可以进行操作的 ...

  9. 数据结构---Java---Hastable

    1.概述 1.1 Hashtable是线程安全的: 1.2 源码 public class Hashtable<K,V> extends Dictionary<K,V> imp ...

  10. vue-cli脚手架快速搭建项目

    前言 vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. 准备工作 在搭建一个vue项目之前,需要先安装好node.js和cnpm. 虽然np ...