Aizu 1335 eequal Sum sets

Source: Internet
Author: User

Let us consider sets of positive integers less than or equal to N. Note, all elements of a set is different. Also Note that the order of elements doesnt matter, which is, both {3, 5, 9} and {5, 9, 3} mean the same set.

       Specifying the number of SET elements and their sum to be k and S, respectively, sets Sati Sfying the conditions is limited. When n = 9, k = 3 and S = All, {6, 8, 9} are the only such set. There may is more than one such set, in general, however. When n = 9, k = 3 and S = $, both {5, 8, 9} and {6, 7, 9} is possible.       you has to W Rite A program This calculates the number of the sets that satisfy the given conditions. Inputthe input consists of multiple datasets. The number of datasets does not exceed 100.   each of the datasets have three integers n, K and S in one line, SE Parated by a space. Assume 1≤n≤20, 1≤k≤10 and 1≤s≤155.the end of the input is indicated by a line containing three ZEROS.O Utputthe output for each dataset should is a line containing a single integer This gives the number of the sets that Satis FY the conditions. No other characters should appear in the output.   Can assume that the number of sets does not exceed 231−1.sample Input9 3 239 3 2210 3 2816 10 10720 8 10220 10 10520 4 1553 2 0 0Sample Output1202015425448100 Program Analysis: This topic, a look at the idea, is the enumeration method to do, but there are so many cases, N! Species, how all times out, but we also have to notice a problem of the logo, is the state of compression, for this kind of topic, n is generally 20, and this problem is 20, and fully conform to the state compression, although the problem has DP solution, but the state compression is enough to solve. Program code:
#include <iostream>using namespacestd;inta[3];inta[ A];intans=0;voidDfsintNintcur) {    if(cur==a[1]){        intsum=0;  for(intI=0; i<a[1];i++) {sum+=A[i]; }        if(sum==a[2]) ans++; }    ints=1; if(cur!=0) s=a[cur-1]+1;  for(inti=s;i<=n;i++) {A[cur]=i; DFS (N,cur+1); }}intMain () { while(cin>>a[0]>>a[1]>>a[2]&&a[0]+a[1]+a[2]) {ans=0; DFS (a[0],0); cout<<ans<<Endl; }return 0;}

Your friend Chen Chunhui inserted a background music for this message-download play player loaded ... Sending ... This message was sent successfully. One more.

Aizu 1335 eequal Sum sets

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.