l-sum It upTime
limit:1000ms Memory limit:10000kb 64bit IO format:%i64d &%i64u
Submit Status
Description
Given a specified total T and a list of n integers, find all distinct sums using numbers from the list. T. For example, if T = 4, n = 6, and the list was [4, 3, 2, 2, 1, 1], then there be four different sums that equal 4:4, 3 +1, 2+1+1, and. (a number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
Input
the input would contain one or more test cases, one per line. Each test case is contains T, the total, followed by N, the number of integers in the list, and followed by n integers x 1. . . , x N. If n = 0 It signals the end of the input; Otherwise, t be is a positive integer less than, n would be a integer between 1 and (inclusive), and X 1,. . . , x n would be is positive integers less than 100. All numbers is separated by exactly one space. The numbers in each list appear in nonincreasing order, and there could be repetitions.
Output
For each
test case, first output a line containing ' Sums of ', the total, and a colon. Then output the 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 is repeated in the sum as many times as it is repeated in the original list. The sums themselves must is sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must is sorted by their first number; Sums with the same first number must is sorted by their second number; Sums with the same first and numbers must is sorted by their third number; And so on. Within each test case, all sums must is distinct; The same sum cannot 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
1#include <iostream>2#include <algorithm>3 using namespacestd;4 5 intnum[ the];6 intway[ the];7 intT,n;8 intAll,all_ti;9 Ten voidDfsintNow ) One { A if(all==T) - { - intI=1; the for(; i<=n;i++) - { - if(way[i]==1) - { +cout<<Num[i]; - Break; + } A } at for(i++;i<=n;i++) - { - if(way[i]==1) - { -cout<<"+"<<Num[i]; - } in } -cout<<Endl; to +all_ti++; -way[now]=0; theall-=Num[now]; * } $ ElsePanax Notoginseng { - intlast=-1; the for(intj=now;j<=n;j++) + { A if(way[j]==0&& num[j]+all<=t&&last!=Num[j]) the { +last=Num[j]; -way[j]=1; $all+=Num[j]; $ Dfs (j); - - } the } -way[now]=0;Wuyiall-=Num[now]; the } - Wu } - About intcmpintXinty) ${returnX>y;} - - - intMain () A { + the while(cin>>t>>N) - { $ if(n==0) Break; theAll=all_ti=0; the for(intI=1; i<=n;i++) the { theCin>>Num[i]; -way[i]=0; in } theSort (num+1, num+1+n,cmp); thecout<<"Sums of"<<T<<":"<<Endl; AboutDfs1); the if(all_ti==0) cout<<"NONE"<<Endl; the the } + return 0; -}
View Code
L-sum It up