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;
}

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

  1. UVA 11077 - Find the Permutations(递推)

    UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  2. UVa 11077 Find the Permutations(置换+递推)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...

  3. UVA 11077 Find the Permutations 递推置换

                               Find the Permutations Sorting is one of the most used operations in real ...

  4. Uva 11077 Find the Permutations [置换群 DP]

    题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...

  5. UVa 11077 Find the Permutations (计数DP)

    题意:给定 n 和 m,问你在 1 ~ n 的所有排列中,有多少个排列满足至少要交换 m 次才能变成 1 2 3 ... n. 析:首先,先考虑一下,某个排列,要变成 1 2 3 .. n,最少要交换 ...

  6. LA 3641 Leonardo的笔记本 & UVA 11077 排列统计

    LA 3641 Leonardo的笔记本 题目 给出26个大写字母的置换B,问是否存在要给置换A,使得 \(A^2 = B\) 分析 将A分解为几个循环,可以观察经过乘积运算得到\(A^2\)后,循环 ...

  7. 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)

    Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...

  8. UVa 11077 (循环分解 递推) Find the Permutations

    把{1, 2, 3,,, n}叫做自然排列 本题便是求有多少个n元排列P要至少经过k次交换才能变为自然排列. 首先将排列P看做置换,然后将其分解循环,对于每个长度为i的循环至少要交换i-1次才能归位. ...

  9. uva 11077 置换

    /** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...

随机推荐

  1. EditPlus 2:用空格替换制表符

    打开软件点击菜单栏上的Tools(工具),在点击perferences(外观),再点击左边栏的File->Setting & Syntax(文件->设置与符号),再点击右栏的Tab ...

  2. C99新增内容之变长数组(VLA)

    我们在使用多维数组是有一点,任何情况下只能省略第一维的长度.比如在函数中要传一个数组时,数组的行可以在函数调用时传递,当属数组的列却只能在能被预置在函数内部.看下面一个例子: #define COLS ...

  3. BZOJ 2406 二分+有上下界的网络流判定

    思路: 求出每行的和  sum_row 每列的和   sum_line 二分最后的答案mid S->i  流量[sum_row[i]-mid,sum_row[i]+mid] i->n+j ...

  4. cookie/session在nodes中的实战

    cookie 和 session 众所周知,HTTP 是一个无状态协议,所以客户端每次发出请求时,下一次请求无法得知上一次请求所包含的状态数据,如何能把一个用户的状态数据关联起来呢? 比如在淘宝的某个 ...

  5. 06--谈谈:C++类的“包含”机制

    谈谈:C++类的“包含”机制   本人在学习Qt的时候发现了一个非常有趣的现象.有很多函数的调用方法都写成了如下的形式: object.func().func2(); 这令小弟着实不懂.在上面这段代码 ...

  6. C# MVC 获得程序运行路径

    string filePath = System.Web.HttpContext.Current.Request.MapPath("~/Upload"); //由虚拟路径指定的服务 ...

  7. Linux常见英文报错中文翻译

    Linux常见英文报错中文翻译(菜鸟必知) 1.command not found 命令没有找到 2.No such file or directory 没有这个文件或目录 3.Permission ...

  8. linux backtrace()详细使用说明,分析Segmentation fault

    linux backtrace()详细使用说明,分析Segmentation fault 在此之前,开发eCos应用程序时,经常碰到程序挂掉后,串口打印输出一大串让人看不懂的数据.今天才明白,原来这些 ...

  9. php第九节课

    面向对象 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  10. 为什么on用的时候会失效?

    困扰了我一个很久的问题今天终于得带解决了,关于 on 的 用法: $("#hasLabels .link").on("click",function(){ .. ...