HDU 1258 sum it up (DFS)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1258

Problem descriptiongiven a specified total T and a list of N integers, find all distinct sums using numbers from the list that add up to T. for example, if T = 4, n = 6, and the list is [,], then there are four different sums that equal 4: + + 2, and 2 + 1 + 1. (A number can be used within a sum as every times as it appears in the list, and a single number counts as a sum .) your job is to solve this problem in general.

 

Inputthe input will contain in one or more test cases, one per line. each test case contains t, the total, followed by N, the number of integers in the list, followed by N integers X1 ,..., XN. if n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, N will be an integer between 1 and 12 (random SIVE ), and X1 ,..., XN will be positive integers less than 100. all numbers will be separated by exactly one space. the numbers in each list appear in nonincreasing order, and there may be repetitions.

 

Outputfor each test case, first output a line ining 'sums', the total, and a colon. then output each sum, one per line; if there is no sums, output the line 'none '. the numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as usual times as it was repeated in the original list. the sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. in other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. within each test case, all sums must be distince; the same sum connot appear twice.

 

Sample input4 6 4 3 2 2 1 5 3 2 1 1 400 12 50 50 50 50 25 25 25 25 25 0 0

 

Sample outputsums of 4: 4 3 + 1 2 + 2 2 + 1 + 1 sums of 5: None sums of 400: 50 + 50 + 50 + 50 + 50 + 50 + 25 + 25 + 25 50 + 50 + 50 + 50 + 50 + 25 + 25 + 25 + 25 + 25 + 25 + 25 + 25
 1 # Include <iostream> 2   Using   Namespace  STD;  3   Int A [ 20 ], Mm [ 20 ], S, Mark [ 20  ], Flag;  4   Void Print (Int  N)  5   {  6       Int I, j, F = 0  ;  7       For (I = 0 ; I <n; I ++ )  8   {  9 J = 0  ; 10           While (J < Mark [I])  11   {  12               If (! F)  13 Printf ( "  % D  "  , A [I]);  14               Else  15 Printf ("  + % D  "  , A [I]);  16 F = 1  ;  17 J ++ ;  18   }  19   }  20 Putchar ( 10  ); 21   }  22   Void DFS ( Int X, Int N, Int  Sum)  23   {  24       If (Sum = S)  25   {  26 Flag =1  ;  27   Print (N );  28           Return  ;  29   }  30       Int  I;  31       For (I = x; I <n; I ++ )  32           If (Mm [I]> MARK [I] & sum + A [I] <= S)  33   {  34 Mark [I] ++ ;  35 DFS (I, n, sum + A [I]);  36 Mark [I] -- ;  37   }  38   }  39   Int Main ()  40   {  41       Int  M, I, j, AA;  42       While (~ Scanf ( "  % D  " , & S, & M), S | M)  43   {  44           Int Su = 0 ;  45 Scanf ( "  % D  " , & [ 0  ]);  46 Su + = [ 0  ];  47 Memset (mm, 0 , Sizeof  (Mm ));  48 Mm [0 ] ++ ;  49           For (J = I = 1 ; I <m; I ++ )  50   {  51 Scanf ( "  % D  " ,& Aa );  52 Su + = AA; 53               If (AA = A [J- 1  ])  54 Mm [J- 1 ] ++ ;  55               Else  56   {  57 Mm [J] ++ ;  58 A [J ++] = AA; 59   }  60   }  61 Printf ( "  Sums of % d: \ n  "  , S );  62           If (SU < S)  63   {  64 Printf ( " None \ n  "  );  65               Continue  ;  66   }  67 Memset (mark, 0 , Sizeof  (Mark ));  68 Flag = 0  ;  69 DFS (0 , J, 0  );  70           If (! Flag)  71 Printf ( "  None \ n  "  );  72   }  73       Return   0  ; 74 }

 

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.