USACO 2.3 Cow Pedigrees(DP)

  不好刷了,这个题想了会,中间还想过卡特兰数列。。。不过好在按照第一感觉就应该是DP,想出了三维的状态转移。。。又MLE,也是不知道给出的提示是神马意思。。。改了滚动数组,过了。效率也不是很高,差点挂掉。dp[i][j][k] 代表第i层,总数为j,最后一层为k个的二叉树种类,然后在这k个上继续搞i+1层。

 1 /*

 2  ID: cuizhe

 3  LANG: C++

 4  TASK: nocows

 5 */

 6 #include <iostream>

 7 #include <cstdio>

 8 #include <cstring>

 9 #include <cmath>

10 #include <algorithm>

11 using namespace std;

12 #define MOD 9901

13 int c[201][201];

14 int dp1[201][201],dp2[201][201];

15 int main()

16 {

17     int n,k,i,j,u,ans,m;

18     freopen("nocows.in","r",stdin);

19     freopen("nocows.out","w",stdout);

20     for(i = 0;i <= 200;i ++)

21     c[i][0] = 1;

22     for(i = 1;i <= 200;i ++)

23     {

24         for(j = 1;j <= i;j ++)

25         c[i][j] = (c[i-1][j-1] + c[i-1][j])%MOD;

26     }

27     scanf("%d%d",&n,&m);

28     ans = 0;

29     dp1[1][1] = 1;

30     for(i = 1;i <= m-1;i ++)

31     {

32         for(j = 1;j <= n;j ++)

33         {

34             for(k = 1;k <= n&&k <= j;k ++)

35             {

36                 for(u = 1;j+2*u <= n;u ++)

37                 dp2[j+2*u][2*u] = (dp2[j+2*u][2*u]+c[k][u]*dp1[j][k])%MOD;

38             }

39         }

40         for(j = 1;j <= n;j ++)

41         {

42             for(k = 1;k <= n;k ++)

43             {

44                 dp1[j][k] = dp2[j][k];

45             }

46         }

47         memset(dp2,0,sizeof(dp2));

48     }

49     for(i = 1;i <= n;i ++)

50     {

51         ans = (ans + dp1[n][i])%MOD;

52     }

53     printf("%d\n",ans);

54     return 0;

55 }

你可能感兴趣的:(USACO)