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$. 求把原序列变成 ...
随机推荐
- C# 《编写高质量代码改善建议》整理&笔记 --(五)类型设计
1.区分接口和抽象类的应用场合 区别: ①接口支持多继承,抽象类则不能. ②接口可以包含方法,属性,索引器,事件的签名,但不能有实现,抽象类则可以. ③接口在增加新方法后,所有的继承者都必须重构,否则 ...
- SmartCode.ETL 这不是先有鸡还是蛋的问题!
继国庆节 SmartCode 正式版(SmartCode.Generator)发布之后,SmartCode 迎来了新的能力 SmartCode.ETL ! SmartCode 正式版从开始发布就从未说 ...
- Visual Studio 2019 正式发布,重磅更新,支持live share
如约而至,微软已于今天推出 Visual Studio 2019 正式版,一同发布的还有 Visual Studio 2019 for Mac. Visual Studio 2019 下载地址:htt ...
- 【设计模式+原型理解】第一章:使用Javascript来巧妙实现经典的设计模式
刚开始学习设计模式之前,我是没想说要学习设计模式的,我只是因为想学习JS中的原型prototype知识,一开始我想JS中为什么要存在原型这个东西?于是慢慢通过原型而接触到设计模式,后来发现我这个过程是 ...
- 【春华秋实】.NET Core之只是多看了你一眼
感官初体验 技术学习是一件系统性的事情,如果拒绝学习,那么自己就会落后以至于被替代..NET也是一样,当开源.跨平台成为主流的时候,如果再故步自封,等待.NET的就是死路一条,幸好.NET Core问 ...
- 前端知识复习: JS选中变色
前端知识复习:JS选中变色 上篇文章 :前端知识复习:Html DIV 图文混排(文字放在图片下边) Js选中图片效果 <!DOCTYPE html> <html xmlns=&qu ...
- java面试记录
怎么确保一个集合不能被修改 ArrayList<String> list = new ArrayList<>();list.add("x");Colle ...
- JVM的总结
1.JVM的内存模型 JVM主要由程序计数器,虚拟机栈,堆,方法区,本地方法区 1.程序计数器的功能是记录当前线程执行到了字节码文件的哪一行, JVM执行的是.java编译后的.class文件 2.虚 ...
- Android与js互相调用
有话要说: 本篇主要总结了简单的Android与js互相调用的方法. 在开发过程中遇到了需要在安卓中调用js方法的需求,于是将具体的实现过程总结成这篇博客. 效果: 其中“调用安卓方法”按钮是html ...
- 一些android开发实用性网站记录
android开发一些有用的网站有很多,可以方便我们开发,记录一下哈. 1.Android源代码在线阅读:https://www.androidos.net.cn/sourcecode 2.在线Jso ...