usaco2.3.2奶牛家谱

来源:互联网 发布:mac双系统 分区 编辑:程序博客网 时间:2024/04/28 12:39

本来是这样写的,但这样有些费时,重复计算了很多算过了的,所以超时,同时,因为高度有100,那么按我的方法,贮存最大节点数的变量会出现溢出,这也证明,这一题不用dp不行啊。。。。。。

#include<iostream>

#include<cstdio>
#include<cstdlib>
#include <cmath>
using namespace std;
int n,h;
int aaa(int h1,int n1,int key);
int main()
{
  //freopen("nocows.in","r",stdin);
  //freopen("nocows.out","w",stdout);
  cin>>n>>h;
  int num=aaa(h,n,1);
  cout<<num%9901<<endl;
 return 0;
}
int aaa(int h1,int n1,int key)
{
    if(h1<1)return 0;
    if(pow(2,h1)-1<n1)return 0;
     else if(pow(2,h1)-1==n1&&key==1)return 1;
     else if(key==2&&pow(2,h1-1)-1==n1)return 1;
    if(key==1){
            if(n1<2*h1-1)return 0;
    int k1=(int)pow(2,h1-1)-1,sum=0;
    for(int i=2*h1-3;i<=k1&&i<=n1;i+=2){
        if(n1-1-i<=k1&&n1-1-i>0){


            int a=aaa(h1-1,i,1);
            int b=aaa(h1-1,n1-i-1,2);
            int c=aaa(h1-1,n1-i-1,1);
            sum+=(a*b*2+a*c);
        }
        }
    return sum;
    }
    else {
        int sum=0;
        for(int i=1;i<h1;i++){
            sum+=aaa(i,n1,1);
        }


        return sum;
    }

}

对的代码:

/*
ID:thy47021
LANG:C++
TASK:nocows
*/


#include <cstdio>
#include <iostream>
using namespace std;
int n,k,dp[200][200];
int main()
{
    freopen("nocows.in", "r", stdin);
freopen("nocows.out", "w", stdout);
     cin>>n>>k;
    dp[1][1]=1;
    for(int i=3;i<=n;i+=2)
      for(int j=1;j<=n-2;j+=2)
        for(int h1=1;h1<=(j+1)/2;h1++)
        for(int h2=1;h2<=(i-j)/2;h2++)
          {dp[i][max(h1,h2)+1]+=dp[j][h1]*dp[i-j-1][h2];
          dp[i][max(h1,h2)+1]%=9901;
          }
          cout<<dp[n][k]<<endl;
    return 0;
}

0 0