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}。比如{2,1,4,3}需要两次交换。给定n和k,统计有多少个排列至少需要k次交换才能变成{1,2,…,n}。

【分析】

  先考虑一下怎么计算最少变换次数。

  显然,如果把它弄成x个循环的乘积,最少变换次数为n-x。

  问题变成了,给你n个数,分成n-x份的圆排列方案。这个方案刚好就是第一类斯特林数啊。

  所以很简单,用第一类斯特林数的方程求方案就行了。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL unsigned long long
#define Maxn LL s1[][]; void init()
{
memset(s1,,sizeof());
s1[][]=;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
s1[i][j]=s1[i-][j-]+s1[i-][j]*(i-);
// printf("==%lld\n",s1[2][0]);
} int main()
{
init();
int n,k;
while()
{
scanf("%d%d",&n,&k);
if(n==&&k==) break;
printf("%llu\n",s1[n][n-k]);
}
return ;
}

注意要用unsigned long long ,还是看了别人的代码才知道的。。。不然会WA、。。。。

其实这题只是用了小小的置换的思想而已。

2017-01-11 19:16:56

【UVA 11077】 Find the Permutations (置换+第一类斯特林数)的更多相关文章

  1. Codeforces 715E - Complete the Permutations(第一类斯特林数)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题.在 AC 此题之前,此题已经在我的任务计划中躺了 5 个月的灰了. 首先考虑这个最短距离是什么东西,有点常识的人(大雾)应该知道, ...

  2. UVA - 11077 Find the Permutations (置换)

    Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...

  3. UVA11077 Find the Permutations —— 置换、第一类斯特林数

    题目链接:https://vjudge.net/problem/UVA-11077 题意: 问n的全排列中多有少个至少需要交换k次才能变成{1,2,3……n}. 题解: 1.根据过程的互逆性,可直接求 ...

  4. 【CF715E】Complete the Permutations(容斥,第一类斯特林数)

    [CF715E]Complete the Permutations(容斥,第一类斯特林数) 题面 CF 洛谷 给定两个排列\(p,q\),但是其中有些位置未知,用\(0\)表示. 现在让你补全两个排列 ...

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

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

  6. 【HDU 4372】 Count the Buildings (第一类斯特林数)

    Count the Buildings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. 【组合数学:第一类斯特林数】【HDU3625】Examining the Rooms

    Examining the Rooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. 如何快速求解第一类斯特林数--nlog^2n + nlogn

    目录 参考资料 前言 暴力 nlog^2n的做法 nlogn的做法 代码 参考资料 百度百科 斯特林数 学习笔记-by zhouzhendong 前言 首先是因为这道题,才去研究了这个玩意:[2019 ...

  9. 【2019雅礼集训】【CF 960G】【第一类斯特林数】【NTT&多项式】permutation

    目录 题意 输入格式 输出格式 思路 代码 题意 找有多少个长度为n的排列,使得从左往右数,有a个元素比之前的所有数字都大,从右往左数,有b个元素比之后的所有数字都大. n<=2*10^5,a, ...

随机推荐

  1. Android通知栏介绍与适配总结

    由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知栏玩法,谷歌也不得不对其进行更新迭代调整,增加新功能的同时,也在不断地改变样式,试图迎合更 ...

  2. SSL 证书类型说明: DV OV EV

    内容来自: ssl 证书的三种类型: dv (域名型) , ov (企业型) 和 ev (扩展型) OV.DV和EV证书的区别 另外: 浏览器兼容性测试报告 Symantec 证书为什么相比其他证书要 ...

  3. poj 1298 The Hardest Problem Ever

    题目链接:http://poj.org/problem?id=1298 题目大意:按照所给的顺序要求将输入的字符串进行排列. #include <iostream> #include &l ...

  4. JDBC+Servlet+JSP实现基本的增删改查(简易通讯录)

    前言: 最近学习JavaWeb的过程中,自己实践练手了几个小项目,目前已经上传到我的Github上https://github.com/Snailclimb/JavaWebProject.目前只上传了 ...

  5. nsa工程式(fb.py): perl6调用并修改IP

    use v6; if (@*ARGS != 1) {say 'Use:scan.p6 ip';exit;} my $check_ip = @*ARGS[0]; $check_ip = '<val ...

  6. 【Python学习笔记】使用Python计算皮尔逊相关系数

    源代码不记得是哪里获取的了,侵删.此处博客仅作为自己笔记学习. def multipl(a,b): sumofab=0.0 for i in range(len(a)): temp=a[i]*b[i] ...

  7. fork与vfork区别

    1. 地址空间各段拷贝: fork: 内核为子进程生成新的地址空间结构,拷贝父进程的代码段,数据空间,堆,栈到自身的地址空间,但注意:子进程的代码段并不会分配物理空间,而是指向父进程的代码段物理空间, ...

  8. Jmeter===Jmeter中使用CSV Data Set Config参数化不重复数据执行N遍(转)

    Jmeter中使用CSV Data Set Config参数化不重复数据执行N遍 要求: 今天要测试上千条数据,且每条数据要求执行多次,(模拟多用户多次抽奖) 1.用户id有175个,且没有任何排序规 ...

  9. Scrapy爬虫:抓取大量斗图网站最新表情图片

      一:目标 第一次使用Scrapy框架遇到很多坑,坚持去搜索,修改代码就可以解决问题.这次爬取的是一个斗图网站的最新表情图片www.doutula.com/photo/list,练习使用Scrapy ...

  10. 如何设置static tableview的section区域高度

    重写代理方法- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { i ...