UVA11077 Find the Permutations
题意
PDF
给出1~n的一个排列,可以通过一系列的交换变成{1,2,…,n}。比如{2,1,4,3}需要两次交换。给定n和k,统计有多少个排列至少需要k次交换才能变成{1,2,…,n}。
分析
将给出的排列P视为一个置换,并将其分解为循环,各循环间相互独立。
单元素循环是不需要交换的,两个元素的循环需要交换1次,3个元素的循环需要交换2次,…,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自己形成一个循环,要么加入前面任意一个循环的任意一个位置。边界为f(1,0) = 1,f(1,j) = 0(j >= 1)。
时间复杂度\(O(n^2)\)
代码
#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef unsigned long long ull;
co int N=22;
ull f[N][N];
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
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>0) f[i][j]+=f[i-1][j-1]*(i-1);
}
for(int n,k;read(n)|read(k);) printf("%llu\n",f[n][k]);
return 0;
}
UVA11077 Find the Permutations的更多相关文章
- UVA11077 Find the Permutations —— 置换、第一类斯特林数
题目链接:https://vjudge.net/problem/UVA-11077 题意: 问n的全排列中多有少个至少需要交换k次才能变成{1,2,3……n}. 题解: 1.根据过程的互逆性,可直接求 ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- Permutations
Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Leetcode Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
随机推荐
- poj1226
题解: 后缀数组 把所有串先翻转,用一个没有出现过的字符连接 然后再把所有串接起来 然后用一个没有出现过的字符连接 然后二分 在后缀数组上判断lcp 代码: #include<cstdio> ...
- sql语句最后一行显示统计。
SELECT id, username, id_Num FROM users UNION ALL SELECT '合计', count(*), null FROM users ORDER BY id_ ...
- 使用MYSQL数据库实现编程----第二章第三章课堂知识小总结
第二章1:创建数据库create database myschool 2.数据类型 1.整型 int 2.小数 double 精确度要求高的 ----Decimal(18,4) 2222222 ...
- (C/C++学习笔记) 十九. 模板
十九. 模板 ● 模板的基本概念 模板(template) 函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 语法: template <<模 ...
- error: illegal character '\ufeff' 的解决方案
今天使用scalac 命令编译scala文件的时候,出错了,出现如下错误提示:
- ssh 免密登陆
A 要免密码登录要B 那么需要在A电脑上使用命令 ssh-keygen -t rsa 在~/.ssh/ 目录下生成id_rsa.pub 这个文件,然后将这个文件的内容拷到B电脑de ~/.ssh/au ...
- scanf *的用法
scanf *的用法 char a[LEN]="12:13:14"; char i,j,k,h,l,m; sscanf(a,"%*c%*c:% ...
- 1.Python爬虫入门一之综述
要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...
- ubantu安装node、npm、cnpm、live-server
更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties software-properti ...
- Day7作业及默写
1. 判断一个数是否是水仙花数, 水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数. 例如: 153 = 1**3 + 5**3 + 3**3 InputNu ...