Subset Problem Solving report
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
Divide the 1~n into two groups, so that they are equal, how many of these groupings are there?
But the order can be reversed, such as {3}, {2,1}, and {2,1}, {3} count as one.
"Data Range"
1<=n<=39
"Input Sample"
7
"Output Example"
4
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
01 knapsack problem.
Each number as an item, then there are N items, numbered i=1~n, respectively, corresponding to the number of 1~n; the volume of the goods I ci=i.
Backpack capacity V=nx (n+1) ÷4; obviously only n%4==1 or n%4==2 when the answer is 0, otherwise the answer is to put the backpack full of the number of programs d[v] divided by 2 results.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
This question has been seen, but the first time this is done is the wrong analysis. Don't think too thin, think the code is very short to write to try. However, I think, if there is such a situation, if it is a test, then I will analyze it, but if it is a practical problem, I will try, after all, it takes only a little time to test the right and wrong, how to think is better than the analysis of the cost.
I think again, did not come up with a solution, online simple search, see the algorithm is 01 backpack DP. So the "backpack Nine Talk" to review again, feel more than the first time to see more, thank Tri Tian Wing Senior!
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:subset4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One intN; A Long Longd[390+1]; - - intMain () the { -Ifstreaminch("subset.in"); -Ofstream out("Subset.out"); - + inch>>N; - if(N%4==1|| N%4==2) out<<0<<Endl; + Else A { at ints=n* (n+1) >>2;//result is D[S,N]/2 -d[0]=1; - for(intI=1; i<=n;++i) - for(intV=S;V>=I;--V) d[v]=d[v]+d[v-i]; - out<<d[s]/2<<Endl; - } in - inch. Close (); to out. Close (); + return 0; -}
Usaco Section2.1 subset Sums Problem solving report "Icedream61"