【算法】【动态规划】Subset Sum

问题描述:

 

When you divide numbers from 1 to N into two subsets, you can make each subset sum equal. For example, if N is 3, elements of the entire set are {1, 2, & 3}. In case each set sum is equal, you can divide them into {1, 2} and {3}. And also, if N is 7, elements of the entire set are {1, 2, 3, 4, 5, 6, & 7}. In case each set sum is equal, you can divide them as below:

{1, 6, 7}, {2, 3, 4, 5}
{2, 5, 7}, {1, 3, 4, 6}
{3, 4, 7}, {1, 2, 5, 6}
{1, 2, 4, 7}, {3, 5, 6}

 

As such, in a give N, use elements from 1 to N to divide into two subsets, and then figure out the number of methods to make the same sums of two subsets.

Time limit: 1 second (Java 2 second) 

 

[Input]
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
N is given on the first row per each test case. (1 ≤ N ≤39) 

 

[Output]
For each test case, you should print "Case #T" in the first line where T means the case number.
Then you print out the number of methods to make the sums of two subsets equal from the next line. When there is no method to make them equal, output zero, of course.

 

Sample Input 




2
7
4 

 

Sample Output 




Case #1
4
Case #2
1 

分析:

动态规划

源码:

/*
You should use the statndard input/output

in order to receive a score properly.

Do not use file input and output

Please be very careful.
*/

#include <iostream>

using namespace std;

int Answer;
int N;
int Value;
long long dyn[391];

int main(int argc, char** argv)
{
	int T, test_case;
	/*
	The freopen function below opens input.txt file in read only mode, and afterward,
	the program will read from input.txt file instead of standard(keyboard) input.
	To test your program, you may save input data in input.txt file,
	and use freopen function to read from the file when using cin function.
	You may remove the comment symbols(//) in the below statement and use it.
	Use #include<cstdio> or #include <stdio.h> to use the function in your program.
	But before submission, you must remove the freopen function or rewrite comment symbols(//).
	*/

	// freopen("input.txt", "r", stdin);

	cin >> T;
	for (test_case = 0; test_case < T; test_case++)
	{

		/
		/*
		Implement your algorithm here.
		The answer to the case will be stored in variable Answer.
		*/
		/
		memset(dyn, 0, sizeof(dyn));

		Answer = 0;
		Value = 0;
		cin >> N;
		
		Value = N*(N + 1);
		if (Value % 4)
		{
			// Print the answer to standard output(screen).
			cout << "Case #" << test_case + 1 << endl;
			cout << 0 << endl;
			continue;
		}

		Value /= 4;
		
		dyn[0] = 1;

		for (int i = 1; i <= N; i++)
		{
			for (int j = Value; j >= i; j--)
			{
				dyn[j] = dyn[j] + dyn[j - i];
			}
		}

		// Print the answer to standard output(screen).
		cout << "Case #" << test_case + 1 << endl;
		cout << dyn[Value]/2 << endl;
	}

	return 0;//Your program should return 0 on normal termination.
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值