Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1258
Sum it up
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 4012 accepted submission (s): 2066
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 Input
4 6 4 3 2 2 1 15 3 2 1 1400 12 50 50 50 50 50 50 25 25 25 25 25 250 0
Sample output
Sums of 4:43+12+22+1+1Sums of 5:NONESums of 400:50+50+50+50+50+50+25+25+25+2550+50+50+50+50+25+25+25+25+25+25
Question: first, let's give you a T, a n, followed by N numbers. Then, ask how many kinds of Addition Methods There are for N numbers so that the addition result is equal to T. Note that they cannot be repeated ~ For example, 4 = 3 + 1, so multiple 4 = 3 + 1 can only be regarded as one type, and 4 = 1 + 3 is also one type;
Train of Thought: sort from big to small ~ Deep Search for AC;
# Include <iostream> # include <string. h> # include <string> # include <stdio. h >#include <cmath> # include <cstdio> # include <algorithm> using namespace STD; int t, n, flag; int A [16], queue [16]; // simulation queue int CMP (int A, int B) {return A> B;} void DFS (INT sum, int count, int POS) {If (sum> T) return; // recursive exit (Target Detection Function) if (sum = T) {flag = 1; for (INT I = 0; I <count-1; I ++) printf ("% d +", queue [I]); printf ("% d \ n", queue [count-1]); return ;} For (INT I = Pos; I <n; I ++) // combines operations that convert one state to another with {sum + = A [I]; queue [count] = A [I]; DFS (sum, Count + 1, I + 1); sum-= A [I]; while (A [I] = A [I + 1]) I ++; // This is to avoid repetition} int main () {While (CIN> T> N) {If (t = 0 & n = 0) break; For (INT I = 0; I <N; I ++) {CIN> A [I];} Sort (A, A + N, CMP); flag = 0; cout <"sums of" <t <":" <Endl; DFS (, 0); // If (! Flag) cout <"NONE" <Endl;} return 0 ;}
HDU 1258 (Deep Search)