Sonya and Informatics

https://codeforces.com/contest/1151/problem/F

题意:给定一个01串,每次取出一组(l,r)1≤l<r≤n,交换s[l],s[r],求经过k次之后变成升序的概率是多大?

题解:构造矩阵+矩阵快速幂

现在我们知道有sn snsn个1和n−sn n-snn−sn个0,我们还知道多少个1在它的位置了,
共有四种0和1,
1 在规定位置,不在规定位置
0 在规定位置 不在规定位置
每一次交换可能引起的变化有三种:不变,+1,-1
目的是1全在规定位置,按照取出l,r列出转移矩阵就ok了

参考文章:题解 构造矩阵 矩阵快速幂

构造矩阵我看不懂,感觉还有一点DP的思想???

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,p,l,r,u,v;
int cnt,flag,temp,sum;
int a[N];struct Matrix{int n;Matrix(int nn = 1):n(nn){ memset(a,0,sizeof(a));};long long a[N][N];void print(){for(int i = 0;i <= n; ++i)for(int j= 0;j <= n; ++j)printf("%I64d%c",a[i][j]," \n"[j==n]);}Matrix operator*(const Matrix &b)const{Matrix c(n);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] += a[i][k] * b.a[k][j];c.a[i][j] %= MOD;}}}//c.print();return c;}
};
Matrix ans,fac;
ll POW(ll a,ll b,ll c){ll res=1;ll base=a%c;while(b){if(b&1)res=(res*base)%c;base=(base*base)%c;b>>=1;}return res;
}void MatrixPOW(ll k){while(k){if(k&1)ans=ans*fac;fac=fac*fac;k>>=1;}
}
void init(){cnt=0;for(int i=0;i<n;i++) cnt += a[i]==1;int now = 0;for(int i = n-cnt;i < n;++i)now += a[i]==1;ans.n =  fac.n  = cnt;ans.a[0][now] = 1;for(int i = 0;i <= cnt; ++i){// 四种情况 	1	:i,sn-i// 		    0	:sn-i,n-i-sn+i-sn+i = n-2*sn+ifac.a[i][i] = n*(n-1)/2-(cnt-i)*(cnt-i)-i*(n-2*cnt+i);// 保持原状if(i+1<=cnt)fac.a[i][i+1] = (cnt-i)*(cnt-i);// 加1if(i)fac.a[i][i-1] = i*(n-2*cnt+i); // 减1}
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//while(t--){scanf("%I64d%I64d",&n,&k);for(int i=0;i<n;i++)scanf("%d",&a[i]);init();//ans.print();//fac.print();MatrixPOW(k);//ans.print();cout<<ans.a[0][cnt]*POW(POW(n*(n-1)/2,k,MOD),MOD-2,MOD)%MOD<<endl;//}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif//cout << "Hello world!" << endl;return 0;
}

 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注