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 ...
随机推荐
- SVN 目录 定义
1.项目名称定义: 项目自然序号_项目名称_负责人名称 例如:23_测试项目_An 2.项目目录定义: 01_Source 注:01_Source 中包含 代码 和 DB 设计 02_Document ...
- Nhibernet Get方法获取数据后,修改字段,未保存,但是数据库的数据却同步了
首先,对象是在session中取得的,所以这个对象已经和数据库同步了,或者说相关联了如果你的session中的对象发生变法,提交事务后,数据库中的数据也会更新未保存更改,不要以为session就不会在 ...
- 2018-8-10-上传代码-CodePlex
title author date CreateTime categories 上传代码 CodePlex lindexi 2018-08-10 19:16:51 +0800 2018-2-13 17 ...
- signal - 有效信号的清单
描述 (DESCRIPTION) 下面 列出 Linux 支持的 信号. 某些 信号 依赖于 体系结构(architecture). 首先, POSIX.1 描述了 下列 信号. 信号 值 动作 说明 ...
- jquey弹出框demo
默认 $('#btn-01').click(function(){ $.dialog({ contentHtml : '<p>我是默认弹出对话框示例展示.我只是用来占位的内容展示,仅仅用来 ...
- disk或者Partition镜像的制作
备份镜像还原一般都是在client-server端这边才有涉及,不过作为平时爱折腾的咸鱼,表示偶尔玩玩这种操作也不错: 工具:pc X 1(装有 大白菜,装机吧,一类制作pe软件的即可,大同小异) ...
- linux(一)vi和vim
vi 多模式文本编辑器 多模式产生的原因 四种模式 正常模式 插入模式 命令模式 可视模式 vi man vi vim vim正常模式 直接vim回车,或vim空格文件名回车 i进入插入模式 I(sh ...
- 【leetcode】944. Delete Columns to Make Sorted
题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...
- 【和孩子一起学编程】 python笔记--第三天
第十章 游戏时间:Skier 首先安装pygame,直接在cmd命令控制框里键入pip install pygame就可以了 代码: import pygame, sys, random skier_ ...
- winform 控件拖拽和缩放
核心类: using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using ...