# 【CF908D New Year and Arbitrary Arrangement】

### 【Analysis】

$f_{i,j}$表示有$i$个A，$j$个AB期望长度。目标状态是$f_{1,0}$，因为$f_{0,0}$会转移自己。容易得到方程：
$f_{i,j}= \dfrac{pa}{pa+pb} \times f_{i-1,j} + \dfrac{pb}{pa+pb}\times f_{i,j+i}$

$x\gets P_b \times \sum\limits_{i=0}^{\infty}(c+i)\times P_a^i$

$S_n=\sum\limits_{i=0}^na_i$

$P_a \times S_n=\dfrac{pa}{pa+pb}\times \sum\limits_{i=0}^n(c+i)\times \dfrac{pa}{pa+pb}$

$=\sum\limits_{i=0}^n(c+i)\times (\dfrac{pa}{pa+pb})^{i+1}$

$\because P_a+P_b=1$

$\therefore x\gets \lim\limits_{n\to\infty}P_b\times S_n=\lim\limits_{n\to\infty}S_n-P_a\times S_n$
$=\lim\limits_{n\to\infty} \sum\limits_{i=0}^n(c+i)\times \dfrac{pa}{pa+pb}-\sum\limits_{i=0}^n(c+i)\times (\dfrac{pa}{pa+pb})^{i+1}$
$=\lim\limits_{n\to\infty} (c+0)\times P_a^0 + \sum\limits_{i=1}^n(c+i)\times P_a^i-\sum\limits_{i=1}^n(c+i-1)\times P_a^i +(c+n)\times P_a^{n+1}$
$=\lim\limits_{n\to\infty} c+\sum\limits_{i=1}^n P_a^i+(c+n)\times P_a^{n+1}$
$=\lim\limits_{n\to\infty} c+\dfrac{P_a-P_a^n\times P_a}{1-P_a}+(c+n)\times P_a^{n+1}$
$=\lim\limits_{n\to\infty} c+\dfrac{pa}{pb}\times (1-P_a^n)+(c+n)\times P_a^{n+1}$
$=\lim\limits_{n\to\infty} c+\dfrac{pa}{pb}+(c+n)\times P_a^{n+1}$
$=\lim\limits_{n\to\infty} c+\dfrac{pa}{pb}+\dfrac{c+n}{(\dfrac{pa+pb}{pa})^{n+1}}$
$\because (c+n)随着n的增大单调递增，(\dfrac{pa+pb}{pa})^{n+1}随着n的增大单调递增$
$\therefore 满足洛必达法则$
$根据洛必达法则，\dfrac{f(x)}{g(x)}如果在x=a处变成 (0/0)或是(\infty/\infty)，那么\dfrac{f(x)}{g(x)}=\dfrac{f^\prime(x)}{g^\prime(x)}$
$\therefore x\gets \lim\limits_{n\to\infty} c+\dfrac{pa}{pb}+\dfrac{c+n}{(\dfrac{pa+pb}{pa})^{n+1}}$
$=\lim\limits_{n\to\infty} c+\dfrac{pa}{pb}+\dfrac{1}{\ln(\dfrac{pa+pb}{pa})\times (\dfrac{pa+pb}{pa})^{n+1}}$
$=c+\dfrac{pa}{pb}$

### 【Code】

#include <bits/stdc++.h>
using namespace std;

typedef long long int64;
const int P = 1e9 + 7;
inline int qPow(int x, int y)
{
int res = 1, base = x;
while (y > 0)
{
if (y & 1)
res = 1ll * res * base % P;
base = 1ll * base * base % P;
y >>= 1;
}
return res;
}
inline int inv(int x) { return qPow(x, P - 2); }
inline int add(int x, int y) { x += y; x -= x >= P ? P : 0; return x; }
inline int mul(int x, int y) { return 1ll * x * y % P; }

int main(void)
{
int n = 1, a = 1, b = 1;
cin >> n >> a >> b;
const int A = 1ll * a * inv(a + b) % P;
const int B = (1 - A + P) % P;
const int V = 1ll * a * inv(b) % P;
vector <vector <int>> dp(n + 1);
for (int i = 1;i <= n; ++i)
dp[i].resize(n + 1);

for (int i = n;i >= 1; --i)
for (int j = n;j >= 0; --j)
dp[i][j] = (i + j >= n) ? (add(i, add(j, V))) : (add(mul(dp[i + 1][j], A), mul(dp[i][i + j], B)));
cout << dp[1][0] << '\n';
return 0;
}


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客