L - Sum It Up

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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 x 1 , . . . , x n . 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 x 1 , . . . , x n 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 distinct; the same sum cannot 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

//一般的bfs,关键的在于同一个位置不能放同样的数。

 #include <iostream>
#include <algorithm>
using namespace std; int num[];
int way[];
int T,N;
int all,all_ti; void dfs(int now)
{
if (all==T)
{
int i=;
for (;i<=N;i++)
{
if (way[i]==)
{
cout<<num[i];
break;
}
}
for (i++;i<=N;i++)
{
if (way[i]==)
{
cout<<"+"<<num[i];
}
}
cout<<endl; all_ti++;
way[now]=;
all-=num[now];
}
else
{
int last=-;
for (int j=now;j<=N;j++)
{
if (way[j]== && num[j]+all<=T&&last!=num[j])
{
last=num[j];
way[j]=;
all+=num[j];
dfs(j); }
}
way[now]=;
all-=num[now];
} } int cmp(int x,int y)
{return x>y;} int main()
{ while (cin>>T>>N)
{
if (N==) break;
all=all_ti=;
for (int i=;i<=N;i++)
{
cin>>num[i];
way[i]=;
}
sort(num+,num++N,cmp);
cout<<"Sums of "<<T<<":"<<endl;
dfs();
if (all_ti==) cout<<"NONE"<<endl; }
return ;
}


L - Sum It Up(DFS)的更多相关文章

  1. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  2. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  3. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  4. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  5. LeetCode Combination Sum III (DFS)

    题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...

  6. (step4.3.4)hdu 1258(Sum It Up——DFS)

    题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...

  7. hdu1258 Sum It Up (DFS)

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  8. nyoj 927 The partial sum problem(dfs)

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...

  9. HDU 1258 Sum It Up(DFS)

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...

随机推荐

  1. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC3

    在Motion上添加一个NC Task   在Axis上右击添加一个轴,类型为Continuous Axis   在PLC上右击添加新项,然后添加一个PLC项目   在引用中添加TC2_MC2的库引用 ...

  2. 使用 Apple Configurator 2 获取ipa文件

    如今很多人想要获取到App Store上的包却苦于无奈,先在要把App Store上的包载下来获取ipa,最直接的就是从手机设备上导出了,但是手机必须要9.0以下才可以导出,鄙人手中正好有公司的测试机 ...

  3. unity Chan!下载

    http://unity-chan.com/download/datadownload.html

  4. 监控应用服务器使用JMX监控Tomcat (推荐)

    前言:做了一个监控应用服务器的项目(支持Tocmat.WebSphere.WebLogic各版本), 过程也算是磕磕绊绊,由于网上缺少相关资料,或者深陷于知识的海洋难以寻觅到有效的资料,因而走过不少弯 ...

  5. vs2015+opencv-3.2.0-vc14配置

    用的VS2015免费的community社区版,功能足矣. 很早就有配置opencv249,原本觉得低版本的稳定,一直没有配成功过,测试总是报错 出现error LINK:无法打开文件“opencv_ ...

  6. json2.js

    /* http://www.JSON.org/json2.js 2010-03-20 Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE AT Y ...

  7. node.js 学习01

    PHP开发技术栈(LAMP) Linux  Apache  MySql  PHPnode.js 全栈开发技术栈(MEAN): MongoDB  Express  Angular  Node.js 现阶 ...

  8. http Referrer-Policy

    Referrer-Policy: no-referrer Referrer-Policy: no-referrer-when-downgrade Referrer-Policy: origin Ref ...

  9. Atitit.播放系统规划新版本 v4 q18 and 最近版本回顾

    Atitit.播放系统规划新版本 v4  q18  and 最近版本回顾 1 版本12 (ing)4 1.1 无映射nas系统..4 1.2 图片简介搜刮其4 1.3 12.8. 电影图片增加png, ...

  10. flink-connector-kafka consumer checkpoint源码分析

    转发请注明原创地址:http://www.cnblogs.com/dongxiao-yang/p/7700600.html <flink-connector-kafka consumer的top ...