题目在这里:https://www.luogu.org/problem/show?pid=1466#sub
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n; long long f[50][5050]={0};//how many methods are there to make up j with the first i numbers int main() { freopen("subset.in","r",stdin); freopen("subset.out","w",stdout); cin>>n; if(n==0||n*(n+1)%4!=0) { cout<<"0"<<endl; return 0; } int s=n*(n+1)/2; s/=2; f[1][1]=1; f[1][0]=1; for(int i=2;i<=n;i++) for(int j=s;j>=0;j--)//分两种情况:i能拼起j(取i或不取), 或不能(只能不取) if(j>=i) f[i][j]=f[i-1][j-i]+f[i-1][j]; else f[i][j]=f[i-1][j]; cout<<f[n][s]/2<<endl; return 0; }
#include<cstdio> int n,s; long long f[400]={0}; int main() { scanf("%d",&n); s=n*(n+1)/2; if (s&1) { printf("0"); return 0; } s/=2; f[0]=1; for(int i=1;i<=n;i++) for(int j=s;j>=i;j--) f[j]+=f[j-i]; printf("%d\n",(long long)f[s]/2); return 0; }
USACO 2.2 Subset Sums 【经典的方案DP+必要的转化】
原文:http://www.cnblogs.com/linda-fcj/p/7206116.html