Subset Sums
For many sets of consecutive integers from 1 through N (1
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)
具体看程序:
/*
ID:nealgav1
PROG:subset
LANG:C++
*/
#include
#include
using namespace std;
const int mm=2300;
long long f[60][mm];
long long dp(int x,int c)
{ memset(f,0,sizeof(f));
f[1][0]=1;f[1][1]=1;
for(int i=2;i=0)
f[i][j]=f[i-1][j]+f[i-1][j-i];
else f[i][j]=f[i-1][j];
/** 容量为j,个数为i的方法数有两种情况,一是个数为i-1,容量就已经是j了,这时不用
往里面放东西,另一种是当个数为i-1,容量为j-i;这时只要将个数i放入容量即为j;因此
f[i][j]=f[i-1][j]+f[i-1][j-i];*/
return f[x][c];
}
int main()
{ ifstream cin("subset.in");
ofstream cout("subset.out");
int m,ans;
cin>>m;
ans=((1+m)*m)/2;
if(ans&1){cout
USER: Neal Gavin Gavin [nealgav1]
TASK: subset
LANG: C++
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 4412 KB]
Test 2: TEST OK [0.000 secs, 4412 KB]
Test 3: TEST OK [0.011 secs, 4412 KB]
Test 4: TEST OK [0.011 secs, 4412 KB]
Test 5: TEST OK [0.011 secs, 4412 KB]
Test 6: TEST OK [0.011 secs, 4412 KB]
Test 7: TEST OK [0.000 secs, 4412 KB]
All tests OK.
YOUR PROGRAM (‘subset’) WORKED FIRST TIME! That’s fantastic
— and a rare thing. Please accept these special automated
congratulations.
Here are the test data inputs:
------- test 1 ----
7
------- test 2 ----
15
------- test 3 ----
24
------- test 4 ----
31
------- test 5 ----
36
------- test 6 ----
39
------- test 7 ----
37
Keep up the good work!
Thanks for your submission!
Subset SumsRob Kolstad
This is a classic dynamic programming problem. Hal’s solution is shown below.
/* Calculate how many two-way partitions of {1, 2, ..., N} are
even splits (the sums of the elements of both partition are equal) */
#include
#include
#define MAXSUM 637
unsigned int numsets[637][51];
int max;
unsigned int sum;
main(int argc, char **argv)
{
int lv, lv2, lv3;
int cnt;
FILE *fin, *fout;
fin = fopen ("subset.in", "r");
fscanf(fin, "%d", &max);
fclose (fin);
fout = fopen("subset.out", "w");
if ((max % 4) == 1 || (max % 4) == 2) {
fprintf (stderr, "0n");
exit(1);
}
sum = max * (max+1) / 4;
memset(numsets, 0, sizeof(numsets[0]));
numsets[0][0] = 1;
for (lv = 1; lv
and here’s an even more concise solution from Nick Tomitov of Bulgaria:
#include
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 = i; j--)
dyn[j] += dyn[j-i];
fout
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 【由浅入深学MySQL】- MySQL连接查询详解
本系列为:MySQL数据库详解,为千锋教育资深Java教学老师独家创作 致力于为大家讲解清晰MySQL数据库相关知识点,含有丰富的代码案例及讲解。如果感觉对大家有帮助的话,可以【点个关注】持续追更~ 文末有重点总结和福利内容! 技术类问题,也欢迎大家和我们沟通…