UVA 11077 Find the Permutations(置换)
来源:互联网 发布:飞升第二元神突破数据 编辑:程序博客网 时间:2024/05/05 06:30
题意:给出1-n的排列,可以通过两两交换变成1,2…n。统计有多少个排列至少交换k次才能变成1,2…n
解法:
1.若1,2…n的循环节个数为x,则至少需要n-x次交换才能得到1,2…n。交换方法是每次取非单数循环中最小的那个数和它的后继交换位置,则该数变成单循环,该循环节中的数的数量少1。因此共需要n-x次交换才能得到1,2….n
2.构建递推式。f[i][j]:前i个数中至少交换j次能得到1,2…n的排列个数
考虑前i-1个数已经排好,将第i个数插入原排列
f[i][j]=f[i-1][j]+f[i-1][j-1]*(i-1)
f[i-1][j]:将i作为单循环
f[i-1][j-1]*(i-1):将i插入某个循环的某个数的后继
注:此题必须用unsigned long long才能通过
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#define MP make_pair#define ll long long#define ull unsigned long longusing namespace std;const ll maxn = 1000 ;ull n,k;ull f[25][25];int main(){ for(ull j=0;j<=21;j++) f[1][j]=(j==0)?1:0; for(ull i=2;i<=21;i++){ for(ull j=0;j<i;j++){ f[i][j]=f[i-1][j]; if(j>0) f[i][j]+=f[i-1][j-1]*(i-1); } } while(scanf("%llu%llu",&n,&k)!=EOF){ if(n==0&&k==0) break; printf("%llu\n",f[n][k]); } return 0;}
0 0
- UVA 11077 Find the 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(置换+dp)
- UVA 11077 Find the Permutations(置换+dp)
- UVA Find the Permutations 11077 (DP&置换群)
- UVA 11077 Find the Permutations 置换+递推
- uva 11077 Find the Permutations 置换+递推
- Find the Permutations [Uva 11077]
- UVA 11077 - Find the Permutations(递推)
- UVA 11077 Find the Permutations DP
- UVa11077-Find the Permutations(dp+置换)
- UVA11077 Find the Permutations(排列统计)
- Find the Permutations UVA11077
- uva11077 Find the Permutations
- uva11077 Find the Permutations
- 黑马程序员-【7K面试题之银行调度系统】
- 仿苹果进度条(progressDialog)
- 设计模式/架构基本原则
- 迭代器模式与组合模式
- const和valotile
- UVA 11077 Find the Permutations(置换)
- leetcode_House Robber
- IOS中的生命周期及加载过程
- Qwt源码解读之标尺相关类
- 设计模式思想总结
- 第一章Java 概述
- 一个简单的 TP 3.2 中 天气查询的接口 (restful)
- java--数组2
- Java代码 给出一个二叉树和一个数,判断该二叉树是否存在一个从根节点到叶节点的和与给出的数相同