题目链接:http://codeforces.com/problemset/problem/1151/F
题目大意:
给定长度为 n 的 01 序列,可以对该序列操作 k 次,每次操作可以交换序列中任意两个元素的位置,求进行 k 次操作后 01 序列升序排列的概率。
分析:
- 交换后 j 减少,即 dp[i + 1][j - 1],必然是左边的 0 和右边的 1 交换,因此 dp[i + 1][j - 1] = dp[i][j] * j * (cnt_1 - cnt_0 + j)。
- 交换后 j 不变,即 dp[i + 1][j],有三种可能,(1)0 与 0 之间交换,有 $\binom{cnt_0}{2}$ 种;(2)1 与 1 之间交换,有 $\binom{cnt_1}{2}$ 种;(3)同一边的 0 与 1 之间交换,有 j * (cnt_0 - j) + (cnt_0 - j) * (cnt_1 - cnt_0 + j) 种;因此 dp[i + 1][j + 1] = dp[i][j] * ($\binom{cnt_0}{2}$ + $\binom{cnt_1}{2}$ + j * (cnt_0 - j) + (cnt_0 - j) * (cnt_1 - cnt_0 + j))。
- 交换后 j 增加,即 dp[i + 1][j + 1],必然是左边的 1 和右边的 0 交换,因此 dp[i + 1][j + 1] = dp[i][j] * (cnt_0 - j) * (cnt_0 - j)。
整理一下得到递推公式:dp[i][j] = dp[i - 1][j - 1] * (cnt_0 - j + 1) * (cnt_0 - j + 1)
+ dp[i - 1][j] * ($\binom{cnt_0}{2}$ + $\binom{cnt_1}{2}$ + j * (cnt_0 - j) + (cnt_0 - j) * (cnt_1 - cnt_0 + j))
+ dp[i - 1][j + 1] * (j + 1) * (cnt_1 - cnt_0 + j + 1)
设 para0(j) = (cnt_0 - j + 1) * (cnt_0 - j + 1)。
设 para1(j) = ($\binom{cnt_0}{2}$ + $\binom{cnt_1}{2}$ + j * (cnt_0 - j) + (cnt_0 - j) * (cnt_1 - cnt_0 + j))。
设 para2(j) = (j + 1) * (cnt_1 - cnt_0 + j + 1)。
于是 dp[i][j] = dp[i - 1][j - 1] * para0(j) + dp[i - 1][j] * para1(j) + dp[i - 1][j + 1] * para2(j)。
由于 i 只依赖 i - 1 以及与 i 无关的参数项,可以考虑矩阵快速幂求解,构造如下矩阵(以cnt_0 = 4 为例):
$$
X = \begin{bmatrix}
para1(0) & para0(1) & 0 & 0 & 0 \\
para2(0) & para1(1) & para0(2) & 0 & 0 \\
0 & para2(1) & para1(2) & para0(3) & 0 \\
0 & 0 & para2(2) & para1(3) & para0(4) \\
0 & 0 & 0 & para2(3) & para1(4)
\end{bmatrix} \tag{1}
$$
再构造如下答案矩阵:
$$
ans(i) = \begin{bmatrix}
dp[i][0] & dp[i][1] & dp[i][2] & dp[i][3] & dp[i][4]
\end{bmatrix} \tag{2}
$$
不难看出,ans(i) 与 X 有如下关系:
$$
ans(i) = ans(i - 1) * X \\
ans(i) = ans(0) * X^i
\tag{3}
$$
于是利用矩阵快速幂即可求出 P。
按照题目要求,再对 Q 用扩展欧几里得算法求一下逆元就差不多了。
代码如下:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Matrix{
int row, col;
LL MOD;
VVL mat; Matrix(int r = , int c = , LL p = mod) : row(r), col(c), MOD(p) {
mat.resize(r);
Rep(i, r) mat[i].resize(c, );
}
Matrix(const Matrix &x, LL p = mod) : MOD(p){
mat = x.mat;
row = x.row;
col = x.col;
}
Matrix(const VVL &A, LL p = mod) : MOD(p){
mat = A;
row = A.size();
col = A[].size();
} // x * 单位阵
inline void E(int x = ) {
assert(row == col);
Rep(i, row) mat[i][i] = x;
} inline VL& operator[] (int x) {
assert(x >= && x < row);
return mat[x];
} inline Matrix operator= (const Matrix &x) {
row = x.row;
col = x.col;
mat = x.mat;
return *this;
} inline Matrix operator= (const VVL &x) {
row = x.size();
col = x[].size();
mat = x;
return *this;
} inline Matrix operator+ (const Matrix &x) {
assert(row == x.row && col == x.col);
Matrix ret(row, col);
Rep(i, row) {
Rep(j, col) {
ret.mat[i][j] = mat[i][j] + x.mat[i][j];
ret.mat[i][j] %= MOD;
}
}
return ret;
} inline Matrix operator* (const Matrix &x) {
assert(col == x.row);
Matrix ret(row, x.col);
Rep(k, col) {
Rep(i, row) {
if(mat[i][k] == ) continue;
Rep(j, col) {
ret.mat[i][j] += mat[i][k] * x.mat[k][j];
ret.mat[i][j] %= MOD;
}
}
}
return ret;
} inline Matrix operator*= (const Matrix &x) { return *this = *this * x; }
inline Matrix operator+= (const Matrix &x) { return *this = *this + x; } inline void print() {
Rep(i, row) {
Rep(j, col) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
}; // 矩阵快速幂,计算x^y
inline Matrix mat_pow_mod(Matrix x, LL y) {
Matrix ret(x.row, x.col);
ret.E();
while(y){
if(y & ) ret *= x;
x *= x;
y >>= ;
}
return ret;
} // 扩展欧几里得求逆元
inline void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){
if (!b) {d = a, x = , y = ;}
else{
ex_gcd(b, a % b, y, x, d);
y -= x * (a / b);
}
} inline LL inv_mod(LL a, LL p = mod){
LL d, x, y;
ex_gcd(a, p, x, y, d);
return d == ? (x % p + p) % p : -;
} // Calculate x^y % p
inline LL pow_mod(LL x, LL y, LL p = mod){
LL ret = ;
while(y){
if(y & ) ret = (ret * x) % p;
x = (x * x) % p;
y >>= ;
}
return ret;
} LL n, k, P, Q;
int a[], cnt_0, cnt_1, cnt_00; int main(){
INIT();
cin >> n >> k;
For(i, , n) {
cin >> a[i];
a[i] ? ++cnt_1 : ++cnt_0;
}
For(i, , cnt_0) if(!a[i]) ++cnt_00; // 构造矩阵
Matrix ans(, cnt_0 + );
ans[][cnt_00] = ;
Matrix X(cnt_0 + , cnt_0 + );
Rep(j, cnt_0 + ) {
X[j][j] = (cnt_0 * (cnt_0 - ) / + cnt_1 * (cnt_1 - ) / + j * (cnt_0 - j) + (cnt_0 - j) * (cnt_1 - cnt_0 + j));
if(j > ) X[j - ][j] = (cnt_0 - j + ) * (cnt_0 - j + );
if(j < cnt_0) X[j + ][j] = (j + ) * (cnt_1 - cnt_0 + j + );
}
ans *= mat_pow_mod(X, k);
P = ans[][cnt_0];
Q = n * (n - ) / ;
Q = pow_mod(Q, k);
Q = inv_mod(Q); cout << (P * Q) % mod << endl;
return ;
}