Good Bye 2017-D. New Year and Arbitrary Arrangement

       复习概率dp,想到这题就拿出来练一下。
D. New Year and Arbitrary Arrangement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

       其实概率dp也是dp中比较难的,dp不是求最值的吗?怎么用它来算概率?会让人摸不着头脑。它的状态转移方程

通常冗杂繁复,一看就让人头大。 

       当我看到这道题,第一反应概率dp,然后不会!看题解发现照着它写能写出来,但自己推又一脸懵逼了,所以我

是真的菜。

       好了,那我们看看这道题。

       定义dp[i][j]表示当前串中出现了 i个子序列'a',j个子序列'ab'时,再继续做下去,最后停止时的子序列'ab'期望出现

概率。我们向dp[i][j]后附加一个a时,转移到的状态应该是dp[i+1][j],附加一个b时,转移到的状态应该是dp[i][i+j]。

因此我们的状态转移方程为:dp[i][j]=pa/(pa+pb)*dp[i+1][j]+pb/(pa+pb)*dp[i][i+j]

我们的初始状态是dp[i][j] = j 这里i随意,j>=k。我们的末状态是dp[0][0],即对应着一开始的空串。

但是,上面的状态定义其实有两个小问题。

       1. 我们刚刚说了,初始状态dp[i][j]=j 这里i随意,也就事实上也就意味着字符a可以无限的附加下去。因此我们做

一个转化,变成dp[i][j]= j + i +pa/pb 这里i+j>=k。

       我们来看这个式子,按照定义dp[i][j]停止后ab出现的期望,因为i+j>=k,因此只要出现一个b即会停止,因此我们

这里采用一个几何分布的期望,pa/pb代表着出现第一次出现b的时候,a的期望出现次数,然后再加上本来就有i个a,

因此最后的ab的数量就是 i+j+pa/pb。

       2. 由于b可以在第一个a出现之前无穷的出现,因此我们把最终求解的状态设置为 dp[1][0]。

       所以一开始就把 pa处理成 pa*(pa+pb)^-1 , 把pb处理成 pb*(pa+pb)^-1 以后直接这样计算即可。

Code:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int f[1001][1001];
ll inv(ll a)
{
	ll s=1;
	for(int n=mod-2;n;n>>=1)
	{
		if(n&1)s=s*a%mod;
		a=a*a%mod;
	}
	return s;
}
int main()
{
	int n,a,b;
	cin>>n>>a>>b;
	ll u=a*inv(a+b)%mod;
	ll v=(1-u+mod)%mod;
	ll c=u*inv(v)%mod;
	for(int i=n;i>0;i--)
		for(int j=n-1;j>-1;j--)
			f[i][j]=(j+i>=n?j+i+c:u*f[i+1][j]+v*f[i][j+i])%mod;
	printf("%d\n",f[1][0]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackflyDC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值