UVA11077 Find the Permutations(排列统计)
来源:互联网 发布:张家口seo公司 编辑:程序博客网 时间:2024/05/22 07:40
UVA11077 Find the Permutations(排列统计)
题目链接
- 题解(出自训练指南)
首先考虑一个简单的问题。任意给出一个排列P,至少需要交换几次才能变成{1,2,…,n}?这个次数也等于从{1,2,…,n}变换到该排列所需的次数。因此,直接把排列P理解成一个置换,并且分解成循环,各循环之间独立。
设f(i,j)表示满足“至少需要交换j次才能变成{1,2,…,n}”的排列个数,则f(i,j)=f(i-1,j-1)+f(i-1,j)*(i-1),因为元素i要么自己形成一个循环,要么加入前面任意一个循环的任意一个位置。边界为f(1,0)=1,其他f(1,j)=0。
代码如下
#include<bits/stdc++.h>using namespace std;typedef unsigned long long ll;ll ans[25][25];int main(){ memset(ans,0,sizeof(ans)); ans[1][0]=1; for (int i=2;i<22;++i) for(int j=0;j<i;++j) { ans[i][j]=ans[i-1][j]; if(j) ans[i][j]+=ans[i-1][j-1]*(i-1); } int n,k; while(~scanf("%d%d",&n,&k),n+k) printf("%llu\n",ans[n][k]); return 0;}
注意要用unsigned long long
阅读全文
0 0
- UVA11077 Find the Permutations(排列统计)
- Find the Permutations UVA11077
- uva11077 Find the Permutations
- uva11077 Find the Permutations
- UVa11077-Find the Permutations(dp+置换)
- UVA 11077 Find the Permutations(置换)
- Find the Permutations [Uva 11077]
- UVA Find the Permutations 11077 (DP&置换群)
- uva11077
- LeetCode | Permutations(全排列)
- LeetCode 46. Permutations(排列)
- 46. Permutations(全排列)
- UVa 11077 Find the Permutations / 置换
- UVA 11077 Find the Permutations(置换+dp)
- UVA 11077 - Find the Permutations(递推)
- uva 11077 - Find the Permutations(置换)
- UVA - 11077 Find the Permutations (置换)
- UVA 11077 Find the Permutations DP
- 依赖
- form表单验证,添加数据显示
- hdfs版本引起的问题check-sum mismatch between
- 知识学习——jQuery事件
- 转自RObin 大佬的代码
- UVA11077 Find the Permutations(排列统计)
- 怎样才能赚到钱系列(十一):拥抱变化的世界
- jquery绑定事件
- 用java编写的“私人备忘录”
- Banner的简单使用
- 递归方式实现打印一个整数的每一位
- Python List 列表
- Spring学习总结
- ubuntu无法打开terminal