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
12 unsigned long long f[maxn][maxn];
13 int main()
14 {
15 memset(f,0,sizeof(f));
16 f[1][0] = 1;
17 for(int i=2;i<=21;i++){
18 for(int j=0;j<i;j++){
19 f[i][j] = f[i-1][j];
20 if(j>0) f[i][j] += f[i-1][j-1]*(i-1);
21 }
22 }
23 int n,k;
24 while(cin>>n>>k){
25 if(n==0&&k==0)
26 break;
27 cout<<f[n][k]<<endl;
28 }
29 return 0;
30 }

uva 11077 置换

时间: 2024-07-30 23:49:24

uva 11077 置换的相关文章

UVa 11330 (置换 循环的分解) Andy&#39;s Shoes

和UVa11077的分析很类似. 我们固定左脚的鞋子不动,然后将右脚的鞋子看做一个置换分解. 对于一个长度为l的循环节,要交换到正确位置至少要交换l-1次. 1 #include <cstdio> 2 #include <cstring> 3 #include <map> 4 using namespace std; 5 6 bool vis[10000 + 10]; 7 8 int main() 9 { 10 //freopen("in.txt",

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 (置换)

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

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 Permutation

可以发现最优的方案就是一个循环节内互换. 所以一个有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<alg

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 1016 - Silly Sort(置换分解+贪心)

UVA 1016 - Silly Sort 题目链接 题意:给定一个序列,数字都不同,每次可以交换两个数字,交换的代价为两数之和,要求出把这个序列变成递增最小代价 思路:利用置换的分解原理,可以把序列的每条循环单独考虑,对于每条循环而言,不断交换肯定每个数字至少会换到一次,再利用贪心的思想,如果每次拿循环中的最小值去置换,那么就是这个最小值会用长度-1次,而剩下的数字各一次,注意这里还有一种可能优的方法,就是先把整个序列中的最小值换到该循环中,等置换完再换出去,两种都考虑进来即可 代码: #in

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