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 ...
随机推荐
- word2010无法显示endnote x7插件及破解endnote x7
最近本人由于要写文章需要使用endnotex7,相比于mendeley和noteexpress,文献管理和引用我喜欢endnote x7,阅读喜欢mendeley.可是由于之前用的正版30天到期了,破 ...
- 为IT程序员量身定制的12个目标——很经典
对程序员们来说挑战自我非常重要,要么不断创新,要么技术停滞不前.新年伊始,我整理了12个月的目标,每个目标都是对技术或个人能力的挑战,而且可以年复一年循环使用. 01. 变得有耐心 02. 保持健康 ...
- Burnside&Polya总结
这里就算是一个小总结吧- 附参考的网址: http://blog.sina.com.cn/s/blog_6a46cc3f0100s2qf.html http://www.cnblogs.com/han ...
- Spark RDD概念学习系列之action操作
不多说,直接上干货! action操作
- C#中显现串口通信SerialPort类
SerialPort类的常用属性 名 称 说 明 BaseStream 获取 SerialPort 对象的基础 Stream 对象 BaudRate 获取或设置串行波特率 BreakState 获取或 ...
- gdal集成kml库的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 最近要读取kml文件,具体就是把kml文件当作一个矢量文件来读取.我发现gdal是支持集成kml库的.不过集成这个km ...
- Android DrawerLayout设置左右侧滑菜单为全屏
我们可以在MainActivity中获取屏幕宽度后动态赋值给侧滑菜单. 在oncreate时 DisplayMetrics metric = new DisplayMetrics(); getWind ...
- uva 11624 Fire! 【 BFS 】
按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...
- Unity 默认进入的scenes
1,如果有多个场景,那么第一个场景要放在最上边,(其他的也要加) 2,如果不添加,那么Unity会把当前打开的场景添加进来. 3,切换场景的代码 using UnityEngine.SceneMana ...
- 关于背景颜色、TEXT、<b>、<i>、<u>、<br>、< >、<br>、<br>、h1-h6、<span>、<div>、<ol>、<ul>、<a>标签的用法(下载、跳转、锚点)、Img插入的用法
<html> <head> <meta charset="UTF-8"> <title></ ...