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];
 
/*
ID: LinKArftc
PROG: 3664.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int MOD = ; ll dp[maxn][maxn];
int n, k; void init() {
dp[][] = ;
dp[][] = ;
for (int i = ; i <= ; i ++) {
for (int j = ; j <= ; j ++) {
dp[i+][j] = (dp[i+][j] % MOD + (j + ) * dp[i][j] % MOD) % MOD;
dp[i+][j+] = (dp[i+][j+] % MOD + (i - j) * dp[i][j] % MOD) % MOD;
}
}
} int main() { init();
while (~scanf("%d %d", &n, &k)) {
printf("%d\n", dp[n][k]);
} return ;
}
 

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. Python-学习-小例子练习

    网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...

  2. fidder工具学习抓取Firefox包

    fidder抓取Firefox的https请求 抓包之前需要设置fidder,我下面的截图是fidder4,打开fidder—>Tools—>Options如图: 选择https,勾选所有 ...

  3. 第八篇Python基本数据类型之列表、元组与字典

    列表 写在最前,必须要会的:append(),extend(),insert(),索引,切片,循环 list  是一个类,是个对象 列表用 方括号[]括起来的,[]内以逗号分割每个元素,列表中的元素可 ...

  4. cocos2d-x 键盘和鼠标事件

    出了菜单可以响应用户事件外,cocos2d中的层(Layer)也可以响应事件.层能够自动响应窗口事件,这些事件主要是键盘和鼠标事件,cocos2d中事件处理是通过Pyglet的事件处理完成的. 1.键 ...

  5. final static 修饰(转载)

    static修饰符        static修饰符能够与属性.方法和内部类一起使用,表示静态的.类中的静态变量和静态方法能够与类名一起使用,不需要创建一个类的对象来访问该类的静态成员,所以,stat ...

  6. Hadoop伪分布式安装步骤(hadoop0.20.2版本)

    最近在学习hadoop,自己下了个视频教程,他的教学版本是hadoop0.20.2版本,现在的最新版本都到了3.0了,版本虽然有点老,但是还是学了一下,觉得有借鉴的价值. 不废话了,开始介绍: 先说一 ...

  7. dechex()

    dechex() 函数把十进制转换为十六进制生成验证码的时候用到了

  8. sudo是干哈子的

    我sudo loop发现啊大家就都是root了,那么这和我直接用root起有啥区别呢? root      3826  0.0  0.1  56596  3984 pts/2    S+   12:5 ...

  9. 找jar包的网址

    http://search.maven.org/#search%7Cga%7C1%7Cmybatis http://mvnrepository.com/

  10. DataBase -- Customers Who Never Order

    Question: Suppose that a website contains two tables, the Customers table and the Orders table. Writ ...