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. CodeForces - 1073D Berland Fair

    XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...

  2. html5 中video标签属性

      <video id="haoroomsvideo" src="haorooms.mp4" poster="images/haorooms.j ...

  3. 计算机等级考试真题1(JAVA)

    答案: 01-05 C D A A C   06-10 B/D    C C C B 11-15 A C A C A 16-20 C B     C    21-25 D D C D D 26-30 ...

  4. AWVS 10.5使用指南

    前言 AWVS是一款可与IBM AppScan比肩的.功能十分强大的Web漏洞扫描器.由Acunetix开发,官方站点提供了关于各种类型漏洞的解释和如何防范,具体参考:Acunetix Web Vul ...

  5. Supermap/Cesium 开发心得----获取三维视角的四至范围

    网上目前有两种获取当前Camera的四至范围的方法 方法一    这种方法是最通用的,即使在哥伦布视角(2.5D下依旧能准确获取值) function getCurrentExtent() { // ...

  6. python 虚拟环境安装与卸载

    Ubuntu16.04 安装 卸载 pip原创Solarzhou 发布于2019-06-12 21:50:28 阅读数 2001 收藏展开 实验环境Ubuntu16.04:VMware15: 问题描述 ...

  7. [AI开发]DeepStream开发填坑记录

    下面是在deepstream使用过程中碰到的一些坑: (1)Pipeline中的Sink如果需要编码存文件或者推rtmp的流,注意控制编码的参数,编码质量不要太高.否则可能Sink带不动,整个Pipe ...

  8. Android进程管理机制研究

    一.Linux中的进程管理在Linux中,进程是指处理器上执行的一个实例,可使用任意资源以便完成它的任务,具体的进程管理,是通过“进程描述符”来完成的,对应Linux内核中的task_struct数据 ...

  9. 精通awk系列(12):awk getline用法详解

    回到: Linux系列文章 Shell系列文章 Awk系列文章 getline用法详解 除了可以从标准输入或非选项型参数所指定的文件中读取数据,还可以使用getline从其它各种渠道获取需要处理的数据 ...

  10. 精通awk系列(5):BEGIN和END语句块

    回到: Linux系列文章 Shell系列文章 Awk系列文章 BEGIN和END语句块 awk的所有代码(目前这么认为)都是写在语句块中的. 例如: awk '{print $0}' a.txt a ...