UVa 11077 (循环分解 递推) Find the Permutations
把{1, 2, 3,,, n}叫做自然排列
本题便是求有多少个n元排列P要至少经过k次交换才能变为自然排列。
首先将排列P看做置换,然后将其分解循环,对于每个长度为i的循环至少要交换i-1次才能归位。
设有d(i, j)个i元排列至少交换j次才能变成自然排列。
则有d(i, j) = d(i-1, j) + d(i-1, j-1) * (i-1)
对于元素i有两种选择,自己成一个长度为1的循环,此时交换次数不变;
或者加到前面任意一个循环的任意一个位置,有i-1中情况,因为所加入的循环长度加一,所以至少交换的次数加一。
#include <cstdio> unsigned long long d[][]; int main()
{
d[][] = ;
for(int i = ; i <= ; i++)
for(int j = ; j < i; j++)
{
d[i][j] = d[i-][j];
if(j) d[i][j] += d[i-][j-] * (i-);
} int n, k;
while(scanf("%d%d", &n, &k) == && n)
printf("%llu\n", d[n][k]); return ;
}
代码君
UVa 11077 (循环分解 递推) Find the Permutations的更多相关文章
- UVa 12034 - Race(递推 + 杨辉三角)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10288 - Coupons(概率递推)
UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...
- uva 11375 Matches (递推)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10561 (SG函数 递推) Treblecross
如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...
- UVA 557 - Burger(概率 递推)
Burger When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was ...
- UVA 11021 Tribles(递推+概率)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059 [思路] 递推+概率. 设f[i]表示一只Tribble经 ...
- UVa 12034 Race (递推+组合数学)
题意:A,B两个人比赛,名次有三种情况(并列第一,AB,BA).输入n,求n个人比赛时最后名次的可能数. 析:本来以为是数学题,排列组合,后来怎么想也不对.原来这是一个递推... 设n个人时答案为f( ...
- UVA 12034 Race (递推神马的)
Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded ...
- UVA 12034 Race(递推)
递推,f[i = i个名次][j = 共有j个人] = 方案数. 对于新加入的第j个人,如果并列之前的某个名次,那么i不变,有i个可供并列的名次选择,这部分是f[i][j-1]*i, 如果增加了一个名 ...
随机推荐
- android button 字母自动大写
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" androi ...
- mysql数据恢复
[1] 当数据库被删除后的恢复方法 首先建立一个测试用的数据库. mysql -u root -p123123 ← 用root登录到MySQL服务器 Enter password: ← ...
- java 非法字符过滤 , 半角/全角替换
java 非法字符过滤 , 半角/全角替换 package mjorcen.netty.test1; import java.io.UnsupportedEncodingException; publ ...
- Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...
- 跨线程调用控件之MethodInvoker
原文:http://www.cnblogs.com/cm8448940/archive/2008/07/10/1240045.html 使用到两个控件,一个按钮button1,一个标签label1. ...
- libvirt编译报错
virsh # list --all错误:连接到管理程序失败错误:无效的连接错误:将插槽连接到 '/usr/local/var/run/libvirt/libvirt-sock' 失败: 没有那个文件 ...
- [转载]Spring Web MVC Framework
Required Configuration You need to map requests that you want the DispatcherServlet to handle, by us ...
- Javascript操作元素属性方法总结
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- JavaScript执行上下文
变量声明.函数声明为何会提升?js执行时是如何查找变量的?JavaScript中最基本的部分——执行上下文(execution context) 什么是执行上下文? 当JavaScript代码运行,执 ...
- 深入理解JVM--类的执行机制
在完成将class文件信息加载到JVM并产生class对象之后,就可以执行Class对象的静态方法或者实例方法对对象进行调用了.JVM在源代码编译阶段将源代码编译为字节码文件,字节码是一种中间代码的方 ...