UVA 11077 Find the Permutations 递推置换
Find the Permutations
Sorting is one of the most used operations in real life, where Computer Science comes into act. It is
well-known that the lower bound of swap based sorting is nlog(n). It means that the best possible
sorting algorithm will take at least O(nlog(n)) swaps to sort a set of n integers. However, to sort a
particular array of n integers, you can always find a swapping sequence of at most (n − 1) swaps, once
you know the position of each element in the sorted sequence.
For example consider four elements <1 2 3 4>. There are 24 possible permutations and for all
elements you know the position in sorted sequence.
If the permutation is <2 1 4 3>, it will take minimum 2 swaps to make it sorted. If the sequence
is <2 3 4 1>, at least 3 swaps are required. The sequence <4 2 3 1> requires only 1 and the sequence
<1 2 3 4> requires none. In this way, we can find the permutations of N distinct integers which will
take at least K swaps to be sorted.
Input
Each input consists of two positive integers N (1 ≤ N ≤ 21) and K (0 ≤ K < N) in a single line.
Input is terminated by two zeros. There can be at most 250 test cases.
Output
For each of the input, print in a line the number of permutations which will take at least K swaps.
Sample Input
3 1
3 0
3 2
0 0
Sample Output
3
1
2
题意:
给定一个1~n的排序,可以通过一系列的交换变成1,2,…,n, 给定n和k,统计有多少个排列至少需要交换k次才能变成有序的序列。
题解:
每个长度为n循环需要交换n-1次才能将交换到对应的位置,例如1->2,2->4,4->1,(1,2,4)位置上对应值为(2,4,1) 相当于一个长度为3的环逆时针旋转了1格,要变换回来,需要跟原位置交换,因为成环,所以共n-1次 那么对于序列P,有x个循环节,长度为n,就需要交换n-x次 对于f[i][j],表示交换j次能变为1~i的序列的种数 我们找到递推式:f[i][j]=f[i-1][j]+f[i-1][j-1]*(i-1),边界是f[1][0]=1,其余为0 解释:新增的i,如果与自己构成循环,那么循环数和长度都加一,交换数不变,所以是f[i-1][j] 新增的i,如果参加到其他环中,每个数后一种,共i-1种,循环数不变,长度加一,所以是f[i-1][j-1]*(i-1) 例如1->2,2->3,3->1,{2,3,1};将4添加到1后就是1->4,4->2,2->3,3->1,序列为{4,3,1,2};其他同上
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll;
const int N = ;
const int mod = 1e9 + ;
unsigned long long dp[N][N];
void init() {
for(int i = ; i <= ; i++) {
dp[i][] = ;
for(int j = ; j < i; j++)
dp[i][j] = dp[i-][j] + dp[i-][j-] * (i-);
}
}
int main () {
init();
int n,k;
while(~scanf("%d%d",&n,&k)) {
if(n == && k == ) break;
printf("%llu\n",dp[n][k]);
}
return ;
}
UVA 11077 Find the Permutations 递推置换的更多相关文章
- UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...
- UVA 557 Burger 排列组合递推
When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at t ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- UVa 1638 Pole Arrangement【递推】
题意:给出n根高度为1,2,3,---n的杆子,从左边能看到l根,右边能够看到r根,问有多少种可能 看的紫书的思路 先假设已经安排好了高度为2---i的杆子, 那么高度为1的杆子的放置方法有三种情况 ...
- UVA 11464 偶数矩阵(递推 | 进制)
题目链接:https://vjudge.net/problem/UVA-11464 一道比较好的题目. 思路如下: 如果我们枚举每一个数字“变”还是“不变”,那么需要枚举$2^{255}$种情况,很显 ...
- UVA - 11077 Find the Permutations (置换)
Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
- Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
随机推荐
- Sqoop_具体总结 使用Sqoop将HDFS/Hive/HBase与MySQL/Oracle中的数据相互导入、导出
一.使用Sqoop将MySQL中的数据导入到HDFS/Hive/HBase watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFyb25oYWRvb3A=/ ...
- P1233 木棍加工
P1233 木棍加工 题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间 ...
- leetcode 系列文章目录
leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...
- linux下解决非本机访问出现的提示
安装完毕之后发现无法用IP访问,出现以下提示 Access forbidden! New XAMPP security concept: Access to the requested directo ...
- ROS-SLAM仿真-cartographer
前言:cartographer是谷歌2016年发布的一个开源slam算法,采用基于图网络的优化方法,主要基于激光雷达来实现. 使用源码编译方式. 一.新建工作空间 1.1 使用roboware新建名为 ...
- 算法入门经典第七章 例题7-2-1 生成1-n的排列
输入正数n,按字典序从小到大的顺序输出n个数的所有排列.两个序列的字典序大小关系等价于从头开始第一个不相同位置处的大小关系. 递归的边界应该很好理解吧,当集合s[]中没有一个元素的时候,按照上面的伪码 ...
- MyEclipse安装TestNG
1.获取TestNG运行包. (1).直接下载*.jar包并导入项目中. (2).maven下载. http://testng.org/doc/download.html 2.为IDE加载TestNG ...
- Safari new Date() 兼容问题
我的时间 var myTime = "2015-12-31 12:10:21"; 正常写法 var newTime = new Date(myTime); safari兼容写法 ...
- Android PullTorefreshScrollview回到顶部
列表滑动下面显示按钮,点击按钮回到顶部的功能,一般scrollview会有滑动监听的事件,通过setOnScrollChangeListener()滑动监听滑动的距离来判断是否显示按钮就好了,但是Pu ...
- 彻底解决降级安装失败无法彻底卸载应用bug
彻底解决魅族手机无法彻底卸载应用bug使用Flyme系统的同学可能会遇到一个问题:卸载了某些软件(例如通过开发者模式调试安装的应用)后,实际这个应用还残留在系统,当你用低版本或者其他签名的apk覆盖安 ...