hdoj 3664 Permutation Counting

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3664

解题思路:将 i 加入到已有的有 i-1 个数的排列中的方法有以下三种:

① 把 i 放最后,加入后 E 值不变

② 把 i 和一个满足 a[k]>k 的数交换,交换后 E 值不变

③ 把 i 和一个不满足 a[k]>k 的数交换,交换后 E 值加一

记 i 个数的排列中 E 值为 j 的个数为 dp[i][j], 则 dp[i][j]=dp[i-1][j]+dp[i-1][j]*j+dp[i-1][j-1]*(i-j).

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: hdoj 3664
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 typedef long long LL;
26 const double PI=acos(-1.0);
27 ///////////////////////////////////////////////////////////////////////////
28 
29 ///////////////////////////////////////////////////////////////////////////
30 //Add Code:
31 LL dp[1005][1005];
32 const LL Mod=1000000007;
33 ///////////////////////////////////////////////////////////////////////////
34 
35 int main(){
36     ///////////////////////////////////////////////////////////////////////
37     //Add code:
38     int n,k,i,j;
39     memset(dp,0,sizeof(dp));
40     for(i=1;i<=1000;i++){
41         dp[i][0]=1;
42         for(j=1;j<=i;j++){
43             dp[i][j]=dp[i-1][j]+dp[i-1][j]*j+dp[i-1][j-1]*(i-j);
44             dp[i][j]%=Mod;
45         }
46     }
47     while(scanf("%d%d",&n,&k)!=EOF) printf("%I64d\n",dp[n][k]);
48     ///////////////////////////////////////////////////////////////////////
49     return 0;
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////
53 /*
54 Testcase:
55 Input:
56 3 0
57 3 1
58 Output:
59 1
60 4
61 */
62 ///////////////////////////////////////////////////////////////////////////

posted on 2013-08-18 10:08  SCNU20102200088  阅读(180)  评论(0编辑  收藏  举报

导航