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 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
1 1 1
2
3 1 4
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的更多相关文章
- Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)
题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...
- 【CodeForces】908 D. New Year and Arbitrary Arrangement
[题目]Good Bye 2017 D. New Year and Arbitrary Arrangement [题意]给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符 ...
- 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 ...
- [CodeForces]908D New Year and Arbitrary Arrangement
设状态f[i][j]表示有i个a,j个ab的期望 发现如果i+j>=k的话就再来一个b就行了. #include <iostream> #include <cstdio> ...
- Codeforces New Year and Arbitrary Arrangement
New Year and Arbitrary Arrangement time limit per test2 seconds You are given three integers k, pa a ...
- Codeforces 908D New Year and Arbitrary Arrangement(概率DP,边界条件处理)
题目链接 Goodbye 2017 Problem D 题意 一个字符串开始,每次有$\frac{pa}{pa+pb}$的概率在后面加一个a,$\frac{pb}{pa+pb}$的概率在后面加一个 ...
- CF 908D New Year and Arbitrary Arrangement——期望dp
题目:http://codeforces.com/contest/908/problem/D 注意是子序列.加一个a对ab个数无影响:加一个b使ab个数多出它前面的a那么多个.所以状态里记录有多少个a ...
- 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 时加一 ...
- CF908D Arbitrary Arrangement
题目大意: 给定三个数\(k\) , \(p_a\) , \(p_b\) 每次有\(\frac{p_a}{p_a+p_b}\)的概率往后面添加一个'a' 每次有\(\frac{p_b}{p_a+p_b ...
随机推荐
- python - web自动化测试 - 元素操作 - 窗口切换
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: 元素操作-切换.py @ide: PyCharm Community ...
- NodeJs02 美女爬虫
note: demo代码要编号 导出模块 一个js文件就是一个模块,模块内部的所有变量,对象,方法对外界都不可见.如果想暴漏出去让别人用,就需要导出模块.语法如下: module.exports = ...
- c++ devived object model
单一虚函数继承 class A{public: virtual int foo( ) { return val ; } virtual int funA( ) {}private: int val ; ...
- ubuntu16.04 搭建nexus+maven 学习
/opt/nexus-2.10.0-02/bin vim nexus 关键配置: RUN_AS_USER=root JAVA_HOME=/usr/lib/java/jre export NEXUS_H ...
- 基于eclipse+maven创建web工程
Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...
- 在浏览器中进行深度学习:TensorFlow.js (八)生成对抗网络 (GAN
Generative Adversarial Network 是深度学习中非常有趣的一种方法.GAN最早源自Ian Goodfellow的这篇论文.LeCun对GAN给出了极高的评价: “There ...
- 【CZY选讲·Triangle】
题目描述 长度为的铁丝,你可以将其分成若干段,并把每段都折成一个三角形.你还需要保证三角形的边长都是正整数并且三角形两两相似,问有多少种不同的分法. 数据范围 1≤≤10^6 题解: ①相 ...
- CodeWarrior的map文件详解
前言 map文件保存了你的整个程序编译链接后的各种信息,包括编译器链接器信息,内存分配信息,对象依赖等,每次编译链接程序后,这个文件都会被覆盖重新生成. 对我来说,它最主要的作用是它详尽的描述了整个程 ...
- Mysql性能优化【转】
mysql的性能优化无法一蹴而就,必须一步一步慢慢来,从各个方面进行优化,最终性能就会有大的提升. Mysql数据库的优化技术 对mysql优化是一个综合性的技术,主要包括 表的设计合理化(符合3NF ...
- hdu6188&&百度之星初赛(B) T5
度度熊的交易计划 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生 ...