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) , B = pb / (pa + pb)
那么 f[i][j] = A * f[i+1][j] + B * f[i][i+j]
当 i+j >= k 时,再出现一个 b 就会结束,所以此时:
f[i][j] = f[i][i+j] * B + f[i+1][i+j+1] * A * B + f[i+2][i+j+2] * A2 * B + ... + f[i+∞][i+j+∞] * A∞ * B
化简一番(等比数列求和,1 - A = B)得到 f[i][j] = i + j + A / B
于是就可以做了;
不会刷表就记忆化搜索...
注意取的答案是 dp(1,0) 而非 dp(0,0),因为 dp(0,0) 会一直调用自己,而 dp(1,0) 可以看做是从第一个 a 出现开始算,反正前面没有 a ,对答案无影响。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=,mod=1e9+;
int k,a,b,c,f[maxn*][*maxn];
bool vis[maxn][maxn];
int pw(int a,int b)
{
int ret=;
for(;b;b>>=,a=((ll)a*a)%mod)
if(b&)ret=((ll)ret*a)%mod;
return ret;
}
int dp(int i,int j)
{
if(vis[i][j])return f[i][j];
vis[i][j]=;
if(i+j>=k)return f[i][j]=(i+j+c)%mod;
f[i][j]=((ll)a*dp(i+,j)+(ll)b*dp(i,i+j))%mod;
return f[i][j];
}
int main()
{
int pa,pb;
scanf("%d%d%d",&k,&pa,&pb);
int tmp=pw(pa+pb,mod-);
a=(ll)pa*tmp%mod; b=(ll)pb*tmp%mod;
c=(ll)a*pw(b,mod-)%mod;
// for(int i=0;i<=2*k;i++)
// for(int j=max(k-i,0);j<=2*k;j++)
// f[i][j]=f[j][i]=(i+j+c)%mod;
// for(int i=k;i>=0;i--)
// for(int j=k;j>=0;j--)
// f[i][j]=(a*f[i+1][j]+b*f[i][i+j])%mod;
printf("%d\n",dp(,));//0,0 会调用自己
//如果不是从第一个a算起,前面一堆b没用
return ;
}
CF 908 D New Year and Arbitrary Arrangement —— 期望DP的更多相关文章
- CF 908D New Year and Arbitrary Arrangement——期望dp
题目:http://codeforces.com/contest/908/problem/D 注意是子序列.加一个a对ab个数无影响:加一个b使ab个数多出它前面的a那么多个.所以状态里记录有多少个a ...
- CF908D New Year and Arbitrary Arrangement(期望Dp+数学)
题目大意:给你一个空字符串,你有\(\frac{pa}{pa+pb}\)的概率往字符串最后面加个\(a\),\(\frac{pb}{pa+pb}\)的概率往字符串最后面加个\(b\),当子序列\(ab ...
- $CF908D\ New\ Year\ and\ Arbitrary\ Arrangement$ 期望$dp$
正解:期望$dp$ 解题报告: 传送门$QwQ$ 阿关于题目里那个形如$ab$的子序列我说下,,,我我我之前$get$了好久$QAQ$.这里子序列的个数的定义是这样儿的,举个$eg$,$aabb$,就 ...
- 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 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 ...
- CF908D New Year and Arbitrary Arrangement 期望、DP
题目传送门 题意:给出正整数$pa,pb,k$,最开始你有一个空串,每一次你有$\frac{pa}{pa + pb}$的概率向串最后放一个$a$,有$\frac{pb}{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 New Year and Arbitrary Arrangement
New Year and Arbitrary Arrangement time limit per test2 seconds You are given three integers k, pa a ...
随机推荐
- STM32——通用定时器基本定时功能
STM32——————通用定时器基本定时功能 1. ...
- Inspector's Dilemma(欧拉通路)
In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directio ...
- jquery源码——noConflict实现
实现方式很简单:在初始化的时候,记录当前全局中jQuery和$两个变量的的值,用_jQuery和_$分别存放,调用noConflict方法时,使用_jQuery和_$分别恢复对应的值,并且返回jQue ...
- Linux 文件与目录结构
[Linux文件] Linux 系统中一切皆文件. [Linux目录结构] --/bin 是Binary的缩写, 这个目录存放着最经常使用的命令. --/sbin s就是Super User的意思,这 ...
- sql判断以逗号分隔的字符串中是否包含某个字符串--------MYSQL中利用select查询某字段中包含以逗号分隔的字符串的记录方法
sql判断以逗号分隔的字符串中是否包含某个字符串---------------https://blog.csdn.net/wttykj/article/details/78520933 MYSQL中利 ...
- [HDU2196]Computer(DP)
传送门 题意 给出一棵树,求离每个节点最远的点的距离 思路 对于我这种菜鸡,真是难啊. 每个点的距离它最远的点,除了在它子树中的,还有在它子树之外的,所以这几个状态都得表示出来. 我们能够很简单的求出 ...
- noip模拟赛 道路分组
分析:因为每一组编号都是连续的嘛,所以能分成一组的尽量分,每次加边后dfs判断一下1和n是否连通.有向图的判连通没有什么很快的方法,特别注意,并查集是错的!这个算法可以得到60分. 事实上每一次都不需 ...
- PHP $_SERVER的使用
常常会用到php的$_SERVER变量,可是好多常用的参数又不熟每次都去查手册.为了记住一些常用的,写个日志吧.前导:网站根目录:/www/domain.com/访问Url:http://www.do ...
- [NOIP2007] 提高组 洛谷P1099 树网的核
题目描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各边长度的集合,并 ...
- IntentService用于服务中开启子线程的自动关闭
package com.pingyijinren.test; import android.app.IntentService; import android.content.Intent; impo ...