Codeforces New Year and Arbitrary Arrangement
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的更多相关文章
- 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)的可能在字符 ...
- [CodeForces]908D New Year and Arbitrary Arrangement
设状态f[i][j]表示有i个a,j个ab的期望 发现如果i+j>=k的话就再来一个b就行了. #include <iostream> #include <cstdio> ...
- Codeforces 908D New Year and Arbitrary Arrangement(概率DP,边界条件处理)
题目链接 Goodbye 2017 Problem D 题意 一个字符串开始,每次有$\frac{pa}{pa+pb}$的概率在后面加一个a,$\frac{pb}{pa+pb}$的概率在后面加一个 ...
- 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 ...
- CF 908D New Year and Arbitrary Arrangement——期望dp
题目:http://codeforces.com/contest/908/problem/D 注意是子序列.加一个a对ab个数无影响:加一个b使ab个数多出它前面的a那么多个.所以状态里记录有多少个a ...
- 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 ...
- 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 ...
随机推荐
- fiddler抓取手机端的数据流量包
1.首先下载安装fiddler 2.然后打开fiddler,进入到tools-->options-->connections 3.然后进入到https 4.设置完成后,查找本机ip 然后打 ...
- Qt 在相同的线程中可以在信号中传递未注册的元对象,在非相同线程中则不能传递未测试的对象,为什么呢?
有兄台知道可以在留言告诉我,万分感谢!!! 需求:需要在多线程中传递未注册的非元对象数据,时间紧急,无法及时更改该传递的数据为元对象,非继承 QObject 这里采用指针方式传递,同时把传递的局部变量 ...
- elasticsearch 分析器 分词器
参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html 在全文搜索(Fu ...
- 数据索引文件idx
数据索引文件idx 数据索引文件idx 数据索引文件idx
- oracle 数据库 锁
首先你要知道表锁住了是不是正常锁?因为任何DML语句都会对表加锁. 你要先查一下是那个会话那个sql锁住了表,有可能这是正常业务需求,不建议随便KILL session,如果这个锁表是正常业务你把se ...
- redis单节点安装及cluster的安装
单点安装 wget http://download.redis.io/releases/redis-4.0.2.tar.gz tar zxvf redis-4.0.1.tar.gz -C /usr/l ...
- 【JavaWeb项目】一个众筹网站的开发(三)第一个网页
一.bootstrap 本项目采用bootstrap3 bootstrap中文网 https://www.bootcss.com/ 使用bootstrap三步: 1.导入jQuery 2.导入boot ...
- BlueStore-先进的用户态文件系统《二》-BlueFS
https://zhuanlan.zhihu.com/p/46362124 简介 上一篇文章中,介绍了BlueStore的诞生背景.逻辑架构以及设计思想,提到了在BlueStore中元数据都是存放在R ...
- SCP-Py-002
项目编号:Py-002 项目等级:EuclidKeter 特殊收容措施: Py-002-1目前被映射在Researcher Kevin的服务器位于Site-Pyproject地下防无线电渗透室且被切断 ...
- mysql left join 用法说明
left join中关于where和on条件的几个知识点: 1.多表left join是会生成一张临时表,并返回给用户 2.where条件是针对最后生成的这张临时表进行过滤,过滤掉不符合where条件 ...