UVA - 11077 Find the Permutations (置换)

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
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;
}
时间: 2024-10-05 04:07:09

UVA - 11077 Find the Permutations (置换)的相关文章

UVA 11077 - Find the Permutations(递推)

UVA 11077 - Find the Permutations 题目链接 题意:给定n,k求出有多少个包含元素[1-n]的序列,交换k次能得到一个[1,2,3...n]的序列 思路:递推dp[i][j]表示i个元素需要j次,那么在新加一个元素的时候,添在最后面次数不变,其余位置都是次数+1,这是可以证明的,原序列中有几个循环,需要的次数就是所有循环长度-1的和,那么对于新加一个元素,加在最后就和自己形成一个循环,次数不变,其余位置都会加入其他循环中,次数+1,因此递推式为dp(i,j)=dp

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 iswell-known that the lower bound of swap based sorting is nlog(n). It means that the best possiblesor

Uva 11077 Find the Permutations [置换群 DP]

题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个排列是一样的 排列就是置换,分解循环,然后显然每个循环变成升序需要$len-1$次交换 然后有$t$个循环的置换需要$n-t$次交换 $DP$就行了$f[i][j]$表示前$i$个数有$j$个循环 其实可以发现就是第一类$stirling$数 注意:以后一定要测一遍极限会爆$long\ long$

UVA 11077 Find the Permutations DP

Find the Permutations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Find the Permutations Input: Standard Input Output: Standard Output Sorting is one of the most used operations in real lif

uva 11077 置换

1 /** 2 给定一个置换,看能不能存在一个置换A^2 = B 3 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即可.. 4 训练指南 5 **/ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 using namespace std; 10 const int maxn = 30; 11 1

poj 2369 Permutations 置换水题

找循环节求lcm就够了,若答案是12345应该输出1,被坑了下. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; #define INF 0x3FFFFFF #define MAXN 2222 #define eps 1e-6 i

UVA - 10733 The Colored Cubes (置换)

All 6 sides of a cube are to becoated with paint. Each side is is coated uniformly with one color. When a selectionof n different colors of paint is available, how many different cubes can youmake? Note that any two cubes are onlyto be called "differ

uva 11330 - Andy&#39;s Shoes(置换)

题目链接:uva 11330 - Andy's Shoes 题目大意:小andy有很多鞋,穿完到处丢,后来他把所有鞋都放回鞋架排成一排,保证了鞋的左右交替,但是颜色混了.问说他至少移动多少次可以将鞋分类好. 解题思路:对应奇数位置为左鞋,偶数位置为右鞋,一双鞋只有一只左鞋和一只右鞋,保证不换左变鞋子,以左鞋的位置为基准换右边鞋子,对应右边鞋子的位置即为一个置换,将置换的循环分解为x个互不相干的循环,ans=n-x #include <cstdio> #include <cstring&g

uva 716 - Commedia dell&#39; arte(置换)

题目链接:uva 716 - Commedia dell' arte 题目大意:给定一个三维的八数码,0表示空的位置,问说是否可以排回有序序列. 解题思路:对于n为奇数的情况,考虑三维八数码对应以为状态下去除0的时候逆序对数,偶数的情况下,考虑将0的位置转移到(n,n,n)位置后对应序列的逆序对数.如果逆序对数为偶数即为可以,奇数不可以. #include <cstdio> #include <cstring> #include <algorithm> using na