CF1151FSonya and Informatics
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\)的方案数
概率可以转化为
\]
转移就是
设\(c = \frac{n*(n - 1)}{2}\)表示总的可能情况
\]
把一个前面的\(1\)换成\(0\)前部分有\(sum_0- (j - 1)\)个\(1\),后一部分有\(sum_0 - (j - 1)\)个\(0\),两两配对的方案数
\]
把一个前面的\(0\)换成\(1\),组合意义类似上面
\]
\(v_1,v_2\)就是上面两个式子的后面部分
表示没有对前面的\(0\)的数目产生影响
这之后我们观察一下这个转移每次转移只和\(j\)有关
我们考虑使用矩阵去优化这个东西
\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的更多相关文章
- Python for Informatics 第11章 正则表达式六(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.7 调试 Python有一 ...
- Python for Informatics 第11章 正则表达式五(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.4 转义字符 之前我们在正 ...
- Python for Informatics 第11章 正则表达式四(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.3 组合查询和抽取 如果我 ...
- Python for Informatics 第11章 正则表达式三(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.2 用正则表达式抽取数据 ...
- Python for Informatics 第11章 正则表达式二(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.1 正则表达式的字符匹配 ...
- Python for Informatics 第11章 正则表达式一(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 目前为止,我们一直在通读文件,查 ...
- 【CF1151F】Sonya and Informatics(动态规划,矩阵快速幂)
[CF1151F]Sonya and Informatics(动态规划,矩阵快速幂) 题面 CF 题解 考虑一个暴力\(dp\).假设有\(m\)个\(0\),\(n-m\)个\(1\).设\(f[i ...
- Protocol Informatics (PI项目)【基于网络轨迹的协议逆向工程文献学习】
Protocol Informatics[基于网络轨迹的协议逆向工程文献学习]by tsy 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途.恕作者著作 ...
- Nastya Studies Informatics CodeForces - 992B (大整数)
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
随机推荐
- docker push dial tcp *.*.*.*:443 getsockopt: connection refused
docker 在提交镜像的时候出现以下错误. 我用的是本地的仓库,所以tcp后面是我的ip地址. 错误信息: #docker push ubuntu docker push dial tcp 192 ...
- 64位linux源码安装mysql
一:下载mysql http://dev.mysql.com/downloads/mysql/中的Generally Available(GA) Releases标签页,在MySQL Communit ...
- 【NS2】NS2修改MAC协议(转载)
NS2版本:2.34 涉及NS2代码文件: ns-2.34/mac/channel.h ns-2.34/mac/channel.cc ns-2.34/mac/wireless-phyExt.h n ...
- c++第四次作业:继承
继承与派生 基本概念和语法 概念 继承与派生是同一过程从不同角度看 保持已有的特性而构造新类的过程称为继承. 在已有类的基础上新增自己的特性而产生新类的过程为派生. 被继承的已有类称为基类(父类) 派 ...
- qt painter多个点的曲线
plot.h #ifndef PLOT_H #define PLOT_H #include<QTimer> #include <QWidget> class pathplot ...
- 洛谷P1164 小A点菜
//求方案数 定义状态f[i][j] 用前i件物品恰好放够体积为j的背包 方案数 #include<bits/stdc++.h> using namespace std; ; ; int ...
- oracle 用EXISTS替代IN
在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用EXISTS(或NOT EXISTS)通常将提高查询的效率. 低效: SELECT * FROM EMP ( ...
- 给tomcat容器配置SSL的记录,包含项目完整部署过程
给tomcat容器配置SSL(https) 昨天公司有一个旧的项目要部署, 服务器(OS是windows 10) 数据库都是新买的, 写个博客记录一下 1, 下载证书(以阿里云为例子) 参考链接: h ...
- java的System.currentTimeMillis()如何转换成C#的DateTime.Now.Ticks?
考虑到我们是东八时区的话,应做如下转换: long milli = System.currentTimeMillis() + 8*3600*1000; long ticks = (milli*1000 ...
- POJ 1961 Period 还是next数组的含义、
题意:求所给串的前缀(包括原串)中有多少循环串(子串长度至少要是周期的两倍) 思路:还是next数组的应用问题.如果不懂next数组的话 http://www.cnblogs.com/sasuke-/ ...