UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431
【思路】
置换+递推
将一个排列看作一个置换,分解为k个循环,则最少需要n-k次交换(循环内部交换)即可排序。
设f[i][j]表示将i个数至少交换j次排序完成的方案数,则有转移方程:
f[i][j] = f[i-1][j]+(i-1)*f[i-1][j-1]
分别表示独立成为一个循环与加入前i-1个循环。
【代码】
#include<cstdio>
#include<cstring>
using namespace std; typedef unsigned long long LL;
const int N = 30+5; LL f[N][N];
int n,k; int main() {
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) f[i][j] += (i-1)*f[i-1][j-1];
}
while(scanf("%d%d",&n,&k)==2 && (n||k))
printf("%llu\n",f[n][k]);
return 0;
}
UVa 11077 Find the Permutations(置换+递推)的更多相关文章
- 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(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- UVA - 590Always on the run(递推)
题目:UVA - 590Always on the run(递推) 题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费. 他刚開始在城市1,必须K天都在这N个城市里跑来跑去.最后一天达到城市 ...
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- UVA 10943 - How do you add? 递推
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 926【简单dp,递推】
UVa 926 题意:给定N*N的街道图和起始点,有些街道不能走,问从起点到终点有多少种走法. 很基础的dp.递推,但是有两个地方需要注意,在标记当前点某个方向不能走时,也要同时标记对应方向上的对应点 ...
- UVa 825【简单dp,递推】
UVa 825 题意:给定一个网格图(街道图),其中有一些交叉路口点不能走.问从西北角走到东南角最短走法有多少种.(好像没看到给数据范围...) 简单的递推吧,当然也就是最简单的动归了.显然最短路长度 ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
- UVa 10288 - Coupons(数学期望 + 递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- web开发基础(同步更新中)
1/Get与Post的区别 GET是我们都熟悉的.它用于请求网页文本.当你在浏览器输入harvard.edu,它会直接访问Harvard的web服务器,去GET /. 第二个最有名的是POST,它经常 ...
- CATransform3DRotate 实现左右,上下翻转效果
CGFloat m34 = 800; CGFloat value = -40://(控制翻转角度) CGPoint point = CGPointMake(0.5, 0.5);//设定翻转时的 ...
- maven项目显示红叉的解决方法
我们在做maven项目时,有时项目会显示红叉,但是项目本身并没有错误,如何去掉呢? 下面是我的解决方法 1.点击项目再右键,在搜索框中输入facets 2.把Dynamic Web Module的版本 ...
- 将[{},{}]转为dict
经常遇到一种需求,需要把从数据库取出的数据,转为dict对象([{}, {},...]-->dict). rs = [{, , "name":"edf"} ...
- Touch组件实现原理
Touch组件的实现主要解决了在pc端和移动端拖拽元素的功能. PC端: 依靠事件: mousedown,mousemove,mouseup的鼠标事件.过程: 1. mousedown事件中记录当前元 ...
- winform C#屏幕右下角弹出消息框并自动消失
①弹出信息框后慢慢下降消失 在主窗体中新增按钮重命名为btnShowAndDisappearMessages,在click事件中写如下代码: private void btnShowAndDisapp ...
- eclipse/ggts/myeclipse清除SVN用户名和密码
很多时候我们在使用eclipse/myeclipse/ggts这些开发工具进行开发的时候会有多个项目存在,不同的项目又存放在不同的svn下,需要进行svn之间的切换,如果你在创建资源库位置的时候保存了 ...
- 使用PHP脚本来写Daemon程序
什么是Daemon进程 这又是一个有趣的概念,daemon在英语中是"精灵"的意思,就像我们经常在迪斯尼动画里见到的那些,有些会飞,有些不会,经常围着动画片的主人公转来转去,啰 ...
- Administration Commands
Commands useful for administrators of a hadoop cluster. balancer Runs a cluster balancing utility. A ...
- iOS: 布局可视化语法 Visual Format Syntax
可视化语法 Visual Format Syntax The following are examples of constraints you can specify using the visua ...