CH3602 Counting Swaps
3602 Counting Swaps 0x30「数学知识」例题
背景
https://ipsc.ksp.sk/2016/real/problems/c.html
Just like yesterday (in problem U of the practice session), Bob is busy, so Alice keeps on playing some single-player games and puzzles. In her newest puzzle she has a permutation of numbers from 1 to n. The goal of the puzzle is to sort the permutation using the smallest possible number of swaps.
Instead of simply solving the puzzle, Alice is wondering about the probability of winning it just by playing at random. In order to answer this question, she needs to know the number of optimal solutions to her puzzle.
描述
You are given a permutation p1, …, pn of the numbers 1 through n. In each step you can choose two numbers x < y and swap px with py.
Let m be the minimum number of such swaps needed to sort the given permutation. Compute the number of different sequences of exactly m swaps that sort the given permutation. Since this number may be large, compute it modulo 109 + 9.
输入格式
The first line of the input file contains an integer t specifying the number of test cases. Each test case is preceded by a blank line.
Each test case consists of two lines. The first line contains the integer n. The second line contains the sequence p1, …, pn: a permutation of 1, …, n.
In the easy subproblem C1, 1 ≤ n ≤ 10.
In the hard subproblem C2, 1 ≤ n ≤ 105.
输出格式
For each test case, output a single line with a single integer: x xmod(109+9)" id="MathJax-Element-1-Frame" role="presentation" style="display: inline; line-height: normal; text-align: left; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; position: relative;" tabindex="0">mod(10^9+9), where x is the number of ways to sort the given sequence using as few swaps as possible.
样例输入
3
3
2 3 1
4
2 1 4 3
2
1 2
样例输出
3
2
1
样例解释
In the first test case, we can sort the permutation in two swaps. We can make the first swap arbitrarily; for each of them, there’s exactly one optimal second swap. For example, one of the three shortest solutions is “swap p1 with p2 and then swap p1 with p3”.
In the second test case, the optimal solution involves swapping p1 with p2 and swapping p3 with p4. We can do these two swaps in either order.
The third sequence is already sorted. The optimal number of swaps is 0, and thus the only optimal solution is an empty sequence of swaps.
</article>
分析
参照Rose_max的题解。
对于每个位置i,我们向他应该填的数所在的位置p[i]连一条边
如此会出来一些环,我们的目的是将这些环拆成n个自环
对于一个长度为n的环,我们发现要把他拆成n个自环至少需要n-1次操作
设T(x,y)表示将长度为n的环拆成长度分别为x,y的环的方案数,设f[n]表示将长度为n的环拆成n个自环的方案数
画图可知
T(x,y)=n/2" role="presentation" style="position: relative;">T(x,y)=n/2 n为偶数且x=y
T(x,y)=n" role="presentation" style="position: relative;">T(x,y)=n otherwise
对于长度为x的环的操作全部看成0,长度为y的环的操作全部看成1,进行多重集的排列。可以发现这对应出的就是长度为n的环要拆成n个自环的操作方案
根据多重集的排列公式有f[n]=∑x+y=nT(x,y)∗f[x]∗f[y]∗(n−2)!(x−1)!(y−1)!" role="presentation" style="text-align: center; position: relative;">f[n]=∑x+y=nT(x,y)∗f[x]∗f[y]∗(n−2)!(x−1)!(y−1)!最终答案也可以用一个多重集的排列给出
对于k个长度分别为l1,l2,...,lk" role="presentation" style="position: relative;">l1,l2,...,lk的环,有ans=∏f[l1]∗f[l2]∗...∗f[lk]∗(n−k)!(l1−1)!(l2−1)!...(lk−1)!" role="presentation" style="text-align: center; position: relative;">ans=∏f[l1]∗f[l2]∗...∗f[lk]∗(n−k)!(l1−1)!(l2−1)!...(lk−1)!递推复杂度O(n2)" role="presentation" style="position: relative;">O(n2)
我们把f的前几项求出来找规律可以发现f[n]=nn−2" role="presentation" style="position: relative;">f[n]=nn−2
如此复杂度降为O(nlogn)" role="presentation" style="position: relative;">O(nlogn)
代码
#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll;
co int SIZE=1e5+1,mod=1e9+9;
int p[SIZE],v[SIZE],T,n;
ll jc[SIZE];
int power(int a,int b){
int c=1;
for(;b;b>>=1){
if(b&1) c=(ll)c*a%mod;
a=(ll)a*a%mod;
}
return c;
}
int main()
{
// freopen(".in","r",stdin),freopen(".out","w",stdout);
jc[0]=1;
for(int i=1;i<=1e5;++i) jc[i]=jc[i-1]*i%mod;
read(T);
while(T--){
read(n);
for(int i=1;i<=n;++i) read(p[i]),v[i]=0;
int cnt=0;
ll ans=1;
for(int i=1;i<=n;++i){
if(v[i]) continue;
int len=1;
v[i]=1;
for(int j=p[i];j!=i;j=p[j]) v[j]=1,++len;
++cnt;
ans=ans*(len==1?1:power(len,len-2))%mod;
ans=ans*power(jc[len-1],mod-2)%mod;
}
ans=ans*jc[n-cnt]%mod;
printf("%lld\n",ans);
}
return 0;
}
CH3602 Counting Swaps的更多相关文章
- Counting swaps
Counting swaps 给你一个1-n的排列,问用最少的交换次数使之变为递增排列的方案数\(mod\ 10^9+7\),1 ≤ n ≤ 10^5. 解 显然最少的交换次数不定,还得需要找到最小交 ...
- 洛谷P4778 Counting swaps 数论
正解:数论 解题报告: 传送门! 首先考虑最终的状态是固定的,所以可以知道初始状态的每个数要去哪个地方,就可以考虑给每个数$a$连一条边,指向一个数$b$,表示$a$最后要移至$b$所在的位置 显然每 ...
- luogu P4778 Counting swaps
计数套路题?但是我连套路都不会,,, 拿到这道题我一脸蒙彼,,,感谢@poorpool 大佬的博客的指点 先将第\(i\)位上的数字\(p_i\)向\(i\)连无向边,然后构成了一个有若干环组成的无向 ...
- LFYZOJ 104 Counting Swaps
题解 #include <iostream> #include <cstdio> #include <algorithm> #include <cmath&g ...
- lfyzoj104 Counting Swaps
问题描述 给定你一个 \(1 \sim n\) 的排列 \(\{p_i\}\),可进行若干次操作,每次选择两个整数 \(x,y\),交换 \(p_x,p_y\). 请你告诉穰子,用最少的操作次数将给定 ...
- luoguP4778 Counting swaps
题目链接 题解 首先,对于每个\(i\)向\(a[i]\)连边. 这样会连出许多独立的环. 可以证明,交换操作不会跨越环. 每个环内的点到最终状态最少交换步数是 \(环的大小-1\) 那么设\(f[i ...
- P4778 Counting Swaps 题解
第一道 A 掉的严格意义上的组合计数题,特来纪念一发. 第一次真正接触到这种类型的题,给人感觉好像思维得很发散才行-- 对于一个排列 \(p_1,p_2,\dots,p_n\),对于每个 \(i\) ...
- 0x36 组合计数
组合计算的性质: C(n,m)= m! / (n!(m-n)!) C(n,m)=C(m-n,m); C(n,m)=C(n,m-1)+C(n-1,m-1); 二项式定理:(a+b)^n=sigema(k ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
随机推荐
- C++数组引用
C++数组引用 一.数组引用 C++数组的引用:引用即别名这样比指针传地址方便多了 形参中的(&a)[10]可以就看做a数组的别名,肯定要指定数组大小,如果没有后面的数组大小,天知道是变量还是 ...
- python-day20--collections模块
1.namedtuple: 生成可以使用名字来访问元素内容的tuple >>> from collections import namedtuple >>> Poi ...
- python-day13--装饰器
1.开放封闭的原则: 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新功能. 2.对 ...
- ubuntu 用户和root权限转换
1,用户权限要转换为root 输入:sudo su 或者sudo -i 然后按照提示输入相应的密码你就可以转化为root用户了. 2,root权限切换成用户权限 输入:su 如果是服务器那就输 ...
- 关于Floyd求解最小环的问题
最近学习了floyd的奇妙用处,求解最小环,自己的领悟写在了纸上. 对于一个最小环,显然至少要包含三个点(此处不把两个点的回路称之为环) 从大体上考虑的话,一定有一个点与左右两侧的点是直接连接的(即不 ...
- IIS Web 服务器/ASP.NET 运行原理基本知识概念整理
前言: 记录 IIS 相关的笔记还是从公司笔试考核题开始的,问 Application Pool 与 AppDomain 的区别? 促使我对进程池进了知识的学习,所以记录一下学习 ...
- datafile相关(add、rename、drop)
--case 1 add14:25:04 FPYJ(150_9)@test> alter tablespace fpyj_data02 add datafile '/oradata02/test ...
- iOS UI-(多)视图控制器的生命周期、加载方法和模态视图方法以及屌丝方法
#import "ViewController.h" #import "SecondViewController.h" @interface ViewContr ...
- Flask初级(四)flash在模板中使用静态文件
Project name :Flask_Plan templates: 默认设置下,Flask在程序根目录中名为static的子目录中寻找静态文件. 随便找个图片放进去把,命令test.png Fla ...
- Flask初级(一)创建及运行flash
和前面的django差不多,选个类型,定义个目录. 选个模板解释器,定义一个模板文件夹名称. Create就创建成功了. 运行一下.会显示 Hello World! 最好给它换个运行环境,建个虚拟环境 ...