由搜索过渡到dp的方法就是将考察的对象由搜索的单层转变为当前层之前所有层构成的整体。

/*
    ID:chenjiong
    PROG:subset
    LANG:C++
*/

#include <stdio.h>
#include <string.h>

const int MAXN = 40;

int N;
int dp[MAXN][MAXN * MAXN];
int sum;

void solve()
{
    int i,j;
    dp[1][1] = 1;
    dp[1][0] = 1;
    for ( i = 2; i <= N; i++)
    {
        for ( j = 0; j <= sum; j++)
        {
            if ( j >= i )
                dp[i][j] = dp[i - 1][j] + dp[i - 1][j - i];
            else
                dp[i][j] = dp[i - 1][j];
        }
    }
}

int main()
{
    freopen("subset.in","r",stdin);
    freopen("subset.out","w",stdout);

    scanf("%d",&N);

    if ( ( N * ( N + 1 ) ) % 4 != 0 )
    {
        printf("0\n");
        return 0;
    }

    sum = N * ( N + 1 ) / 2;
    solve();

    printf("%d\n",dp[N][sum / 2] / 2);

    return 0;
}

 

posted on 2012-11-12 23:04  Sinker  阅读(130)  评论(0编辑  收藏  举报