题目链接:http://codeforces.com/problemset/problem/1151/F

题目大意:

  给定长度为 n 的 01 序列,可以对该序列操作 k 次,每次操作可以交换序列中任意两个元素的位置,求进行 k 次操作后 01 序列升序排列的概率。

分析:

  每一次操作就是在 n 个数中选2个,因此有 $\binom{n}{2}$ 种,一共有 k 次操作,所以一共有 $\binom{n}{2}^{k}$ 种可能结果,即分母 Q。
  对于分子 P,设序列中 0 的个数为 cnt_0,设序列中 1 的个数为 cnt_1,设序列前 cnt_0 个数中0的个数为 cnt_00 ,定义 dp[i][j] 为在完成 i 次操作后,序列前 cnt_0 个数中有 j 个 0 的操作序列种数。那么 P = dp[k][cnt_0]。初始条件下 dp[0][cnt_00] = 1。
  对于 dp[i][j] 有如下三种操作情况:
 
  先放一下 dp[i][j] 时的直观图:
  说明:在 dp[i][j] > 0 时下面这张图一定画的出来,也就是 cnt_1 - cnt_0 + j 一定大于等于 0。如果 cnt_1 - cnt_0 + j < 0 ,则 dp[i][j] 必然等于 0 ,因为这样一种序列是不存在的。
  1. 交换后 j 减少,即 dp[i + 1][j - 1],必然是左边的 0 和右边的 1 交换,因此  dp[i + 1][j - 1] = dp[i][j] * j * (cnt_1 - cnt_0 + j)。
  2. 交换后 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))。
  3. 交换后 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 ;
}

CodeForces 1151F Sonya and Informatics的更多相关文章

  1. Codeforces 1151F Sonya and Informatics (概率dp)

    大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...

  2. 【CF1151F】Sonya and Informatics(动态规划,矩阵快速幂)

    [CF1151F]Sonya and Informatics(动态规划,矩阵快速幂) 题面 CF 题解 考虑一个暴力\(dp\).假设有\(m\)个\(0\),\(n-m\)个\(1\).设\(f[i ...

  3. Codeforces Round #553 F Sonya and Informatics

    题目 题目大意 给定一个长为 $n$($2 \le n \le 100$)的01串 $S$ .对 $S$ 进行 $k$($1 \le k \le 10^9$)次操作:等概率地选取两个下标 $i, j$ ...

  4. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  5. Codeforces 713A. Sonya and Queries

    题目链接:http://codeforces.com/problemset/problem/713/A 题意: Sonya 有一个可放置重复元素的集合 multiset, 初始状态为空, 现给予三种类 ...

  6. Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)

    [题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...

  7. Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  8. Codeforces C. Sonya and Problem Wihtout a Legend(DP)

    Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...

  9. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

随机推荐

  1. 如何在ASP.NET Core程序启动时运行异步任务(2)

    原文:Running async tasks on app startup in ASP.NET Core (Part 2) 作者:Andrew Lock 译者:Lamond Lu 在我的上一篇博客中 ...

  2. 【Android Studio安装部署系列】三十二、Android模拟器Genymotion安装使用教程详解

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 一.注册\登录 打开Genymotion官网,https://www.genymotion.com/ ,首先点击右上角的Sign in进行 ...

  3. okhttputils【 Android 一个改善的okHttp封装库】使用(二)

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 上一篇讲了如何在项目中导入OKHttputils库的操作,这一篇主要讲常见请求的写法. get请求 public String getPe ...

  4. Docker中完成Jenkins的安装

    去年就开始学习采用Docker+Jenkins+.Net Core搭建生成式流水线,一直拖到现在,也没有彻底的好好静下来去总结总结.趁着现在对自己的严格要求下,逐渐开始重视自我总结,以此来巩固逐渐失去 ...

  5. Asp.Net Core中服务的生命周期选项区别和用法

    在做一个小的Demo中,在一个界面上两次调用视图组件,并且在视图组件中都调用了数据库查询,结果发现,一直报错,将两个视图组件的调用分离,单独进行,却又是正常的,寻找一番,发现是配置依赖注入服务时,对于 ...

  6. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)解决办法

    一,报错原因及分析 mysql的这个报错的原因是mysql服务没有正确启动就是mysqld这个程序. mysql要想运行需要mysql和mysqld两个都启动才行 二,解决办法 右键我的电脑——> ...

  7. 从PRISM开始学WPF(三)Prism-Region-更新至Prism7.1

    [7.1update]在开始前,我们先看下版本7.1中在本实例中的改动. 首先,项目文件中没有了Bootstrapper.cs,在上一篇的末尾,我们说过了,在7.1中,不见推荐使用Bootstrapp ...

  8. [转] 以后再有人问你selenium是什么,你就把这篇文章给他

    本文转自:https://blog.csdn.net/TestingGDR/article/details/81950593 写在最前面:目前自动化测试并不属于新鲜的事物,或者说自动化测试的各种方法论 ...

  9. AJAX获取JSON WEB窗体代码

    1.添加引用 using System.Web.Services; 2.添加方法 [WebMethod] public static string getFoodClasses(int parentI ...

  10. 【Redis】LRU算法和Redis的LRU实现

    LRU原理 在一般标准的操作系统教材里,会用下面的方式来演示 LRU 原理,假设内存只能容纳3个页大小,按照 7 0 1 2 0 3 0 4 的次序访问页.假设内存按照栈的方式来描述访问时间,在上面的 ...