题意:给定 n 和 m,问你在 1 ~ n 的所有排列中,有多少个排列满足至少要交换 m 次才能变成 1 2 3 ... n。

析:首先,先考虑一下,某个排列,要变成 1 2 3 .. n,最少要交换几次,这个问题,我们可以把这个排列拆成几个循环,很明显在每个循环中,假设循环长度是 n ,那么至少要交换 n-1 次才能完成,如果不理解的,可以自己举个例子看看,有这个条件,那么就好做了,dp[i][j] 表示 1  ~ i 的排列中至少要交换 j 次才能变成 1 2 3 .. i,dp[i][j] = dp[i-1][j] + (i-1) * dp[i-1][j-1],解释一下这个方程,dp[i-1][j] ,就是直接把 i 放到第 i 个位置,不用交换,正好就是dp[i-1][j],(i-1) * dp[i-1][j-1],这个意思就是对,对于每个排列,我们可以把 i 放到前面的任何循环中,也就是可以把 i 和 前面 1 ~ i-1 上的数进行交换,正好某个循环长度多 1,其他的不变,所以就需要 j 次才能完成。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 20 + 10;
const int maxm = 1e6 + 2;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} LL dp[maxn][maxn]; int main(){
dp[1][0] = 1;
for(int i = 2; i < 22; ++i)
for(int j = 0; j < i; ++j)
dp[i][j] = dp[i-1][j] + (j ? dp[i-1][j-1] * (i-1) : 0);
while(scanf("%d %d", &n, &m) == 2 && n + m) printf("%llu\n", dp[n][m]);
return 0;
}

  

UVa 11077 Find the Permutations (计数DP)的更多相关文章

  1. Uva 11077 Find the Permutations [置换群 DP]

    题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...

  2. UVA 11077 - Find the Permutations(递推)

    UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  3. UVA 11077 Find the Permutations 递推置换

                               Find the Permutations Sorting is one of the most used operations in real ...

  4. UVA - 11077 Find the Permutations (置换)

    Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...

  5. UVa 11077 Find the Permutations(置换+递推)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...

  6. UVA 10564 计数DP

    也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...

  7. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

  8. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  9. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

随机推荐

  1. selenium学习一

    chrome版本和chromedriver的对应关系 chromedriver版本 支持的Chrome版本 v2.40 v66-68 v2.39 v66-68 v2.38 v65-67 v2.37 v ...

  2. gradle项目与maven项目互转

    maven to gradle 在maven项目根目录下执行命令: gradle init --type pom 当然你得先下载Gradle,配置完环境变量. gradle to maven grad ...

  3. swift - 闭包 -定义和使用

    方法一: 1.定义 typealias OpenOrderSuccessResultBlock = ( _ dataArray:[String])->Void 2.类方法实现属性 var bac ...

  4. make ;makefile; cmake; qmake的区分

    1. make 是用来执行Makefile的.2. Makefile是类unix环境下(比如Linux)的类似于批处理的"脚本"文件.其基本语法是: 目标+依赖+命令,只有在目标文 ...

  5. 百度地图插件(百度地图AK申请配置指南)

    百度地图AK申请配置指南     [LBS云] 百度地图AK申请配置指南 1. 该文档是详细版,图文并茂: 2. 该指南是针对browser-mobile-sever三种终端开发的申请与配置说明: 3 ...

  6. 9.Mysql字符集

    9.字符集9.1 字符集概述 字符集就是一套文字符号及其编码.比较规则的集合. ASCII(American Standard Code for Information Interchange)字符集 ...

  7. 同一个线程多次调用start()会出现的问题

    结果: 线程首先会运行一次,然后抛出java.lang.IllegalThreadStateException异常. 根据控制台的异常信息,定位到Thread.java的第708行,也就start() ...

  8. Autel MaxiSys MS906TS tire pressure settings Lexus LS460h

    Use AUTEL MaxiSYS MS906TS error reader to install tire pressure Lexus LS460h in Vung Tau. Make : Lex ...

  9. 零基础照样做RNA-seq差异分析

    零基础照样做RNA-seq差异分析 GCBI知识库2018-08-24 14:43:36 基因表达谱的差异分析是RNA-seq中最常见的应用.你眼中的RNA-seq差异分析或许是酱紫的,对不会编程,不 ...

  10. CH#56C 异象石

    一道LCA 原题链接 先跑一边\(dfs\),求出每个节点的时间戳,如果我们将有异象石的节点按时间戳从小到大的顺序排列,累加相邻两节点之间的距离(首尾相邻),会发现总和就是答案的两倍. 于是我们只需要 ...