# CF908D New Year and Arbitrary Arrangement(期望 dp)

## 题目大意

Translated by yybyyb

## 数据范围

$1 \le k \le 1000$

## 解题思路

$f[x][y]$ 表示放了 x 个 a，获得了 y 个子序列的概率，枚举下一个放 a 还是 b 有

$f[x+1][y] += f[x][y] \times P_a\\ f[x][y+x] += f[x][y] \times P_b$

$E(x)=\sum_{i=0}P_a^i P_b\times(k+i)\\ =k+P_b\times\sum_{i=0}P_a^i\times i$

$\text{设 } T= \sum_{i=0}P_a^i\times i\\ E(x)=k+(1-P_a)T\\ P_aT=\sum_{i=1}P_a^i(i-1)\\ T-P_aT=\sum_{i=1}P_a^i=\frac {1}{1-P_a}-1=\frac {1}{P_b}-1\\ \therefore E(x)=k+(1-P_a)T=k-1+\frac {1}{P_b}$

/*
／＞　 フ
| 　_　 _|
／ミ _x 彡
/　　　 　 |
/　  ヽ　　 ?
／￣|　　 |　|　|
| (￣ヽ＿_ヽ_)_)
＼二つ
*/

#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;

template <typename T>
x = 0; bool f = 0;
char c = getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
for (;isdigit(c);c=getchar()) x=x*10+(c^48);
if (f) x=-x;
}

template<typename F>
inline void write(F x, char ed = '\n') {
static short st[30];short tp=0;
if(x<0) putchar('-'),x=-x;
do st[++tp]=x%10,x/=10; while(x);
while(tp) putchar('0'|st[tp--]);
putchar(ed);
}

template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }

template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }

const int P = 1e9 + 7;
ll fpw(ll x, ll mi) {
ll res = 1;
for (; mi; mi >>= 1, x = x * x % P)
if (mi & 1) res = res * x % P;
return res;
}

const int N = 2005;
ll k, pa, pb;
ll f[N][N];
int main() {
ll t = fpw(pa + pb, P - 2);
pa = pa * t % P, pb = pb * t % P;
f[1][0] = 1; ll ans = 0;
for (int i = 1;i < k; i++) {
for (int j = 0;j < k; j++) {
f[i+1][j] = (f[i+1][j] + f[i][j] * pa) % P;
if (j + i < k) f[i][j+i] = (f[i][j+i] + f[i][j] * pb) % P;
else ans = (ans + (j + i) * f[i][j] % P * pb) % P;
}
}
t = fpw(pb, P - 2) * pa % P;
for (int i = 0;i < k; i++)
ans = (ans + f[k][i] * (k + i + t)) % P;
write(ans);
return 0;
}
`
posted @ 2020-10-20 17:26  Hs-black  阅读(110)  评论(0编辑  收藏  举报