USACO Section 2.2 Subset Sums(整数划分01背包思想)
2012-09-01 11:29
417 查看
Subset SumsJRMFor 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 题意:输入一个 n ,从 1 到 n 这 n 个数分成两部分,这两部分的和相等,问这种方法有多少种。 分析:d[i][j] 表示前 i 个数组合起来的和是 j 的种数,对 j 考虑选择与不选择两种情况,不选:d[i-1][j],选择:d[i-1][j-i];所以d[i][j]=d[i-1][j]+d[i-1][j-i];View Code
/* ID: dizzy_l1 LANG: C++ TASK: subset */ #include<iostream> #include<cstring> #include<cstdio> using namespace std; long long d[1000]; int main() { freopen("subset.in","r",stdin); freopen("subset.out","w",stdout); int n,sumv,i,j; while(cin>>n) { sumv=(n+1)*n/2; if(sumv%2==1) { printf("0\n"); continue; } sumv=sumv/2; memset(d,0,sizeof(d)); d[0]=1; for(i=1;i<=n;i++) { for(j=sumv;j>=i;j--) { d[j]+=d[j-i]; } } cout<<d[sumv]/2<<endl; } return 0; }
相关文章推荐
- USACO Section 2.3 Money Systems (整数划分完全背包思想)
- USACO Section2.2 Subset Sums 解题报告 【icedream61】
- USACO Subset 整数划分01背包
- USACO section 2.2 Subset Sums(DP,背包)
- USACO section 2.2 Subset Sums(DP,背包)
- USACO Section 2.2 Subset Sums - 01背包的思想
- UVA - 10313 Pay the Price 整数的划分问题 01背包
- HDU6092 Rikka with Subset(01背包,2017 HDU多校联赛 第5场)
- USACO 2.2 Subset Sums 集合(subset)
- HDU 3446 有贪心思想的01背包
- 【USACO 2.2】Subset Sums (DP)
- NYOJ90-整数划分,经典递归思想~~
- USACO 2.2 Subset Sums(水DP)
- 整数划分问题(python)--递归 and 动态规划(m个盘里放n个苹果思想类似)
- nyoj927(01背包的思想)
- USACO-Section 2.2 Subset Sums (DP)
- 51nod 1007 正整数分组 (类01背包问题)
- 递归与分治01-整数划分
- hdu 6092 Rikka with Subset(逆向01背包+思维)
- POJ1745Divisibility(01背包思想)