hdu 2583 permutation 好题 递推

permutation

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


Problem Description
Permutation plays a very important role in Combinatorics. For example ,1 2 3 4 5 and 1 3 5 4 2 are both 5-permutations. As everyone's known, the number of n-permutations is n!. According to their magnitude relatives ,if we insert the sumbols "<" or ">"between every pairs of consecutive numbers of a permutations,we can get the permutations with symbols. For example,1 2 3 4 5 can be changed to 1<2<3<4<5, 1 3 5 4 2 can be changed to 1<3<5>4>2. Now it's yout task to calculate the number of n-permutations with k"<"symbol. Maybe you don't like large numbers ,so you should just geve the result mod 2009.
 

Input
Input may contai multiple test cases.
Each test case is a line contains two integers n and k .0 The input will terminated by EOF.
 

Output
The nonegative integer result mod 2007 on a line.
 

Sample Input

5 2
 

Sample Output

66
 

Source
ECJTU 2009 Spring Contest
 

Recommend
lcy
 



题意:   输入  n m  问n组成的全排列中  有m个小于号的排列的个数


思路:

我们设

n(<=100)个数 1,2,...,n的置换有n!个 ,有k个小于号的有 f[n][k]个。

假如

a[1],a[2],...,a[n-1],是n-1个数的置换,有 k个'<'

把 n插入,有n个位置可以插入,
    把 n插入到a[1]的前面, '<'没有增加 ,  1个位置 
    如 a[i]

插入到其他位置 '<'增加
   因此
   f[n][k]=f[n-1][k]*(k+1)   + f[n-1][k-1] *(n-k)


思路来自:http://hi.baidu.com/fhhgoeqlilqrtxr/item/4c8c0b768905a7336dc37cc4


#include
int ans[111][111];
int main()
{
    int n,k,i,j;
    for(i=0;i<=100;i++)
    {
        ans[i][0]=1;
        ans[0][i]=0;
    }
    for(i=1;i<=100;i++)
        for(j=1;j<=100;j++)
           {
               ans[i][j]=ans[i-1][j]*(j+1)+ans[i-1][j-1]*(i-j);
               ans[i][j]%=2009;
           }
    while(scanf("%d %d",&n,&k)!=EOF)
    {
             printf("%d\n",ans[n][k]);
    }
    return 0;
}




你可能感兴趣的:(技巧分析题)