USACO 2.2 Subset Sums集合
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
Description
对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的。举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字和是相等的:
{3} and {1,2}
这是唯一一种分法(交换集合位置被认为是同一种划分方案,因此不会增加划分方案总数)如果N=7,有四种方法能划分集合{1,2,3,4,5,6,7},每一种分发的子集合各数字和是相等的:
{1,6,7} and {2,3,4,5} {注 1+6+7=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}
给出N,你的程序应该输出划分方案总数,如果不存在这样的划分方案,则输出0。程序不能预存结果直接输出。
Input
输入文件只有一行,且只有一个整数N
Output
输出划分方案总数,如果不存在则输出0。
Sample Input
7
Sample Output
4
刚看到这题完全不知道怎么下手,就是枚举都不知道怎么下手的好,就直接看了看分析:n个数字的累加和为tot,只有当tot为偶数的时候题目才可能有解。一下就有点感觉了,在题目给出的例子中左边的集合的和等于右边的也等于整个集合{1...N}的和的一半,所以只有当tot为偶数的时候才能够有解。
忽然感觉灵光爆满,知道怎么写了,可是仔细想一想,就是枚举我也还是不知道怎么下手的好啊。
今天做出来了, 首先是自己做了一个枚举的, 提交测试一看, 果然第4个测试点超时, 后来看了网上的分析, 其实昨天我就看了分析, 只是没看懂而已, 后来自己的思考, 加上爸爸的一句话的指点, 就弄懂了, 自然而然的AC了.
思路就是一个DP的思路, DP的公式就是:
将数据{1...N}分成两个子集, f[i][j]就表示前i个数能够组合成和是j的个数. 最后要输出的也就是f[n][n*(n+1)/4].
然后DP方程就是: f[i][j] = f[i - 1][j] + f[i - 1][j - i]
意思就是说前i个数能够凑成和为j的个数是, 前i-1个数能够凑成和为j的数加上前i-1个数能够凑成和为j - i的个数. f[i - 1][j] 比较好理解, 但是f[i - 1][j - i]难理解一点, 后者的意思就是说前面i - 1个只需要凑成j - i就够了, 因为第i个数也能够加进来了..
不知道说清楚没有, 总之我是看得懂自己写的这些东西,(貌似是废话..)
但是还有一种情况要考虑阿, 因为这是动态规划, 而且使用两个循环来实习的, 如
for(i = 2; i <= n; i++)
for(j = 1; j <= tot; j++)
.....
在这种情况下, 如果直接把上面的"...."替换成f[I][j] = f[i - 1][j] + f[i - 1][j - i]就错了(Ps: 我提交的时候就是直接这样子的, 不知道为什么两个网站AC了~~), 因为j - i可能会小于0 自然而然是不合法的, 所以需要判断一下, 如果j - i < 0的话f[i][j] = f[i - 1][j]..
/*
LANG: C
ID: zqy11001
PROG: subset
*/
#include <stdio.h>
#define min(a, b) ((a) < (b) ? (a) : (b))
int f[40][390];
int main(void)
{
int n, tot;
int i, j,t;
freopen("subset.in", "r", stdin);
freopen("subset.out", "w", stdout);
scanf("%d", &n);
tot = n*(n + 1) >> 1;
if(tot & 1){
printf("0\n");
return 0;
}
tot = tot >> 1;
f[1][1] = 1;
for(i = 2; i <= n; i++){
for(j = 1; j <= tot; j++){
f[i][j] = f[i - 1][j];
if(j - i >= 0){
f[i][j] += f[i - 1][j - i];
}
}
}
printf("%d\n", f[n][tot]);
return 0;
}