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& ...
随机推荐
- Synchronized 实现原理
记得刚刚开始学习Java的时候,一遇到多线程情况就是synchronized.对于当时的我们来说,synchronized是如此的神奇且强大.我们赋予它一个名字"同步",也成为我们 ...
- 2016集训测试赛(二十一)Problem C: 虫子
题目大意 给你一棵树, 每个点有一个点权. 有两种操作: link / cut 修改某个点的点权 每次操作后, 你要输出以下答案: 在整棵树中任意选两个点, 这两个点的LCA的期望权值. Soluti ...
- centos7 samba安装与配置
1.关闭防火墙. CentOS 7 是自带的firewall,CentOS 6 好像是iptables.关闭防火墙命令如下: 第一种方法是关闭防火墙: systemctl disable firewa ...
- sql执行顺序图
http://www.16aspx.com/cmsimages/20130325/664845013.png
- javascript if(条件)------------条件中可以使用的值
1.布尔变量true/false2.数字非0,非NaN/ ( 或NaN) NaN--------Not a Number 3.对象非null/(null或undefined) 4.字符串非空串(&qu ...
- Ruby之Rspec的报错解决
#enconding:utf-8 require 'selenium-webdriver' require 'rspec' describe "baidu main page" d ...
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- Html5 meta 笔记
摘抄:原文地址:http://www.kmapk.com/html/help/02/127.html 一.天猫 <title>天猫触屏版</title> <meta co ...
- arcgis水文分析
前言 1.在开始之前首先需要注意几点: 1.arcgis 需要 python2.7 的支持,并有必要的模块库,请一定注意避免与其他软件冲突,例如tecplot 2009 需要python2.5的支持, ...
- uva 11404 dp
UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...