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 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...
随机推荐
- Java基础-1简单了解与原理
简单了解: Java看起来设计得很像C++,但是为了使语言小和容易熟悉,设计者们把C++语言中许多可用的特征去掉了,这些特征是一般程序员很少使用的.因为Java没有结构,数组和串都是对象,所以不需要指 ...
- HDU 3856 Palindrome ( Manacher + RMQ + 二分 ) WA!!!
不知道错在哪了,求大神指教!!! 思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行. #include < ...
- android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题
android 在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...
- SpringBoot Rabbitmq发送消息
官方文档:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp ...
- SSWR 跟 进一法除法
1.对于浮点数SSWR float x = 3.456; //保留到小数点后两位 ) + 0.5) / 100.0; //output b = 3.46; 2.对于整数SSWR float x ; ...
- Codeforces Round #522 (Div. 2) C. Playing Piano
C. Playing Piano 题目链接:https://codeforces.com/contest/1079/problem/C 题意: 给出数列{an},现在要求你给出一个数列{bn},满足: ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- 在GitHub多个帐号上添加SSH公钥
GitHub后台可以添加多个SSH Keys,但是同一个SSH Keys只能在添加在一个帐号上(添加时提示“Key is already in use”).理由很容易想到,SSH公钥使用时相当于用户名 ...
- python tornado对接权限中心的sdk封装
# -*- coding: utf-8 -*- import json import requests import logging as logger from python.akskapp.scr ...
- python 读取数据库时,datetime类型无法被json序列化--解决方案
新增针对datetime的jsonencode: # -*- coding: utf-8 -*- import json from datetime import date, datetime cla ...