Uva 11077 Find the Permutations [置换群 DP]

题意:

给定$n$和$k$,问有多少排列交换$k$次能变成升序

$n \le 21$



$uva$貌似挂掉了$vjudge$上一直排队

从某个排列到$1,2,...,n$和从$1,2,...,n$到某个排列是一样的

排列就是置换,分解循环,然后显然每个循环变成升序需要$len-1$次交换

然后有$t$个循环的置换需要$n-t$次交换

$DP$就行了$f[i][j]$表示前$i$个数有$j$个循环

其实可以发现就是第一类$stirling$数

注意:以后一定要测一遍极限会爆$long\ long$

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=30;
typedef unsigned long long ll;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();}
    return x*f;
}
int n,k;
ll f[N][N];
void dp(){
    f[0][0]=1;
    for(int i=1;i<=21;i++)
        for(int j=1;j<=21;j++)
            f[i][j]=f[i-1][j-1]+f[i-1][j]*(i-1);
}
int main(){
    //freopen("in","r",stdin);
    dp();
    while(true){
        n=read();k=read();
        if(n==0&&k==0) break;
        printf("%llu\n",f[n][n-k]);
    }
}
时间: 2024-12-29 04:21:37

Uva 11077 Find the Permutations [置换群 DP]的相关文章

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

UVA 11077 Find the Permutations DP

Find the Permutations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Find the Permutations Input: Standard Input Output: Standard Output Sorting is one of the most used operations in real lif

UVA 11077 Find the Permutations 递推置换

                           Find the Permutations Sorting is one of the most used operations in real life, where Computer Science comes into act. It iswell-known that the lower bound of swap based sorting is nlog(n). It means that the best possiblesor

UVA - 11077 Find the Permutations (置换)

Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It iswell-known that the lower bound of swap based sorting is nlog(n).It means that the best possible sorting algorithm will take at least W(nlog(n))swaps

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

UVA - 825Walking on the Safe Side(dp)

题目: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵,起点是1 * 1,终点是n * m,这个矩阵上有些点是不可以经过的,要求从起点到终点距离最短,并且不能走那种不能走的点,一共有多少种方式. 解题思路:要求路径最短的话,每个点要不向右走,要不向下走.dp[i][j] = dp[i][j + 1] + dp[i + 1][j]:当这个点不能通过,dp[i][j] = 0:这个坑点在样例输入,不一定是规范的输入,可能两个数字之间很多

uva 11361 Investigating Div-Sum Property 数位dp

// uva 11361 Investigating Div-Sum Property 数位dp // // 题目大意: // // 给你一个整数a和一个整数b,问在[a,b]范围内,有多少个自身被k整除并且 // 各位数之和也能被k整除.比如k = 7 ,322满足条件,因为332能被整除7,并 // 3 + 2 + 2 = 7 也能被7整除 // // 解题思路: // // 求一个区间的题目,这类题目,往往可以转化为不超过x的f(x).则最后要求 // 的就是f(b) - f(a-1).如

uva live 3516 Exploring Pyramids 区间DP

// uva live 3516 Exploring Pyramids 区间DP // // 题目大意: // // 给你一个多叉树,每个节点是一个大写字母,从根节点走,按照先序遍历的 // 原则访问,不能访问则回溯,每次记录一下节点的字符,最后得到一个字符串.现 // 在给你一个字符串,问可能符合条件的多叉树的数量. // // 解题思路: // // 区间DP,我们注意到,从根节点出发,一定会再次回到根节点,那么我们可以设 // d(i,j) 是序列i到j段形成的符合条件的多叉树的数量,则

UVA 11825 Hackers&#39; Crackdown 状压DP

感觉白书上的做法很神! 首先状压表示电脑之间的联通关系,然后预处理出所有关闭电脑的组合达到的状态,然后枚举每个状态并且枚举每个状态的所有子集,之后无脑递推就木有了. 关于枚举一个状态所有子集的小技巧:假设当前状态是S0 有 for s = s0; s != 0; s =  (s - 1) & s0 #include <cstdio> #include <cstring> #include <iostream> #include <map> #incl