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

Range

\(k\le1000,p_a,p_b\le10^6\)

Algorithm

\(DP\),概率与期望

Mentality

设 \(f_{i,j}\) 表示当前有 \(i\) 个 \(a\) ,\(j\) 个子序列 \(ab\) ,在整个序列结束时的子序列 \(ab\) 的期望个数。发现第一维可能无限大,考虑倒推。

\(f_{i,j}\) 的转移有两种情况,一是在末尾加入 \(a\) ,转移至 \(f_{i+1,j}\) ,而是加入 \(b\) 转移至 \(f_{i,j+i}\) 。那么倒推的方程就很明显了:

\[f_{i,j}=\frac{p_a}{p_a+p_b}f_{i+1,j}+\frac{p_b}{p_a+p_b}f_{i,j+1}
\]

不过第一维无限大的问题还是没解决,必须考虑边界的问题。

我们发现,当 \(i+j\ge k\) 的时候,如果我们加入 \(b\) ,则整个串就会立即终止。

那么对于一个状态 \(f_{i,j},(i+j\ge k)\) 来说,设在此状态上连续加入 \(x\) 个 \(a\) 再加入一个 \(b\) ,则:

\[f_{i,j}=\frac{p_b}{p_a+p_b}\sum_{x=0}^\infty(i+j+x)(\frac{p_a}{p_a+p_b})^x
\]

这是一个等比数列,那么我们直接等比数列求和就好了。

算出来得到:

\[f_{i,j}=i+j+\frac{p_a}{p_b}
\]

直接记搜就星了。

Code

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) w = ch == '-' ? -1 : 1, ch = getchar();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
const int Max_n = 1e3 + 5, mod = 1e9 + 7;
int n, a, b;
int pa, pb, pp;
int f[Max_n][Max_n];
int ksm(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) res = 1ll * res * a % mod;
return res;
}
int DP(int a, int ab) {
int &res = f[a][ab];
if (res != -1) return res;
if (a + ab >= n) {
res = (a + ab + pp) % mod;
return res;
}
return res =
(1ll * pa * DP(a + 1, ab) % mod + 1ll * pb * DP(a, ab + a) % mod) %
mod;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D.in", "r", stdin);
freopen("D.out", "w", stdout);
#endif
n = read(), a = read(), b = read();
pa = 1ll * a * ksm(a + b, mod - 2) % mod;
pb = 1ll * b * ksm(a + b, mod - 2) % mod;
pp = 1ll * a * ksm(b, mod - 2) % mod;
memset(f, -1, sizeof(f));
printf("%d\n", DP(1, 0));
}

【CF908D】New Year and Arbitrary Arrangement的更多相关文章

  1. 论文阅读(Xiang Bai——【CVPR2012】Detecting Texts of Arbitrary Orientations in Natural Images)

    Xiang Bai--[CVPR2012]Detecting Texts of Arbitrary Orientations in Natural Images 目录 作者和相关链接 方法概括 方法细 ...

  2. 【CodeForces】908 D. New Year and Arbitrary Arrangement

    [题目]Good Bye 2017 D. New Year and Arbitrary Arrangement [题意]给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符 ...

  3. CSU 1997: Seating Arrangement【构造】

    1997: Seating Arrangement Description Mr. Teacher老师班上一共有n个同学,编号为1到n. 在上课的时候Mr. Teacher要求同学们从左至右按1, 2 ...

  4. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

  5. 论文阅读(Xiang Bai——【TIP2014】A Unified Framework for Multi-Oriented Text Detection and Recognition)

    Xiang Bai--[TIP2014]A Unified Framework for Multi-Oriented Text Detection and Recognition 目录 作者和相关链接 ...

  6. 论文阅读(Weilin Huang——【TIP2016】Text-Attentional Convolutional Neural Network for Scene Text Detection)

    Weilin Huang--[TIP2015]Text-Attentional Convolutional Neural Network for Scene Text Detection) 目录 作者 ...

  7. 【RobotFramework】Selenium2Library类库关键字使用说明

    Add CookieArguments:[ name | value | path=None | domain=None | secure=None | expiry=None ]Adds a coo ...

  8. 【LA2796】Concert Hall Scheduling(最大费用最大流)

    Description You are appointed director of a famous concert hall, to save it from bankruptcy. The hal ...

  9. 【HDU1538】A Puzzle for Pirates(经典的海盗问题)

    [题目] Description A bunch of pirates have gotten their hands on a hoard of gold pieces and wish to di ...

随机推荐

  1. GoLand相同目录(包)下方法调用

    之前写的python,摸索go的时候发现相同目录下写的方法调用,在编译时竟然会报错,Mark~ 相同目录下方法调用,main.go文件调用tool.go中的add方法,要点如下: 1.两个文件的pak ...

  2. 抓包工具Charles的使用说明

    Charles介绍&前期准备: Charles介绍:Charles跟fiddler是目前行业中比较常用的抓包工具,这里主要介绍Charles的使用,fiddler的使用类似. 前期准备:使用前 ...

  3. [ASP.NET Core 3框架揭秘] 跨平台开发体验: Linux

    如果想体验Linux环境下开发.NET Core应用,我们有多种选择.一种就是在一台物理机上安装原生的Linux,我们可以根据自身的喜好选择某种Linux Distribution,目前来说像RHEL ...

  4. 【CV现状-2】三维感知

    #磨染的初心--计算机视觉的现状 [这一系列文章是关于计算机视觉的反思,希望能引起一些人的共鸣.可以随意传播,随意喷.所涉及的内容过多,将按如下内容划分章节.已经完成的会逐渐加上链接.] 缘起 三维感 ...

  5. BOM对象——Location

    BOM对象--location <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

  6. C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    有人在社区问到:C#调用Oracle中自定义函数的返回值时,无法正常调用.但在PL/SQL中正常调用返回. 于是动手一试: 1.准备函数(Oralce 11g.2.0.0.4) CREATE OR R ...

  7. 初学Manjaro

    近期,看有朋友在使用Manjaro系统,感觉这个系统挺不错,于是我也安装了Manjaro,学习一下!!!! Manjaro下载地址:https://manjaro.org/download/ Manj ...

  8. 集合系列 Queue(十):LinkedList

    我们之前在说到 List 集合的时候已经说过 LinkedList 了.但 LinkedList 不仅仅是一个 List 集合实现,其还是一个双向队列实现. public class LinkedLi ...

  9. IT兄弟连 HTML5教程 CSS3属性特效 CSS3分栏布局

    CSS3中新出现的多列布局(multi-column)是传统HTML网页中块状布局模式的有力扩充.这种新语法能够让WEB开发人员轻松的让文本呈现多列显示.我们知道,当一行文字太长时,读者读起来就比较费 ...

  10. Get提交数据过大,tomcat报错,信息: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level

    原因 get提交的数据过大,超过了默认值. 解决办法 get提交配置 设置tomcat的server.xml.找到我们启动的端口,一般是8080,增加maxHttpHeaderSize= " ...