Usaco Section2.1 subset Sums Problem solving report "Icedream61"

Source: Internet
Author: User

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&GT;=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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.