题目链接: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 用扩展欧几里得算法求一下逆元就差不多了。

代码如下:

  1. #pragma GCC optimize("Ofast")
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6. #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7. #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8. #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  10. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  11. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  12. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  13.  
  14. #define pr(x) cout << #x << " = " << x << " "
  15. #define prln(x) cout << #x << " = " << x << endl
  16.  
  17. #define LOWBIT(x) ((x)&(-x))
  18.  
  19. #define ALL(x) x.begin(),x.end()
  20. #define INS(x) inserter(x,x.begin())
  21.  
  22. #define ms0(a) memset(a,0,sizeof(a))
  23. #define msI(a) memset(a,inf,sizeof(a))
  24. #define msM(a) memset(a,-1,sizeof(a))
  25.  
  26. #define MP make_pair
  27. #define PB push_back
  28. #define ft first
  29. #define sd second
  30.  
  31. template<typename T1, typename T2>
  32. istream &operator>>(istream &in, pair<T1, T2> &p) {
  33. in >> p.first >> p.second;
  34. return in;
  35. }
  36.  
  37. template<typename T>
  38. istream &operator>>(istream &in, vector<T> &v) {
  39. for (auto &x: v)
  40. in >> x;
  41. return in;
  42. }
  43.  
  44. template<typename T1, typename T2>
  45. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  46. out << "[" << p.first << ", " << p.second << "]" << "\n";
  47. return out;
  48. }
  49.  
  50. typedef long long LL;
  51. typedef unsigned long long uLL;
  52. typedef pair< double, double > PDD;
  53. typedef pair< int, int > PII;
  54. typedef set< int > SI;
  55. typedef vector< int > VI;
  56. typedef map< int, int > MII;
  57. typedef vector< LL > VL;
  58. typedef vector< VL > VVL;
  59. const double EPS = 1e-;
  60. const int inf = 1e9 + ;
  61. const LL mod = 1e9 + ;
  62. const int maxN = 1e5 + ;
  63. const LL ONE = ;
  64. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  65. const LL oddBits = 0x5555555555555555;
  66.  
  67. struct Matrix{
  68. int row, col;
  69. LL MOD;
  70. VVL mat;
  71.  
  72. Matrix(int r = , int c = , LL p = mod) : row(r), col(c), MOD(p) {
  73. mat.resize(r);
  74. Rep(i, r) mat[i].resize(c, );
  75. }
  76. Matrix(const Matrix &x, LL p = mod) : MOD(p){
  77. mat = x.mat;
  78. row = x.row;
  79. col = x.col;
  80. }
  81. Matrix(const VVL &A, LL p = mod) : MOD(p){
  82. mat = A;
  83. row = A.size();
  84. col = A[].size();
  85. }
  86.  
  87. // x * 单位阵
  88. inline void E(int x = ) {
  89. assert(row == col);
  90. Rep(i, row) mat[i][i] = x;
  91. }
  92.  
  93. inline VL& operator[] (int x) {
  94. assert(x >= && x < row);
  95. return mat[x];
  96. }
  97.  
  98. inline Matrix operator= (const Matrix &x) {
  99. row = x.row;
  100. col = x.col;
  101. mat = x.mat;
  102. return *this;
  103. }
  104.  
  105. inline Matrix operator= (const VVL &x) {
  106. row = x.size();
  107. col = x[].size();
  108. mat = x;
  109. return *this;
  110. }
  111.  
  112. inline Matrix operator+ (const Matrix &x) {
  113. assert(row == x.row && col == x.col);
  114. Matrix ret(row, col);
  115. Rep(i, row) {
  116. Rep(j, col) {
  117. ret.mat[i][j] = mat[i][j] + x.mat[i][j];
  118. ret.mat[i][j] %= MOD;
  119. }
  120. }
  121. return ret;
  122. }
  123.  
  124. inline Matrix operator* (const Matrix &x) {
  125. assert(col == x.row);
  126. Matrix ret(row, x.col);
  127. Rep(k, col) {
  128. Rep(i, row) {
  129. if(mat[i][k] == ) continue;
  130. Rep(j, col) {
  131. ret.mat[i][j] += mat[i][k] * x.mat[k][j];
  132. ret.mat[i][j] %= MOD;
  133. }
  134. }
  135. }
  136. return ret;
  137. }
  138.  
  139. inline Matrix operator*= (const Matrix &x) { return *this = *this * x; }
  140. inline Matrix operator+= (const Matrix &x) { return *this = *this + x; }
  141.  
  142. inline void print() {
  143. Rep(i, row) {
  144. Rep(j, col) {
  145. cout << mat[i][j] << " ";
  146. }
  147. cout << endl;
  148. }
  149. }
  150. };
  151.  
  152. // 矩阵快速幂,计算x^y
  153. inline Matrix mat_pow_mod(Matrix x, LL y) {
  154. Matrix ret(x.row, x.col);
  155. ret.E();
  156. while(y){
  157. if(y & ) ret *= x;
  158. x *= x;
  159. y >>= ;
  160. }
  161. return ret;
  162. }
  163.  
  164. // 扩展欧几里得求逆元
  165. inline void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){
  166. if (!b) {d = a, x = , y = ;}
  167. else{
  168. ex_gcd(b, a % b, y, x, d);
  169. y -= x * (a / b);
  170. }
  171. }
  172.  
  173. inline LL inv_mod(LL a, LL p = mod){
  174. LL d, x, y;
  175. ex_gcd(a, p, x, y, d);
  176. return d == ? (x % p + p) % p : -;
  177. }
  178.  
  179. // Calculate x^y % p
  180. inline LL pow_mod(LL x, LL y, LL p = mod){
  181. LL ret = ;
  182. while(y){
  183. if(y & ) ret = (ret * x) % p;
  184. x = (x * x) % p;
  185. y >>= ;
  186. }
  187. return ret;
  188. }
  189.  
  190. LL n, k, P, Q;
  191. int a[], cnt_0, cnt_1, cnt_00;
  192.  
  193. int main(){
  194. INIT();
  195. cin >> n >> k;
  196. For(i, , n) {
  197. cin >> a[i];
  198. a[i] ? ++cnt_1 : ++cnt_0;
  199. }
  200. For(i, , cnt_0) if(!a[i]) ++cnt_00;
  201.  
  202. // 构造矩阵
  203. Matrix ans(, cnt_0 + );
  204. ans[][cnt_00] = ;
  205. Matrix X(cnt_0 + , cnt_0 + );
  206. Rep(j, cnt_0 + ) {
  207. X[j][j] = (cnt_0 * (cnt_0 - ) / + cnt_1 * (cnt_1 - ) / + j * (cnt_0 - j) + (cnt_0 - j) * (cnt_1 - cnt_0 + j));
  208. if(j > ) X[j - ][j] = (cnt_0 - j + ) * (cnt_0 - j + );
  209. if(j < cnt_0) X[j + ][j] = (j + ) * (cnt_1 - cnt_0 + j + );
  210. }
  211. ans *= mat_pow_mod(X, k);
  212. P = ans[][cnt_0];
  213. Q = n * (n - ) / ;
  214. Q = pow_mod(Q, k);
  215. Q = inv_mod(Q);
  216.  
  217. cout << (P * Q) % mod << endl;
  218. return ;
  219. }

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. C# 《编写高质量代码改善建议》整理&笔记 --(五)类型设计

    1.区分接口和抽象类的应用场合 区别: ①接口支持多继承,抽象类则不能. ②接口可以包含方法,属性,索引器,事件的签名,但不能有实现,抽象类则可以. ③接口在增加新方法后,所有的继承者都必须重构,否则 ...

  2. SmartCode.ETL 这不是先有鸡还是蛋的问题!

    继国庆节 SmartCode 正式版(SmartCode.Generator)发布之后,SmartCode 迎来了新的能力 SmartCode.ETL ! SmartCode 正式版从开始发布就从未说 ...

  3. Visual Studio 2019 正式发布,重磅更新,支持live share

    如约而至,微软已于今天推出 Visual Studio 2019 正式版,一同发布的还有 Visual Studio 2019 for Mac. Visual Studio 2019 下载地址:htt ...

  4. 【设计模式+原型理解】第一章:使用Javascript来巧妙实现经典的设计模式

    刚开始学习设计模式之前,我是没想说要学习设计模式的,我只是因为想学习JS中的原型prototype知识,一开始我想JS中为什么要存在原型这个东西?于是慢慢通过原型而接触到设计模式,后来发现我这个过程是 ...

  5. 【春华秋实】.NET Core之只是多看了你一眼

    感官初体验 技术学习是一件系统性的事情,如果拒绝学习,那么自己就会落后以至于被替代..NET也是一样,当开源.跨平台成为主流的时候,如果再故步自封,等待.NET的就是死路一条,幸好.NET Core问 ...

  6. 前端知识复习: JS选中变色

    前端知识复习:JS选中变色 上篇文章 :前端知识复习:Html DIV 图文混排(文字放在图片下边) Js选中图片效果 <!DOCTYPE html> <html xmlns=&qu ...

  7. java面试记录

    怎么确保一个集合不能被修改   ArrayList<String> list = new ArrayList<>();list.add("x");Colle ...

  8. JVM的总结

    1.JVM的内存模型 JVM主要由程序计数器,虚拟机栈,堆,方法区,本地方法区 1.程序计数器的功能是记录当前线程执行到了字节码文件的哪一行, JVM执行的是.java编译后的.class文件 2.虚 ...

  9. Android与js互相调用

    有话要说: 本篇主要总结了简单的Android与js互相调用的方法. 在开发过程中遇到了需要在安卓中调用js方法的需求,于是将具体的实现过程总结成这篇博客. 效果: 其中“调用安卓方法”按钮是html ...

  10. 一些android开发实用性网站记录

    android开发一些有用的网站有很多,可以方便我们开发,记录一下哈. 1.Android源代码在线阅读:https://www.androidos.net.cn/sourcecode 2.在线Jso ...