HDU 3664 Permutation Counting(DP)

来源:互联网 发布:西单有mac专柜吗 编辑:程序博客网 时间:2024/05/21 06:32

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 03 1

Sample Output

14          

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}


题意:给出N,一个数列中包含从1到N的N个数,现在让这个数列进行重排,当ai>i时,这个元素对数列的E—value值贡献1。

问:给出N和k(数列的E—value值),问初始数列有多少种重排方法,使其E—value值为k。

思路:先列出1-3的的所有情况

 N\K     0       1       2       3

1          1       0

2          1        1      0

3          1        4      1        0

当 n为2 ,k为 0 时有一种排列(1,2),k为1 时有一种排列(2,1)

当 n为3 ,k为 1 时只需在n为2,k为 0 和 1 的情况下插入3使E—value值加1或不变即可……

当n为2, k为 0 时,有 两个位置对E-value没有贡献,把3插入没有贡献的位置,把之前的数放到数列最后,这样E-value值则为1,有2种方法。

当n为2, k为 1 时,当前k值为所求E-value值,所有把3插入有贡献的位置,把之前的数放到最后,这样有1种方法。另外把3放到最后也是一种方法,所以有2种方法。

因此得到递推方程d[i][j]=d[i-1][j]*(j+1)+d[i-1][j-1]*(i-j)

细节参见代码:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<map>#include<vector>#include<algorithm>#include<queue>#include<cmath>#include<sstream>using namespace std;typedef long long int LL;int n,k;LL d[1005][1005];int mod=1e9+7;int main(){    memset(d,0,sizeof(d));    d[1][0]=1;d[1][1]=0;    for(int i=1;i<=1001;i++) d[i][0]=1;    for(int i=2;i<=1001;i++){       for(int j=1;j<=1001;j++)        d[i][j]=((d[i-1][j]*(j+1))%mod+(d[i-1][j-1]*(i-j))%mod)%mod;    }    while(~scanf("%d%d",&n,&k)){        printf("%I64d\n",d[n][k]);    }    return 0;}


0 0