可以发现最优的方案就是一个循环节内互换。

所以一个有n个元素,c个循环节的置换的交换次数(最少)是n-c。

然后就可以递推了,把i插入到前i-1个元素构成的置换中,要么新成立一个循环,要么加入到之前的任意循环中去。

所以f[i][j]=f[i-1][j]+f[i-1][j-1]*(i-1)

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define ll unsigned long long
ll f[25][25];
int n,k; inline void init(){
f[0][0]=1;
for(int i=1;i<=21;i++)
for(int j=0;j<i;j++){
f[i][j]=f[i-1][j];
//i自己独立形成一个循环
if(j) f[i][j]+=f[i-1][j-1]*(ll)(i-1);
//i插入之前循环的任意一个位置
}
} int main(){
init(); while(scanf("%d%d",&n,&k)==2){
if(!n&&!k) break;
printf("%llu\n",f[n][k]);
} return 0;
}

  

Uva 11077 Find the Permutation的更多相关文章

  1. UVA 11077 - Find the Permutations(递推)

    UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  2. LA 3641 Leonardo的笔记本 & UVA 11077 排列统计

    LA 3641 Leonardo的笔记本 题目 给出26个大写字母的置换B,问是否存在要给置换A,使得 \(A^2 = B\) 分析 将A分解为几个循环,可以观察经过乘积运算得到\(A^2\)后,循环 ...

  3. Uva 11077 Find the Permutations [置换群 DP]

    题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...

  4. 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)

    Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...

  5. UVA - 11077 Find the Permutations (置换)

    Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...

  6. UVA 11077 Find the Permutations 递推置换

                               Find the Permutations Sorting is one of the most used operations in real ...

  7. UVa 11077 (循环分解 递推) Find the Permutations

    把{1, 2, 3,,, n}叫做自然排列 本题便是求有多少个n元排列P要至少经过k次交换才能变为自然排列. 首先将排列P看做置换,然后将其分解循环,对于每个长度为i的循环至少要交换i-1次才能归位. ...

  8. UVa 11077 Find the Permutations(置换+递推)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...

  9. uva 11077 置换

    /** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...

随机推荐

  1. day04_08-while查询所有行

    <?php $link = @mysql_connect('localhost','root',''); mysql_query('use test',$link); mysql_query(' ...

  2. SELECTORS模块实现并发简单版FTP

    环境:windows, python 3.5功能:使用SELECTORS模块实现并发简单版FTP允许多用户并发上传下载文件 结构:ftp_client ---| bin ---| start_clie ...

  3. [OpenCV]Mat类详解

    http://blog.csdn.net/yang_xian521/article/details/7107786 Preface Mat:Matrix Mat类可以被看做是opencv中C++版本的 ...

  4. python XlsxWriter创建Excel 表格

    文档(英文) https://xlsxwriter.readthedocs.io/index.html 常用模块说明(中文) https://blog.csdn.net/sinat_35930259/ ...

  5. sql的over函数的使用

    over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.其参数:over(partition by columnname1 order by c ...

  6. 【linux】如何解决VMWare上linux虚拟机连不上外网的问题?

    >>>故障现象:虚拟机连接不到外网? >>>故障背景: Centos7.4发行版本: 虚拟机和VM软件都是nat模式: 注意这里默认的VMWare的DHCP服务时开 ...

  7. Json对象转json数组

    var arr = [];             arr.push(strData);

  8. thinkphp中dump()方法

    dump ThinkPHP 框架 自定义的  用作框架变量 调试用的输出 功能可以说和 var_dump一样的

  9. shit vue-cli & path bug & baseUrl bug

    vue-cli path bug https://cli.vuejs.org/zh/guide/#cli baseUrl bug baseUrl: "././" , https:/ ...

  10. RabbitMQ-Java客户端API指南-上

    RabbitMQ-Java客户端API指南-上 客户端API严格按照AMQP 0-9-1协议规范进行建模,并提供了易于使用的附加抽象. RabbitMQ Java客户端使用com.rabbitmq.c ...