【算法】【動態規劃】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.
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章