博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA - 11077 Find the Permutations (置换)
阅读量:5240 次
发布时间:2019-06-14

本文共 2106 字,大约阅读时间需要 7 分钟。

Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It iswell-known that the lower bound of swap based sorting is nlog(n).It means that the best possible sorting algorithm will take at least W(nlog(n))swaps to sort a set of nintegers. However, to sort a particular array of n integers, you can alwaysfind a swapping sequence of at most (n-1) swaps, once you know theposition of each element in the sorted sequence. For example – consider fourelements <1 2 3 4>. There are 24 possible permutations and for allelements you know the position in sorted sequence.

 

If the permutation is <2 1 43>, 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 31> requires only 1 and the sequence <1 2 3 4> requires none. In thisway, we can find the permutations of Ndistinct integers which will take at least K swaps to be sorted.

Input

Each input consists of two positiveintegers N (1≤N≤21) and K (0≤K<N) in a single line. Inputis terminated by two zeros. There can be at most 250 test cases.

Output

For each of the input, print in aline the number of permutations which will take at least K swaps.

SampleInput                             Output for Sample Input

3 1

3 0

3 2

0 0

 

3

1

2

 


Problemsetter: Md. Kamruzzaman

Special Thanks: Abdullah-al-Mahmud

 题意:给出一个1-n的排列。能够通过一系列的置换变成{1,2,3...n},给定n和k。统计有多少个排列须要交换k次才干变成{1,2,3..n}

思路:我们能够把排列p理解成一个置换,而且分解成循环,则各循环之间独立,且c个元素的循环须要交换c-1次,设f[i][j]表示至少须要交换j次才干变成{1,2....i}的排列个数,则f[i][j] = f[i-1][j] + f[i-1][j-1]*(i-1)。由于元素i要么自己形成一个循环,要么增加前两随意一个循环的任何位置

#include 
#include
#include
#include
typedef unsigned long long ull;using namespace std;const int maxn = 30;ull f[maxn][maxn];int main() { memset(f, 0, sizeof(f)); f[1][0] = 1; for (int i = 2; i <= 21; i++) for (int j = 0; j < i; j++) { f[i][j] += f[i-1][j]; if (j) f[i][j] += f[i-1][j-1] * (i-1); } int n, k; while (scanf("%d%d", &n, &k) != EOF && n) { printf("%llu\n", f[n][k]); } return 0;}

转载于:https://www.cnblogs.com/yangykaifa/p/7106797.html

你可能感兴趣的文章
细说php(二) 变量和常量
查看>>
iOS开发网络篇之Web Service和XML数据解析
查看>>
个人寒假作业项目《印象笔记》第一天
查看>>
java 常用命令
查看>>
ZOJ 1666 G-Square Coins
查看>>
CodeForces Round #545 Div.2
查看>>
卷积中的参数
查看>>
Linux中Zabbix4.0的搭建
查看>>
《LoadRunner没有告诉你的》之六——获取有效的性能需求
查看>>
51nod1076 (边双连通)
查看>>
Item 9: Avoid Conversion Operators in Your APIs(Effective C#)
查看>>
js去除空格
查看>>
学习Spring Boot:(二十八)Spring Security 权限认证
查看>>
IT学习神器——慕课网App获App Store、Android应用市场重磅推荐
查看>>
Linux网络状态工具ss命令使用详解
查看>>
深入浅出JavaScript(2)—ECMAScript
查看>>
编程珠玑第十一章----排序
查看>>
Face The Right Way POJ - 3276 (开关问题)
查看>>
STEP2——《数据分析:企业的贤内助》重点摘要笔记(六)——数据描述
查看>>
变量的命名规范
查看>>