• hdu 1258 Sum It Up ( dfs )


    http://acm.hdu.edu.cn/showproblem.php?pid=1258

    Problem Description
    Given 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 [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(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 will contain 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(inclusive), 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.
     
    Output
    For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are 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 many 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 1 5 3 2 1 1 400 12 50 50 50 50 50 50 25 25 25 25 25 25 0 0
     
    Sample Output
    Sums 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+25 50+50+50+50+50+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%d",&s,&m),s|m)
    43     {
    44         int su=0;
    45         scanf("%d",&a[0]);
    46         su+=a[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 }
  • 相关阅读:
    [洛谷P2463][SDOI2008]Sandy的卡片
    后缀数组
    周记【距gdoi:110天】
    hdu3068最长回文(Manacher算法)
    二分图的一些题目合集
    周记【距gdoi:117天】
    二分图、网络流模版总结
    2014end
    周记【距gdoi:126天】
    2-sat
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2646197.html
Copyright © 2020-2023  润新知