CF1151FSonya and Informatics

给一个长度为 n$ (n\leq 100)$的 \(0/1\) 串,进行 k\((k \leq 10^9)\)次操作,每次操作选择两个位置 \((i,j)\)\((i < j)\),交换$ i,j$ 上的数,求 \(k\) 次操作后,该 \(0/1\) 串变成非降序列的概率,答案对 \(10^9+7\) 取模。

首先这道题目我们可以想出来一下比较朴素的DP

我们设\(sum_0\)为原列中\(0\)个数

我们发现我们不关心序列长什么样子

只关心前\(sum_0\)个数中\(0\)个个数

设\(f_{i,j}\)表示前\(i\)次操作之后,还有\(j\)个\(0\)的方案数

概率可以转化为

\[\frac{f_{k,sum_0}}{\sum_{i = 0}^{sum_0} f_{k,i}}
\]

转移就是

设\(c = \frac{n*(n - 1)}{2}\)表示总的可能情况

\[f_{i,j} += f_{i - 1,j - 1}+(sum_0 - (j - 1)) * (sum_0 - (j - 1))
\]

把一个前面的\(1\)换成\(0\)前部分有\(sum_0- (j - 1)\)个\(1\),后一部分有\(sum_0 - (j - 1)\)个\(0\),两两配对的方案数

\[f_{i,j} += f_{i - 1,j + 1} + (j + 1) * (sum_1 - (sum_0 - (j + 1)))
\]

把一个前面的\(0\)换成\(1\),组合意义类似上面

\[f_{i,j} += f_{i - 1,j} + (c - v_1 - v_2)
\]

\(v_1,v_2\)就是上面两个式子的后面部分

表示没有对前面的\(0\)的数目产生影响

这之后我们观察一下这个转移每次转移只和\(j\)有关

我们考虑使用矩阵去优化这个东西

\[\left[\begin{array}{ccccc}{f_{0}[0]} & {f_{-1}[1]} & {0} & {\dots} & {0} \\ {f_{+1}[0]} & {f_{0}[1]} & {f_{-1}[2]} & {\dots} & {0} \\ {0} & {f_{+1}[1]} & {f_{0}[2]} & {\dots} & {0} \\ {\dots} & {\dots} & {\dots} & {\dots} & {\dots}\\{0}&{0}&{0}&{\dots} &{f_0[n]}\end{array}\right]
\left[\begin{array}{c}{d p[0]} \\ {d p[1]} \\ {d p[2]} \\ {\cdots} \\ {d p[n]}\end{array}\right]=\left[\begin{array}{c}{d p[0]} \\ {d p[1]} \\ {d p[2]} \\ {\cdots} \\ {d p[n]}\end{array}\right]
\]

大约是这个样子

我们这样就完成了一次转移

\(k\)很大

我们直接上矩阵快速幂就可以了

