【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 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 (置换+第一类斯特林数)的更多相关文章
- Codeforces 715E - Complete the Permutations(第一类斯特林数)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题.在 AC 此题之前,此题已经在我的任务计划中躺了 5 个月的灰了. 首先考虑这个最短距离是什么东西,有点常识的人(大雾)应该知道, ...
- UVA - 11077 Find the Permutations (置换)
Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...
- UVA11077 Find the Permutations —— 置换、第一类斯特林数
题目链接:https://vjudge.net/problem/UVA-11077 题意: 问n的全排列中多有少个至少需要交换k次才能变成{1,2,3……n}. 题解: 1.根据过程的互逆性,可直接求 ...
- 【CF715E】Complete the Permutations(容斥,第一类斯特林数)
[CF715E]Complete the Permutations(容斥,第一类斯特林数) 题面 CF 洛谷 给定两个排列\(p,q\),但是其中有些位置未知,用\(0\)表示. 现在让你补全两个排列 ...
- UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- 【HDU 4372】 Count the Buildings (第一类斯特林数)
Count the Buildings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 【组合数学:第一类斯特林数】【HDU3625】Examining the Rooms
Examining the Rooms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 如何快速求解第一类斯特林数--nlog^2n + nlogn
目录 参考资料 前言 暴力 nlog^2n的做法 nlogn的做法 代码 参考资料 百度百科 斯特林数 学习笔记-by zhouzhendong 前言 首先是因为这道题,才去研究了这个玩意:[2019 ...
- 【2019雅礼集训】【CF 960G】【第一类斯特林数】【NTT&多项式】permutation
目录 题意 输入格式 输出格式 思路 代码 题意 找有多少个长度为n的排列,使得从左往右数,有a个元素比之前的所有数字都大,从右往左数,有b个元素比之后的所有数字都大. n<=2*10^5,a, ...
随机推荐
- [uva11997]k个最小和
一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和. k<=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i<=k)+a[ ...
- COGS1882 [国家集训队2011]单选错位
★ 输入文件:nt2011_exp.in 输出文件:nt2011_exp.out 简单对比时间限制:1 s 内存限制:512 MB [试题来源] 2011中国国家集训队命题答辩 [问题 ...
- Vue前端开发规范(山东数漫江湖)
一.强制 1. 组件名为多个单词 组件名应该始终是多个单词的,根组件 App 除外. 正例: export default { name: 'TodoItem', // ... } 反例: expor ...
- bzoj 1927 网络流
首先我们可以知道这道题中每个点只能经过一次,那么我们引入附加源汇source,sink,那么我们可以将每个点拆成两个点,分别表示对于图中这个节点我们的进和出,那么我们可以连接(source,i,1,0 ...
- bzoj 2434 fail tree+dfs序
首先比较明显的是我们可以将字符串组建立ac自动机,那么对于询问s1字符串在s2字符串中出现的次数,就是在以s1结尾为根的fail tree中,子树有多少个节点是s2的节点,这样我们处理fail tre ...
- 使用JQGrid 问题汇总 不定时更新
jqgrid左下角的复杂搜索框显示为下拉框样式searchoptions: { value: ": 全部; 1: 在用; 2: 报废", sopt: ['eq'] } jqgrid ...
- poj 1062 昂贵的聘礼 (dijkstra最短路)
题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- FindQQByProcess
看网上有许多通过进程寻找QQ号的例子,看了一下,里面涉及的知识点还是比较多,但网上的兼容性不太好,而且没有给出匹配字符的来源,所以自己动手写了一下,顺便给出一些我调试的结果. #include &qu ...
- Linux 入门记录:三、Linux 文件基本操作管理
一.复制文件.目录 使用 cp 命令复制文件或目录: $ cp 源文件(夹)目标文件(夹) 常用参数: -r 递归复制整个目录树 -v 显示复制过程的详细信息 二.移动.重命名文件或目录 通过 mv ...
- 集合类---set
定义:一个不包含重复元素的collection.set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素,不保证集合里元素的顺序. 方法使用详解: 1 ...