Codeforces 908 D New Year and Arbitrary Arrangement

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

  

原文地址:https://www.cnblogs.com/JYYHH/p/8455806.html

时间: 2024-08-01 17:03:54

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

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

题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾添加字母b,当出现≥k个ab子串时立即停止添加字母,求最后期望的ab子串个数.(子串ab不要求连续) 例子:当k=1,aab含2个ab,bbabbab时不可能出现的,因为到了bbab就会停止添加字母. 题解: 期望DP DP果然是智商的分界线 orz @.@#,这题题意其实我也没看太懂,后来看了别人

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) , B = pb / (pa + pb) 那么 f[i][j] = A * f[i+1][j] + B * f[i][i+j] 当 i+j >= k 时,再出现一个 b 就会结束,所以此时: f[i][j] = f[i][i+j] * B + f[i+1][i+j+1] * A * B + f[

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

题目链接  Goodbye 2017 Problem D 题意  一个字符串开始,每次有$\frac{pa}{pa+pb}$的概率在后面加一个a,$\frac{pb}{pa+pb}$的概率在后面加一个$b$. 求当整个串中有至少$k$个$ab$的时候(不需要连续,下同),字符串中$ab$个数的期望. 设$f[i][j]$为字符串有$i$个$a$,$j$个$ab$的时候字符串中$ab$个数的期望 设$p = \frac{pa}{pa+pb}$, $q = \frac{pb}{pa+pb}$ 那么对

【CF908D】New Year and Arbitrary Arrangement

Problem Description 给定三个数 \(k,pa,pb\) ,每次有 \(\frac{pa}{pa+pb}\) 的概率往后面添加一个 a,有 \(\frac{pb}{pa+pb}\) 的概率往后面添加一个 b ,当出现了 \(k\) 个形如 ab 的子序列(不用连续)时停止. 求最后子序列 ab 的期望个数. 答案对 \(10^9+7\) 取模. Sample Input 1 1 1 1 Output 1 2 Input 2 3 1 4 Output 2 370000006 Ra

Goodbye 2017 Solution

从这里开始 题目列表 Problem A New Year and Counting Cards Problem B New Year and Buggy Bot Problem C New Year and Curling Problem D New Year and Arbitrary Arrangement Problem E New Year and Entity Enumeration Problem F New Year and Rainbow Roads Problem G New

期望$DP$ 方法总结

期望\(DP\) 方法总结 这个题目太大了,变化也层出不穷,这里只是我的一点心得,不定期更新! 1. 递推式问题 对于无穷进行的操作期望步数问题,一般可用递推式解决. 对于一个问题\(ans[x]\), 我们可以考虑建立逻辑转移: \[ans[now] = Merge(\ \ Function(ans[now])\ ,\ Function(ans[other])\ \ )\] 那么我们进行移项后, \[ans[now]\ Delete\ Function(ans[now])\ \ =\ \ Fu

省选前的CF题

RT,即将退役的人懒得一篇篇写题解,于是有了这个东西 CF1004E 树上选一条不超过k个点的链,最小化其余点到链上点的最大距离 这个思路很有意思,不像平时一般的树上问题,是从叶子开始一点点贪心合并直到合得只剩一条链,这条链就是最后的答案 用优先队列完成,复杂度$O(n\log n)$ 1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 #include<

CodeForces 367 C Sereja and the Arrangement of Numbers 欧拉回路

Sereja and the Arrangement of Numbers 题解: ummm. 在一副图中,如果全部点的度数是偶数/只有2个点是奇数,则能一笔画. 考虑图的点数k为奇数的时候,那么每个点的度数都是偶数点,所以就是可以一笔画,答案为 1 +k * (i - kll) / 2; k为偶数的时候,所有的点是奇数点,我们保留2个点是奇数点,将其他的点改为偶数点,就可以一笔画了.  1 +k * (i - kll) / 2 + k/2 - 1. 代码: #include<bits/stdc

CodeForces - 367C Sereja and the Arrangement of Numbers

题目大意: 要求构建一个长为n的数组,其中每两种不同的元素必须有一对是相邻的 然后给出m个交易,如果数组中有q[i]这个元素,就给w[i]元钱,求最多能得到多少钱? 具体思路: 先求出有x个不同元素的最小长度,然后给钱排个序就好啦 怎么求最小长度呢?可以把数组看成一个完全图的一条路径 最小长度大概就是一个欧拉路径啦,奇偶分类讨论一下就好啦 AC代码 #include<bits/stdc++.h> #define int long long using namespace std; int a[