Permutation Counting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1487    Accepted Submission(s): 754

Problem Description
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.
 
Input
There are several test cases, and one line for each case, which contains two integers, N and k. (1 <= N <= 1000, 0 <= k <= N). 
 
Output
Output 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
3 0
3 1
 
Sample Output
1
4

Hint

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}

 
Source
 
题意:对于任一种N的排列A,定义它的E值为序列中满足A[i]>i的数的个数。给定N和K(K<=N<=1000),问N的排列中E值为K的个数。
dp[i][j]表示前i个数的排列中E值为j的个数,所以当插入第i+1个数时,如果放在第i+1或满足条件的j个位置时,j不变,与其余i-j个位置上的数调换时j会+1。所以
dp[i+1][j] = dp[i+1][j] + (j + 1) * dp[i][j];
dp[i+1][j+1] = dp[i+1][j+1] + (i - j) * dp[i][j];
 
  1. /*
  2. ID: LinKArftc
  3. PROG: 3664.cpp
  4. LANG: C++
  5. */
  6.  
  7. #include <map>
  8. #include <set>
  9. #include <cmath>
  10. #include <stack>
  11. #include <queue>
  12. #include <vector>
  13. #include <cstdio>
  14. #include <string>
  15. #include <utility>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <iostream>
  19. #include <algorithm>
  20. using namespace std;
  21. #define eps 1e-8
  22. #define randin srand((unsigned int)time(NULL))
  23. #define input freopen("input.txt","r",stdin)
  24. #define debug(s) cout << "s = " << s << endl;
  25. #define outstars cout << "*************" << endl;
  26. const double PI = acos(-1.0);
  27. const double e = exp(1.0);
  28. const int inf = 0x3f3f3f3f;
  29. const int INF = 0x7fffffff;
  30. typedef long long ll;
  31.  
  32. const int maxn = ;
  33. const int MOD = ;
  34.  
  35. ll dp[maxn][maxn];
  36. int n, k;
  37.  
  38. void init() {
  39. dp[][] = ;
  40. dp[][] = ;
  41. for (int i = ; i <= ; i ++) {
  42. for (int j = ; j <= ; j ++) {
  43. dp[i+][j] = (dp[i+][j] % MOD + (j + ) * dp[i][j] % MOD) % MOD;
  44. dp[i+][j+] = (dp[i+][j+] % MOD + (i - j) * dp[i][j] % MOD) % MOD;
  45. }
  46. }
  47. }
  48.  
  49. int main() {
  50.  
  51. init();
  52. while (~scanf("%d %d", &n, &k)) {
  53. printf("%d\n", dp[n][k]);
  54. }
  55.  
  56. return ;
  57. }
 

HDU3664 Permutation Counting的更多相关文章

  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 - 3664 Permutation Counting

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

  5. HDU 3664 Permutation Counting (DP)

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

  6. HDU 6880 Permutation Counting dp

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

  7. UVALive 5971

    Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...

  8. LeetCode_Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. CH3602 Counting Swaps

    题意 3602 Counting Swaps 0x30「数学知识」例题 背景 https://ipsc.ksp.sk/2016/real/problems/c.html Just like yeste ...

随机推荐

  1. 九度OJ--Q1473

    import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 大家都知道,数据在计算机里中存储是以二进制的形式存储的. * 有一天 ...

  2. Python 3 学习笔记之——数据类型

    1. 数字 类型 int, float, bool, complex type() 查看变量类型 isinstance(a, int) 查看变量类型 运算符 % 取余 // 返回商的整数部分 ** 幂 ...

  3. Pro Git - 笔记2

    Git Basics Getting a Git Repository Initializing a Repository in an Existing Directory For Linux: $ ...

  4. lintcode-91-最小调整代价

    91-最小调整代价 给一个整数数组,调整每个数的大小,使得相邻的两个数的差不大于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少. 注意事项 你可以假设数组 ...

  5. JDK源码分析 – Integer

    Integer类的申明 public final class Integer extends Number implements Comparable<Integer> { … } Int ...

  6. java第七笔记

  7. 在Linux下调试Python代码的各种方法

    这是一个我用于调试或分析工具概述,不一定是完整全面,如果你知道更好的工具,请在评论处标记. 日志 是的,的确,不得不强调足够的日志记录对应用程序是多么的重要.您应该记录重要的东西,如果你的记录足够好的 ...

  8. Python执行Linux系统命令的4种方法

    http://www.jb51.net/article/56490.htm (1) os.system 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 复制代码代码如下: system( ...

  9. 【Python】Python学习----第一模块笔记

    1.python是什么? python是动态解释型的强类型定义语言. python官方版本的解释器是CPython.该解释器使用C语言开发. 当前主要使用3.x版本的python. 2.第一个pyth ...

  10. 【bzoj2815】[ZJOI2012]灾难 拓扑排序+倍增LCA

    题目描述(转自洛谷) 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过生物 ...