Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)

题目链接:New Year and Arbitrary Arrangement

题意:  

 有一个ab字符串,初始为空。 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾添加字母b,当出现≥k个ab子串时立即停止添加字母,求最后期望的ab子串个数。(子串ab不要求连续) 例子:当k=1,aab含2个ab,bbabbab时不可能出现的,因为到了bbab就会停止添加字母。

题解: 期望DP

  DP果然是智商的分界线 orz @。@#,这题题意其实我也没看太懂,后来看了别人的博客才勉强写出来。。。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAX_N = 5e3+9;
 4 const int MOD = 1e9+7;
 5 long long k,pa,pb;
 6 int DP[MAX_N][MAX_N];
 7 long long quick_mod(long long x,int p)
 8 {
 9     long long ans = 1;
10     long long base = x;
11     while(p)
12     {
13         if(p&1) ans = ans*base %MOD;
14         p>>=1;
15         base = (base*base)%MOD;
16     }
17     return ans;
18 }
19 long long inv(long long x)
20 {
21     return quick_mod(x,MOD-2);
22 }
23 long long make_DP(long long i,long long j)
24 {
25     if(i+j>=k)
26         return (i+j+pa*inv(pb)%MOD)%MOD;
27     if(DP[i][j] != -1)
28         return DP[i][j];
29     return DP[i][j] = ((pa*make_DP(i+1,j)%MOD) + (pb*make_DP(i,i+j)%MOD))*inv(pa+pb) %MOD;
30 }
31 int main()
32 {
33     while(cin>>k>>pa>>pb)
34     {
35         memset(DP,-1,sizeof(DP));
36         //cout<<"......"<<DP[2][0]<<endl;
37         cout<<make_DP(1,0)<<endl;
38     }
39     return 0;
40 }

  

原文地址:https://www.cnblogs.com/doggod/p/8410651.html

时间: 2024-08-08 07:41:04

Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)的相关文章

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 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

Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp

一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\) 表示第 \(i\) 个格子期望经过多少次,所以 \(dp_{n+1}=1\). https://www.cnblogs.com/suncongbo/p/11996219.html 原文地址:https://www.cnblogs.com/KisekiPurin2019/p/12063633.ht

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[

CodeForces 757D Felicity&#39;s Big Secret Revealed(状压DP)

题意:给定一个01串,一个有效的n切割定义如下:一个横杠代表一次切割,第一条横杠前面的01串不算,最后一条横杠后面的01串不算,将两个横杠中的01串转化成十进制数字,假设这些数字的最大值是MAX且这些数字囊括了1-MAX的所有数字,则称为一次有效切割.求2~n+1次有效切割的切法. 思路: 由于题目要求包含所有1-MAXN的数字,且n<=75,所以MAXN<=20.另dp[i][j]表示第i位前面有一个横杆且存在j这个状态,接着从第i位开始枚举到第j位为下一个横杆的位置,设这两段横杆之间的数字

codeforces 453 B Little Pony and Harmony Chest (状压dp)

题目大意: 需要你构造一个b数组.使得b数组中的所有元素互质. 而且使得b数组与a数组中的每个对应下标元素的差值和最小. 思路分析: 考虑到 a中所有元素都是 0 - 30. 所以b中的元素也只可能在 0 - 59. 因为如果b 选择60的话,结果和1是一样的,而且b序列中 1 可以重复出现很多次. 因为gcd (1,x) = 1.. 所以们首先把2 - 59中的所有素数处理出来,只有17个. 然后状压这些素数因子. dp[i] [s] [0] 表示 递推到第 i 个位置 达到素数因子为s的状态

[Codeforces Round #146 (Div. 1) B]Let&#39;s Play Osu!(期望Dp)

Description You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be

[Codeforces 464D]World of Darkraft(期望DP)

[Codeforces 464D]World of Darkraft(期望DP) 题面 游戏中有k种装备,每种装备初始时都是等级1.zyd每打一只怪,就会随机爆出一件装备.掉落和更新装备方式如下: 假设这种装备当前等级为t,那么系统会在[1,t+1]中等概率随机出该装备的等级.爆出装备后,会装备上身上的和爆出的装备之间等级更高的那件,并卖掉等级更低的装备.其中等级为i的装备价格为i金币. 求打了n只怪后获得金币的期望值,精确到\(10^{-9}\). \(n,k\leq 10^5\) 分析 首先

Codeforces 908D New Year and Arbitrary Arrangement(概率DP,边界条件处理)

题目链接  Goodbye 2017 Problem D 题意  一个字符串开始,每次有$\frac{pa}{pa+pb}$的概率在后面加一个a,$\frac{pb}{pa+pb}$的概率在后面加一个$b$. 求当整个串中有至少$k$个$ab$的时候(不需要连续,下同),字符串中$ab$个数的期望. 设$f[i][j]$为字符串有$i$个$a$,$j$个$ab$的时候字符串中$ab$个数的期望 设$p = \frac{pa}{pa+pb}$, $q = \frac{pb}{pa+pb}$ 那么对

【CF908D】New Year and Arbitrary Arrangement

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 Ra