UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations
题意:给定n,k求出有多少个包括元素[1-n]的序列,交换k次能得到一个[1,2,3...n]的序列
思路:递推dp[i][j]表示i个元素须要j次。那么在新加一个元素的时候。添在最后面次数不变。其余位置都是次数+1,这是能够证明的。原序列中有几个循环,须要的次数就是全部循环长度-1的和,那么对于新加一个元素,加在最后就和自己形成一个循环。次数不变,其余位置都会增加其它循环中,次数+1。因此递推式为dp(i,j)=dp(i−1,j−1)∗(i−1)+dp(i−1,j)
代码:
#include <stdio.h>
#include <string.h> const int N = 22;
int n, k;
unsigned long long dp[N][N]; int main() {
dp[1][0] = 1;
for (unsigned long long i = 2; i <= 21; i++) {
for (int j = 0; j < i; j++) {
dp[i][j] = dp[i - 1][j];
if (j) dp[i][j] += dp[i - 1][j - 1] * (i - 1);
}
}
while (~scanf("%d%d", &n, &k) && n || k) {
printf("%llu\n", dp[n][k]);
}
return 0;
}
UVA 11077 - Find the Permutations(递推)的更多相关文章
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...
- UVA 557 Burger 排列组合递推
When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at t ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- UVa 1638 Pole Arrangement【递推】
题意:给出n根高度为1,2,3,---n的杆子,从左边能看到l根,右边能够看到r根,问有多少种可能 看的紫书的思路 先假设已经安排好了高度为2---i的杆子, 那么高度为1的杆子的放置方法有三种情况 ...
- UVA 11464 偶数矩阵(递推 | 进制)
题目链接:https://vjudge.net/problem/UVA-11464 一道比较好的题目. 思路如下: 如果我们枚举每一个数字“变”还是“不变”,那么需要枚举$2^{255}$种情况,很显 ...
- Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
- UVa 580 - Critical Mass(递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- 【hash】什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法【关于hash的详解】
什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法
- mysql忘记密码的解决办法
mysql忘记密码时,需要重设密码. 在Windows下的操作如下: 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant- ...
- Discussion about z pre-pass
Z pre-pass In the rendering Process, the first pass render to a depth buffer to get the front layer ...
- 2016.10.17 yaml文件里的labels和Pod、RC、Service的对应关系
在看kubernetes的例子时,出现了一个疑问. Pod.RC.Service的yaml文件里,都出现了labels,还有labelSelector.有些不太清楚,因此就这点来学习下. 接上文: ...
- 25. Spring Boot使用自定义的properties【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069515 spring boot使用application.properties默认了很 ...
- 使用rabbitmq rpc 模式
服务器端 安装 ubuntu 16.04 server 安装 rabbitmq-server 设置 apt 源 curl -s https://packagecloud ...
- 国内云引擎平台概览——新浪SAE,阿里ACE,百度BCE
新浪SAE 平时大家的測试server都是执行在自己的PC上面,用Tomcat或者IIS搭建的本机server. 事实上新浪云平台SinaAppEngine也是挺好用的. 今天总结一下我使用过程中的一 ...
- jquery Table基础操作
鼠标移动行变色 $("#table1 tr").hover(function(){ $(this).children("td").ad ...
- 使用Apache Benchmark做压力测试遇上的5个常见问题
这一篇文章主要记录我在使用Apache Benchmark(一下检测ab)做网站压力测试的过程中,遇到的一些问题以及解决办法,方便日后使用. 这一篇文章主要记录我在使用Apache Benchmark ...
- ios面试基础
1.#import和#include的差别 @class? @class一般用于头文件里须要声明该类的某个实例变量的时候用到,在m文 件中还是须要使用#import 而#import比起#includ ...