题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数。

析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的。

dp[i][j]表示 i 个排列,恰好有 j 个数,dp[i][j] = dp[i-1][j] * (j+1) + dp[i-1][j-1] * (i-j)。这是状态转移方程。

为什么是这样呢,dp[i-1][j] * (j+1) 意思是,你前i-1个已经凑够 j 个了,那么我把 i 可以去替换这个 j 个任何一个,再加上,把这个 i 放在最后,

一共是 j+1个,所以乘以它。

dp[i-1][j-1] * (i-j) 意思是已经够 j-1了,还差一个,然后那一个必须是 i ,所以用 i 去替换 i-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 <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
LL dp[maxn][maxn]; void init(){
memset(dp, 0, sizeof(dp));
for(int i = 0; i <= 1000; ++i) dp[i][0] = 1; for(int i = 1; i <= 1000; ++i)
for(int j = 0; j <= i; ++j)
dp[i][j] = (dp[i-1][j] * (j+1) + dp[i-1][j-1] * (i-j)) % mod;
} int main(){
init();
while(scanf("%d %d", &n, &m) == 2) printf("%I64d\n", dp[n][m]);
return 0;
}

  

HDU 3664 Permutation Counting (DP)的更多相关文章

  1. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  2. hdu 3664 Permutation Counting(水DP)

    Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. HDU - 3664 Permutation Counting 排列规律dp

    Permutation Counting Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the ...

  4. HDU 5791:Two(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description   Alice gets two sequences A ...

  5. HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))

    Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...

  6. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  7. Codeforces 954H Path Counting(DP)

    题目链接  Path Counting 题意  给定一棵高度为$n$的树,给出每一层的每个点的儿子个数(某一层的所有点儿子个数相同).   令$f_{k}$为长度为$k$的路径条数,求$f_{1}, ...

  8. jzoj6009. 【THUWC2019模拟2019.1.18】Counting (dp)

    Description 羽月最近发现,她发动能力的过程是这样的: 构建一个 V 个点的有向图 G,初始为没有任何边,接下来羽月在脑中构建出一个长度为 E 的边的序列,序列中元素两两不同,然后羽月将这些 ...

  9. HDU 4833 Best Financing (DP)

    Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. Struts2系列——struts2的result

    在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...

  2. mysql优化小技巧

    对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索引.主键索引.唯一索引unique.全文索引] c: 分表技术(水平 ...

  3. Oracle 课程三之表设计

    完成本课程的学习后,您应该能够: •普通堆表优点和缺点 •理解rowid •全局临时表优点.缺点和适用场景 •分区表的类型和原理.优点和缺点.适用场景 •表字段的高效设计 •sequence的设计   ...

  4. Ch04-文字列表的设计

    Ch04: 文字列表的设计 4.1 编号列表 语法: <OL> <li>编号1 <li>编号2 ...... </OL> 属性: 编号类型:type=1 ...

  5. 不同语言的Unix时间戳

    如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScript Math.round(new Date().getTime()/1000)ge ...

  6. [Papers]NSE, $\pi$, Lorentz space [Suzuki, NA, 2012]

    $$\bex \sen{\pi}_{L^{s,\infty}(0,T;L^{q,\infty}(\bbR^3))} \leq \ve_*, \eex$$ with $$\bex \frac{2}{s} ...

  7. .net-.net试题2

    ylbtech-doc:.net-.net试题2 .net试题2 1.A,.net试题2返回顶部 01.{DotNet题目}下列变量的赋值,正确的是:(  )(选择2项) A )int myInteg ...

  8. Mybatis拦截器介绍

    拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...

  9. Apache:如何利用.htaccess文件对PHP网站或文件进行伪静态处理

    来源:http://www.ido321.com/1123.html 今天get了一招:利用.htaccess文件对PHP网站或文件进行伪静态处理. 一.检查服务器是否支持伪静态处理: 必 须要空间支 ...

  10. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...