\(\mathcal{O}\left(n^{3} \log k\right)\)

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#include<cmath>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std;
const int N = 105;
const LL mod = 1e9 + 7;
int n,k;
int a[N];
LL sum0,sum1;
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline LL up(LL x){
if(x >= mod) x -= mod;
return x;
}
inline LL down(LL x){
if(x < 0) x += mod;
return x;
}
struct Ma{
LL a[N][N];
LL *operator[](int i){return a[i];}
inline void clear(){memset(a,0,sizeof(a));}
friend Ma operator * (Ma x,Ma y){
Ma c;c.clear();
for(int i = 0;i <= n;++i)
for(int j = 0;j <= n;++j)
for(int k = 0;k <= n;++k)
c.a[i][j] = (c.a[i][j] + (x.a[i][k] * y.a[k][j])) % mod;
return c;
}
};
Ma s,ss;
inline Ma quick(Ma x,int y){
Ma res;
for(int i = 0;i <= n;++i) res[i][i] = 1;
while(y){
if(y & 1) res = res * x;
y >>= 1;
x = x * x;
}
return res;
}
Ma dp;
LL inv;
inline LL p3(LL x){
return x * (sum0 - (sum1 - x)) % mod;
}
inline LL p2(LL x){
LL v1 = sum1 * (sum1 - 1) / 2;
LL v2 = sum0 * (sum0 - 1) / 2;
LL v3 = x * (sum1 - x) % mod;
LL v4 = (sum1 - x) * (sum0 - (sum1 - x)) % mod;
return (v1 + v2 + v3 + v4) % mod;
}
inline LL p1(LL x){
return (sum1 - x) * (sum1 - x) % mod;
}
inline LL quickmul(LL x,LL y){
LL res = 1;
while(y){
if(y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int main(){
n = read(),k = read();
for(int i = 1;i <= n;++i){
a[i] = read() ^ 1;
sum0 += (a[i] == 0);
sum1 += (a[i] == 1);
}
LL t = 0;
for(int i = 1;i <= sum1;++i) t += (a[i] == 1);
dp[t][0] = 1;
for(int i = 0;i <= sum1;++i){
if(i > 0) ss.a[i][i - 1] = p1(i - 1);
ss.a[i][i] = p2(i);
if(i + 1 <= sum1) ss.a[i][i + 1] = p3(i + 1);
}
Ma ans = quick(ss,k) * dp;
Ma aa = quick(ss,k);
LL res = 0;
for(int i = 0;i <= sum1;++i) res = (res + ans[i][0]) % mod;
printf("%lld\n",ans[sum1][0] * quickmul(res,mod - 2) % mod);
return 0;
}

CF1151FSonya and Informatics的更多相关文章

  1. Python for Informatics 第11章 正则表达式六(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.7 调试 Python有一 ...

  2. Python for Informatics 第11章 正则表达式五(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.4 转义字符 之前我们在正 ...

  3. Python for Informatics 第11章 正则表达式四(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.3 组合查询和抽取 如果我 ...

  4. Python for Informatics 第11章 正则表达式三(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.2 用正则表达式抽取数据 ...

  5. Python for Informatics 第11章 正则表达式二(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.1 正则表达式的字符匹配 ...

  6. Python for Informatics 第11章 正则表达式一(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 目前为止,我们一直在通读文件,查 ...

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

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

  8. Protocol Informatics (PI项目)【基于网络轨迹的协议逆向工程文献学习】

    Protocol Informatics[基于网络轨迹的协议逆向工程文献学习]by tsy 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途.恕作者著作 ...

  9. Nastya Studies Informatics CodeForces - 992B (大整数)

    B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...

随机推荐

  1. day39-Spring 17-Spring的JDBC模板:完成增删改的操作

    JdbcTemplate根DBUtils非常类似,你要是有非常多的Dao,你每一个Dao都需要写它 /*在Dao层注入JDBC模板*/ private JdbcTemplate jdbcTemplat ...

  2. hdu1907 尼姆博弈

    尼姆博弈的性质. 最后一个取输.若a1^a2^a3...^a4=0表示利他态T,不然为利己态S.充裕堆:1个堆里的个数大于2.T2表示充裕堆大于等于2,T1表示充裕堆大于等于1,T0表示无充裕堆.S2 ...

  3. 洛谷P3286 [SCOI2014]方伯伯的商场之旅

    题目:洛谷P3286 [SCOI2014]方伯伯的商场之旅 思路 数位DP dalao说这是数位dp水题,果然是我太菜了... 自己是不可能想出来的.这道题在讲课时作为例题,大概听懂了思路,简单复述一 ...

  4. json 2016-09-18 22:03 207人阅读 评论(18) 收藏

    JSON:JavaScript 对象表示法(JavaScript Object Notation) JSON是什么? JSON(JavaScript Object Notation) 是一种轻量级的数 ...

  5. SQL if语句简要

    if语句 可以作为表达式用 可以在存储过程中作为流程控制语句用 表达式 IF(条件,条件true,条件false) 示例 sex字段m,f 互换 update salary set sex = if( ...

  6. 自定义View系列教程03--onLayout源码详尽分析

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

  7. Namenode文件系统命名空间映像文件及修改日志

  8. display:flex; justify-content:space-between; 最后一行显示内容无法靠左显示

    给父元素添加同每行展示列数一样(展示列表最多的)的子元素. 子元素设置样式: width:同子元素一样的width : height:0;

  9. html--多媒体文件

    添加多媒体文件 1.添加多媒体文件标记 <embed src="" width="" height=""></embed& ...

  10. 安装win下的Anaconda ----针对python3.6.4版本

    我的python版本是3.6.4, Anaconda下载地址: Anaconda官网:https://repo.anaconda.com/archive/ 清华大学镜像站:https://mirror ...