题意

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的更多相关文章

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

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

  2. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  5. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

  7. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  8. [leetcode] 47. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. Leetcode Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

随机推荐

  1. Unity3D在C#编程中的一些命名空间的引用及说明

    System包含用于定义常用值和引用数据类型.事件和事件处理程序.接口.属性和处理异常的基础类和基类.其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序环 ...

  2. leetcode ----ARRAY TWOSUM

    代码的(判断nums[i]或者是target-nums[i]都可以):

  3. nginx:支持https

    1.查看nginx模块 nginx -V 注意是大写的V,小写的v是查看版本号的命令. 如果看到with-ssl那就是有的 2.注册ssl证书并下载 免费的ssl证书有: Let's Encrypt ...

  4. JAVA设计模式(二)工厂模式

    在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.但是在一些情况下, new操作符直接生成对象会带来一些问题.举例来说, 许多类型对象的创造需要一 ...

  5. 八. Python基础(8)--函数

    八. Python基础(8)--函数 1 ● 函数返回布尔值 注意, 自定义的函数也可以是用来作逻辑判断的, 例如内置的startswith()等函数. def check_len(x):     ' ...

  6. jQuery $.each()常见的几种使用方法

    <code class="language-html"><!doctype html> <html> <head> <meta ...

  7. Centos7部署kubernetes-ETCD集群(三)

    1.下载etcd软件包 wget https://github.com/coreos/etcd/releases/download/v3.2.18/etcd-v3.2.18-linux-amd64.t ...

  8. shiro学习笔记 (一)shiro的简介

    shirio的功能 Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境.Shiro可以帮助我们完成:认证.授权.加密.会话管理.与Web集成.缓存等 ...

  9. Alpha冲刺2

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9960487.html 作业博客:https://edu.cnblogs.com/campus/ ...

  10. 继承映射中的java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: person is not mapped [FROM person]

    继承映射中查询对象的过程中报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxExcep ...