Uva 11077 Find the Permutation
可以发现最优的方案就是一个循环节内互换。
所以一个有n个元素,c个循环节的置换的交换次数(最少)是n-c。
然后就可以递推了,把i插入到前i-1个元素构成的置换中,要么新成立一个循环,要么加入到之前的任意循环中去。
所以f[i][j]=f[i-1][j]+f[i-1][j-1]*(i-1)
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define ll unsigned long long
ll f[25][25];
int n,k; inline void init(){
f[0][0]=1;
for(int i=1;i<=21;i++)
for(int j=0;j<i;j++){
f[i][j]=f[i-1][j];
//i自己独立形成一个循环
if(j) f[i][j]+=f[i-1][j-1]*(ll)(i-1);
//i插入之前循环的任意一个位置
}
} int main(){
init(); while(scanf("%d%d",&n,&k)==2){
if(!n&&!k) break;
printf("%llu\n",f[n][k]);
} return 0;
}
Uva 11077 Find the Permutation的更多相关文章
- UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- LA 3641 Leonardo的笔记本 & UVA 11077 排列统计
LA 3641 Leonardo的笔记本 题目 给出26个大写字母的置换B,问是否存在要给置换A,使得 \(A^2 = B\) 分析 将A分解为几个循环,可以观察经过乘积运算得到\(A^2\)后,循环 ...
- 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 (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- UVA - 11077 Find the Permutations (置换)
Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- UVa 11077 (循环分解 递推) Find the Permutations
把{1, 2, 3,,, n}叫做自然排列 本题便是求有多少个n元排列P要至少经过k次交换才能变为自然排列. 首先将排列P看做置换,然后将其分解循环,对于每个长度为i的循环至少要交换i-1次才能归位. ...
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...
- uva 11077 置换
/** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...
随机推荐
- pytorch下对简单的数据进行分类(classification)
看了Movan大佬的文字教程让我对pytorch的基本使用有了一定的了解,下面简单介绍一下二分类用pytorch的基本实现! 希望详细的注释能够对像我一样刚入门的新手来说有点帮助! import to ...
- NodeJS05
商品分类模块 分类model const mongoose = require('mongoose') const schema = new mongoose.Schema({ name: { typ ...
- EK算法模板
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关问题 D: 手机话费
http://acm.ocrosoft.com/problem.php?cid=1316&pid=3 题目描述 小明的手机每天消费1元,每消费K元就可以获赠1元,一开始小明有M元,问最多可以用 ...
- 防御暴力破解SSH攻击
托管在IDC的机器我们通常都用SSH方式来远程管理.但是经常可以发现log-watch的日志中有大量试探登录的 信息,为了我们的主机安全,有必要想个方法来阻挡这些可恨的"HACKER&quo ...
- B-Tree索引和Hash索引的区别
Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...
- vue cli & vue 3.x
vue cli & vue 3.x https://cli.vuejs.org/dev-guide/ui-api.html#ui-api https://cli.vuejs.org/zh/gu ...
- 【bzoj2044】三维导弹拦截 dp+二分图最大匹配
题目描述 n个物品,第i个位置有ai.bi.ci三种属性.每次可以选出满足$\ a_{p_i}<a_{p_{i+1}}\ ,\ b_{p_i}<b_{p_{i+1}}\ ,\ c_{p_i ...
- ACdream 1029 前缀和
Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Subm ...
- python类基础
#coding:gbk class Person(): def __init__(self,age,gender,height,weight): self.age = age self.gender ...