一.题目描述
For many sets of consecutive integers from 1 through N (1 <= N <= 39), one can partition the set into two sets whose sums are identical.
For example, if N=3, one can partition the set {1, 2, 3} in one way so that the sums of both subsets are identical:
•{3} and {1,2}
This counts as a single partitioning (i.e., reversing the order counts as the same partitioning and thus does not increase the count of partitions).
If N=7, there are four ways to partition the set {1, 2, 3, ... 7} so that each partition has the same sum:
•{1,6,7} and {2,3,4,5}
•{2,5,7} and {1,3,4,6}
•{3,4,7} and {1,2,5,6}
•{1,2,4,7} and {3,5,6}
Given N, your program should print the number of ways a set containing the integers from 1 through N can be partitioned into two sets whose sums are identical. Print 0 if there are no such ways.
Your program must calculate the answer, not look it up from a table.
PROGRAM NAME: subset
INPUT FORMAT
The input file contains a single line with a single integer representing N, as above.
SAMPLE INPUT (file subset.in)
7
OUTPUT FORMAT
The output file contains a single line with a single integer that tells how many same-sum partitions can be made from the set {1, 2, ..., N}. The output file should contain 0 if there are no ways to make a same-sum partition.
SAMPLE OUTPUT (file subset.out)
4
二.题目分析
题目等效于从1,2,,,,N中找到和为sum/2的种类数,可以使用DFS搜索所有组合,但是肯定会超时,如代码1中。采用动态规划的方式,重要的是找到转态转移方程.
dp[i,j]表示在前i个序列中和为j的个数,可以这样想,对于第i个物品,当i>j时,我们要想得到和为j,肯定不能放入i,此时dp[i,j]=dp[i-1,j];当i<=j时,我们可以选择放入或者不放入两种情况,当不放入时情况同上,当放入i时,由于i的价值即值是i,因此这时的种类数应该等于前i-1个序列中何为j-i的种类数,得到dp[i,j]=dp[i-1,j-i];
综合上述分析,可以得到:
j<i时, dp[i,j]=dp[i-1,j]
j>=i时, dp[i,j]=dp[i-1,j]+dp[i-1,j-i];
采用简化的方式,可以得到 j>=i时,dp[j] += dp[j-i];
三.代码
1.DFS (TLE)
#include <stdio.h>
#include <stdlib.h>
int cn,sum,N;
void DFS(int n,int total) //total after add n
{
if(n==N+1)
return ;
if(total>sum)
return;
if(total==sum)
{
cn++;
return ;
}
//(n,total);
DFS(n+1,total+n+1); //深入方向搜索
DFS(n+1,total); //水平方向搜索
}
int main()
{
int i;
FILE *in=NULL,*out=NULL;
in=fopen("subset.in","r"),out=fopen("subset.out","w");
if(!in||!out)
{
printf("file open error!\n");
return -1;
}
fscanf(in,"%d",&N);
sum=0;
for(i=1;i<=N;i++)
sum+=i;
if(sum%2!=0)
{
printf("0\n");
return 0;
}
cn=0; //all possible
sum /=2;
DFS(0,0);
fprintf(out,"%d\n",cn/2);
return 0;
}
2.DP(AC)
#include<stdio.h>
#include<stdlib.h>
#define MAX 800
int main()
{
FILE *in,*out;
long long**f;
int i,j,N,sum=0,s=1;
in=fopen("subset.in","r");
out=fopen("subset.out","w");
if(in==NULL||out==NULL)
{
printf("error!\n");
return -1;
}
fscanf(in,"%d",&N);
f=(long long**)malloc(MAX*sizeof(long long*));
for(i=0;i<MAX;i++)
f[i]=(long long*)malloc(MAX*sizeof(long long));
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
f[i][j]=0;
sum=N*(N+1)/2 ;
if(sum%2!=0)
{
fprintf(out,"0\n");
return 0;
}
sum=sum/2;
f[1][1]=1;
f[1][0]=1;
for(i=2;i<=N;i++)
{
for(j=0;j<=sum;j++)
{
if(j<i) //肯定是不能取i
f[i][j]=f[i-1][j];
else //分为取i / 不取i
f[i][j]=f[i-1][j]+f[i-1][j-i];
}
}
fprintf(out,"%lld\n",f[N][sum]/2);
return 0;
}
3.DP(AC)
#include <fstream>
using namespace std;
const unsigned int MAX_SUM = 1024;
int n;
unsigned long long int dyn[MAX_SUM];
ifstream fin ("subset.in");
ofstream fout ("subset.out");
int main() {
fin >> n;
fin.close();
int s = n*(n+1);
if (s % 4) {
fout << 0 << endl;
fout.close ();
return ;
}
s /= 4;
int i, j;
dyn [0] = 1;
for (i = 1; i <= n; i++)
for (j = s; j >= i; j--)
dyn[j] += dyn[j-i];
fout << (dyn[s]/2) << endl;
fout.close();
return 0;
}