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;
- }
UVA - 11077 Find the Permutations (置换)的更多相关文章
- UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
- UVa 11077 Find the Permutations (计数DP)
题意:给定 n 和 m,问你在 1 ~ n 的所有排列中,有多少个排列满足至少要交换 m 次才能变成 1 2 3 ... n. 析:首先,先考虑一下,某个排列,要变成 1 2 3 .. n,最少要交换 ...
- LA 3641 Leonardo的笔记本 & UVA 11077 排列统计
LA 3641 Leonardo的笔记本 题目 给出26个大写字母的置换B,问是否存在要给置换A,使得 \(A^2 = B\) 分析 将A分解为几个循环,可以观察经过乘积运算得到\(A^2\)后,循环 ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- UVa 11077 (循环分解 递推) Find the Permutations
把{1, 2, 3,,, n}叫做自然排列 本题便是求有多少个n元排列P要至少经过k次交换才能变为自然排列. 首先将排列P看做置换,然后将其分解循环,对于每个长度为i的循环至少要交换i-1次才能归位. ...
- uva 11077 置换
/** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...
随机推荐
- 【LuoguP5004】 专心OI - 跳房子
首先这是一道计数类DP,那我们得先推式子,经过瞎掰乱凑,经过认真分析,我们可以得到这样的方程 F(N)=F(0)+F(1)+....+F(N-M-1) 所有F初值为1,F(1)=2 ANS=F(N+M ...
- A - Petya and Strings
Problem description Little Petya loves presents. His mum bought him two strings of the same size for ...
- 在.xls;*.xlsx类型文件的导入(可以导入多条数据)
2018-11-28 17:36:35 需要jar包:poi-3.8-201203026.jar jsp页面: <LINK href="${basePath}plugins/upl ...
- CSS画各种二维图形
1.效果 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...
- 表单校验插件(bootstrap-validator)
表单校验插件(bootstrap-validator) 参考文档 http://blog.csdn.net/nazhidao/article/details/51542508 http://blog. ...
- Java数据的基本类型
整数类型: byte:8位(1个字节) eg:byte x=2,y: 错误实例:byte b:b=b+3: 其中b=b+3是错误的,应该是b=(byte)(b+3)强制转换: short:16位( ...
- Python学习①. 基础语法
Python 简介 Python 是一种解释型,面向对象的语言.特点是语法简单,可跨平台 Python 基础语法 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编 ...
- 测试 Zoundry Raven
安装很方便,看看发布的内容是否好用 但发现从博客上取下来的内容是有问题的,不能正常打开
- 想要远程服务器长时间挂机不断开ssh连接的技巧
使用top命令挂着就好了,top命令执行的“查看系统进程和资源占用”的任务会一直输出动态的数据,一直有数据传输就不会因为长时间挂机而断开ssh链接了,尤其针对于海外服务器,因为高延迟经常出现挂机久了自 ...
- 分布式锁获取token
package com.sankuai.qcs.regulation.nanjing.util; import com.dianping.squirrel.client.StoreKey; impor ...