Permutation Counting

Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the amount of elements where ai > i. For example, the E-value of permutation {1, 3, 2, 4} is 1, while the E-value of {4, 3, 2, 1} is 2. You are requested to find how many permutations of {1, 2, …, N} whose E-value is exactly k.

InputThere are several test cases, and one line for each case, which contains two integers, N and k. (1 <= N <= 1000, 0 <= k <= N). 
OutputOutput one line for each case. For the answer may be quite huge, you need to output the answer module 1,000,000,007.Sample Input

  1. 3 0
  2. 3 1

Sample Output

  1. 1
  2. 4

Hint

  1. There is only one permutation with E-value 0: {1,2,3}, and there are four permutations with E-value 1: {1,3,2}, {2,1,3}, {3,1,2}, {3,2,1}
  2.  
  3. 数列1-n,可以随意排列组合,求恰有ka[i]>i的排列个数。
  1. 一道找规律的dp。看数据范围就知道不能暴力求解,但可以用暴力找出n较小的几种小数列排列数,发现规律。类似杨辉三角,就像两数和靠拢,于是可以发现状态转移方程f[i][j]=(((i+1)*f[i][j-1])%MOD+((j-i)*f[i-1][j-1])%MOD)%MOD
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<string>
  5. #include<math.h>
  6. #include<queue>
  7. #include<set>
  8. #include<stack>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<iterator>
  12. #define MAX 1005
  13. #define INF 0x3f3f3f3f
  14. #define MOD 1000000007
  15. using namespace std;
  16.  
  17. typedef long long ll;
  18.  
  19. ll f[MAX][MAX];
  20.  
  21. int main()
  22. {
  23. int n,m,i,j;
  24. for(i=;i<=;i++){
  25. f[][i]=;
  26. }
  27. for(i=;i<=;i++){
  28. for(j=i+;j<=;j++){
  29. f[i][j]=(((i+)*f[i][j-])%MOD+((j-i)*f[i-][j-])%MOD)%MOD;
  30. }
  31. }
  32. while(~scanf("%d%d",&n,&m)){
  33. if(n==m){
  34. printf("0\n");
  35. continue;
  36. }
  37. printf("%lld\n",f[m][n]);
  38. }
  39. return ;
  40. }
  1.  

HDU - 3664 Permutation Counting 排列规律dp的更多相关文章

  1. hdu 3664 Permutation Counting(水DP)

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

  2. HDU 3664 Permutation Counting (DP)

    题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...

  3. HDU - 3664 Permutation Counting

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

  4. hdu3664 Permutation Counting(dp)

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

  5. HDU 6880 Permutation Counting dp

    题意: 给你一个n和一个长度为n-1的由0/1构成的b序列 你需要从[1,n]中构造出来一个满足b序列的序列 我们设使用[1,n]构成的序列为a,那么如果ai>ai+1,那么bi=1,否则bi= ...

  6. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  7. HDU3664 Permutation Counting

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

  8. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  9. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

随机推荐

  1. toggle不支持事件代理的解决办法或者 jquery live绑定toggle

    $(".xxx").live("click", function () {      $(this).toggle(function () {},functio ...

  2. Linux就该这么学--了解Shell脚本

    有人曾经将Shell形容是人与计算机硬件的“翻译官”,Shell作为用户与Linux系统通讯的媒介.自身也定义了各种变量和参数,并提供了诸如循环.分支等高级语言才有的控制结构特性.如何正确的使用这些功 ...

  3. IDEA tomcat三步实现热部署

    IDEA tomcat三步实现热部署 添加Artifacts选项 添加Artifacts选项,XXXwar 和 XXXwar exploded二选一,若要热部署须选后者. 设置tomcat部署方案 设 ...

  4. flask的请求上下文源码解读

    一.flask请求上下文源码解读 通过上篇源码分析( ---Flask中的CBV和上下文管理--- ),我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__ ...

  5. Java多线程系列 基础篇02 线程的创建和运行

    1.线程创建的方式常用有两种 1. 继承 Thread 类创建线程 2. 实现 Runnable 接口创建线程 2.Thread 和 Runnable的区别 Thread和Runnable的相同点:都 ...

  6. ajax 异步 跨域上传图片

    客户端 <label for="text">名称</label> <input type="text" id="text ...

  7. 项目发布之后 总提示有一个.DLL找不到或不匹配

    最近发布项目(.net,winform)总提示有一个.dll文件找不到或者不匹配 但是在本地调试是正常的 这个.dll,原来是从.net组件中引用到项目的,后来我将此.dll文件从网上下载,然后在项目 ...

  8. 如何使用安信可 ESP 系列一体化开发环境【转】

    本文转载自:http://wiki.ai-thinker.com/ai_ide_use 关于 Problems 报错 注意:Eclipse 只是一个代码编写工具,它并不能读取 makefile 里面的 ...

  9. 算法(Algorithms)第4版 练习 1.3.219

    方法实现: //1.3.19 /** * remove the last node in the linked list whose first node is first * * @return r ...

  10. Contiki 2.7 Makefile 文件(一)

    一.主控Makefile 这里以hello-world例子为主线,从其工程Makefile开始,解析整个build过程.