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 ...
随机推荐
- 集合--Set&&HashSet和TreeSet
特点:元素无序,不可重复 1.添加元素 set.add("tanlei"); set.addAll(Arrays.asList(44,"磊","磊&q ...
- Python学习之路9☞面向对象的程序设计
一 面向对象的程序设计的由来 见概述:http://www.cnblogs.com/linhaifeng/articles/6428835.html 二 什么是面向对象的程序设计及为什么要有它 面向过 ...
- python 顺序传入
- 手动实现一个form组件
最近研究了一下element-ui,想着手动实现一下里面的form组件,贴个组件里面的代码 <el-form :model="ruleForm" status-icon :r ...
- laravel中如何实现验证码验证及使用
开发环境: laravel5.5 php7.1.11 mysql 验证码 是防止恶意破解密码.刷票.论坛灌水.刷页的手段.验证码有 多种类型. 现在我给大家实现如何使用图片验证码,其原理是让用户输入一 ...
- js+canvas制作前端验证码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 2018-9-4-Roslyn-通过-nuget-统一管理信息
title author date CreateTime categories Roslyn 通过 nuget 统一管理信息 lindexi 2018-09-04 08:55:19 +0800 201 ...
- HDFS概念名称节点和数据节点-名称节点
- 我钟爱的HTML5和CSS3在线工具【转】
我真的喜欢上了HTML5, CSS3, JavaScript编程,但是有一些代码还是需要一些辅助工具来做才行,例如,CSS3的Gradient渐变如果手写代码的话真的不爽,还有像animation动画 ...
- oracle 用EXISTS替代IN
在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用EXISTS(或NOT EXISTS)通常将提高查询的效率. 低效: SELECT * FROM EMP ( ...