HDU 1258 Sum It up Deep Search

Source: Internet
Author: User


crawling in process ... crawling failed Time limit:MS Memory Limit:32768KB 64bit IO Format:%I 64D &%i64u SubmitStatus Practice HDU 1258

Description

Given a specified total T and a list of n integers, find all distinct sums using numbers from the list that add-to T. F or example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there be four different sums that equal 4:4,3+1,2+2, 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, and 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 be is a positive integer less than, n would be an integer between 1 and (inclusive), and X1,..., xn wi ll be 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 distince; The same sum connot appear twice.

Sample Input

Sample Output

Test instructions: is given a number (sum), and then given the number of n, allowing you to satisfy the sum of all the different combinations and output these combinations of ideas: deep search, starting from the No. 0 deep search, when the No. 0 of all the combination to meet the next with the current number of different search, AC code: #include < Iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace Std;
int t;
int n;
int sum;
int tag,k;
int a[13];//the given number
int b[13];//Record the number of conditions that satisfy the condition
void Dfs (int x) {
int i;
if (sum>t) {
return;
}
if (sum==t) {//output
for (int j=0;j<k-1;j++) {
printf ("%d+", B[j]);
}
printf ("%d\n", b[k-1]);
tag=1;
Return
}
for (i=x;i<n;i++) {
Sum+=a[i];
B[k++]=a[i];
DFS (i+1);//Search Next
Sum-=a[i];
while (I+1<n&&a[i]==a[i+1]) {i++;} Avoid searching for the same number when backtracking back
k--;
}
}
int main () {
while (~SCANF ("%d", &t)) {
scanf ("%d", &n);
if (n==0) {break;}
for (int i=0;i<n;i++) {
scanf ("%d", &a[i]);
}
sum=0;
tag=0;
k=0;
printf ("Sums of%d:\n", t);
DFS (0);
if (tag==0) {
printf ("none\n");
}
}
return 0;
Note: Where to avoid the same number of search, the idea of deep search is the main feeling is backtracking, back to the ground how to operate; this kind of question looks simple, does not make easy,,,, Dish,,, continue to work hard .....

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1258 Sum It up Deep Search

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.