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. haproxy 在centos上cannot bind 端口的问题

    setsebool -P haproxy_connect_any=1 要不把selinux干掉也行

  2. Common Lisp学习资源整理

    Lisp Hackers: Interviews with 100x More Productive Programmers Posted on June 26th, 2013 Lisp Hacker ...

  3. 2019 The Preliminary Contest for ICPC China Nanchang National Invitational(A 、H 、I 、K 、M)

    A. PERFECT NUMBER PROBLEM 题目链接:https://nanti.jisuanke.com/t/38220 题意: 输出前五个完美数 分析: 签到.直接百度完美数输出即可 #i ...

  4. ES6+转ES5(webpack+babel、指定多个js文件、自动注入)

    接续上篇ES6+转ES5,本篇将使用webpack和babel将多个不同目录下指定的多个ES6+语法的js文件编译为ES5,并将编译后的文件配置注入对应的html文件. 所需环境node.npm.设置 ...

  5. IDC:助力转型、深入变革:中国云专业服务市场持续升温

    来源:IDC 北京,2019 年 10 月 24 日——随着云计算在中国市场更深入和广泛的应用,与云相关的专业服务(咨询.迁移.开发与建设服务)需求旺盛.IDC 首次发布的<2019 年上半年云 ...

  6. sqlserver日志处理不当而造成的隐患

    sqlserver日志处理不当而造成的隐患 事故背景:一大早还在路上,群里陆续有人反馈系统一直报错 “Unknown error 258”,后来查询日志发现错误日志 第一反应是不是数据库连接不够用了? ...

  7. Linux电源管理(7)_Wakeup events framework

    1. 前言 本文继续"Linux电源管理(6)_Generic PM之Suspend功能"中有关suspend同步以及PM wakeup的话题.这个话题,是近几年Linux ker ...

  8. DevOps工程师的成长路线图

    DevOps工程师的成长路线图 我们推崇的是 Reducing the gap between Devs and Operation teams. 来自kamranahmedse you built ...

  9. 备份下ESP8266的AT指令集手册和用例手册中文版,准备为V7做几个ESP8266的例子

    指令集手册:https://files.cnblogs.com/files/armfly/4a-esp8266_at_instruction_set_cn.rar 用例手册: https://file ...

  10. NLP入门(十一)从文本中提取时间

      在我们的日常生活和工作中,从文本中提取时间是一项非常基础却重要的工作,因此,本文将介绍如何从文本中有效地提取时间.   举个简单的例子,我们需要从下面的文本中提取时间: 6月28日,杭州市统计局权 ...