CodeForces 1151F Sonya and Informatics
题目链接: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 ;
}
CodeForces 1151F Sonya and Informatics的更多相关文章
- Codeforces 1151F Sonya and Informatics (概率dp)
大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...
- 【CF1151F】Sonya and Informatics(动态规划,矩阵快速幂)
[CF1151F]Sonya and Informatics(动态规划,矩阵快速幂) 题面 CF 题解 考虑一个暴力\(dp\).假设有\(m\)个\(0\),\(n-m\)个\(1\).设\(f[i ...
- 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$ ...
- Codeforces 714C. Sonya and Queries Tire树
C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...
- Codeforces 713A. Sonya and Queries
题目链接:http://codeforces.com/problemset/problem/713/A 题意: Sonya 有一个可放置重复元素的集合 multiset, 初始状态为空, 现给予三种类 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- 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 ...
- 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 ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
题目链接 Sonya and Problem Wihtout a Legend 题意 给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...
随机推荐
- 2.2Bind建立配置文件和实体的映射「深入浅出ASP.NET Core系列」
希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 新建MVC项目 这次我们没有使用控制台项目,而是使用mvc来测试. 如下图所示,选择空的项目,建完后,记得把项目设 ...
- Maven-常用插件
罗列笔者认为比较有用的一些maven打包插件,方便后续查阅 spring-boot-maven-plugin springboot自带的maven插件,可用于简单的JAR/WAR方式打包,官方地址为h ...
- Jquer + Ajax 制作上传图片文件
没什么 说的 直接 上代码 //选择图片并上传 function selectImg(node){ var f = node.value; var file = node.files[0]; if( ...
- 忘记Linux登录密码怎么办?
1.启动虚拟机,出现下面倒计时界面时,按e键.进入启动前编辑. 2.进入如下界面,再按e键. 3.进入如下页面后,选中第二项kernel开头的项,选中后再按e键. 4.进入如下界面后,在最后面输入空格 ...
- 2.python中self详解(程序适用于python3版本)
先介绍下Python中的类和实例面向对象最重要的概念就是类(class)和实例(instance),类(class)是抽象的模板,比如学生这个抽象的事物,可以用一个Student类来表示.而实例是根据 ...
- html meta标签使用及属性介绍
自学前端开始,我对meta标签接触不多,主要把精力都集中在能显示出来的标签上,比如span.button.h1等等.有时候去查看一些知名网站的源码,发现head标签里有一大摞的meta. 今天就来学习 ...
- 前端入门25-福音 TypeScript
声明 本篇内容摘抄自以下来源: TypeScript 中文网 只梳理其中部分知识点,更多更详细内容参考官网. 正文-TypeScript 今天来讲讲有 Java 基础转 JavaScript 的福音: ...
- 第十课html5 新增标签及属性 html5学习5
一.常用新增标签 1.header:定义页面的页眉头部 2.nav:定义导航栏 3.footer:定义页面底部,页脚 4.article:定义文章 5.section:定义区域 6.aside:定义侧 ...
- adb 查看 android手机的CPU架构
adb shell cat /proc/cpuinfo 当然要下载adb并配置好环境变量
- 从零开始搭建服务器部署web项目
前言 该教程旨在完整描述建站过程,会将博主踩过的坑尽量详细的罗列出来.整个建站流程相对较为庞大,因此做了分解,同时适合大家有针对性的查询感兴趣的部分. 一.如何拥有自己的云服务器. 二.域名的购买与解 ...