USACO 2.3.4

 

#include <iostream>
#include<fstream>
#include<cstring>

using namespace std;

ifstream infile;
ofstream outfile;

long long dp[40][800];

int n,sum;

int main(){
    
    infile.open("subset.in");
    outfile.open("subset.out");
    
    infile>>n;
    //cin>>n;
    sum = (n+1)*n/2;
    
    if(sum % 2 == 1){
        outfile<<0<<endl;
        return 0;
    }
    
    memset(dp,0,sizeof(dp));
    
    dp[1][1]  = dp[1][0] = 1;
    for(int i=2;i<=n;i++){
        for(int j= 0 ;j<=sum;j++){
            if(j<i)
                dp[i][j] = dp[i-1][j];
            else 
                dp[i][j] = dp[i-1][j] + dp[i-1][j-i];
        }
        
        
    }
    
    outfile<<dp[n][sum/2]/2<<endl;
    //cout<<dp[n][sum/2]/2<<endl;
    return 0;
    
}

 

 

 

 

#include <iostream>
#include<fstream>
#include<cstring>

using namespace std;

ifstream infile;
ofstream outfile;
int n,sum,curSum = 0;
int c = 0;
int path[40];

void dfs(int step);

int main(){
    
    infile.open("subset.in");
    outfile.open("subset.out");
    infile>>n;
    
    sum = n*(n+1)/2;
    if(sum % 2 == 1)
        return 0;
    
    sum/=2;
    
    dfs(1);
    
    outfile<<c/2<<endl;
    
    return 0;
    
}

void dfs(int step){
    if(curSum > sum)
        return;
    
    if(curSum == sum){
        c++;
        return ;
    }
        
    int s = path[step-1]+1;
    for(int i=s;i<=n;i++){
        path[step] = i;
        curSum+=i;
        dfs(step+1);
        curSum-=i;
    }

}


你可能感兴趣的:(USACO 2.3.4)