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. 火车票抢票API 根据乘客的车次与座席要求快速订票出票

    火车票抢票API 根据乘客的车次与座席要求快速订票出票:https://www.juhe.cn/docs/api/id/257 1.站站查询 接口地址:http://v.juhe.cn/grabTic ...

  2. GDB基本命令(整合)(转)

    directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...

  3. iOS编程 手动忽略clang编译器警告

    在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能运行之类的警告. 有代码洁癖的孩子们非常想消除他们, 今天就让我们来一次Fuck 警告.! 首先学会主要的语句 #pra ...

  4. JAVA自带监控工具的介绍

    转:http://www.alidw.com/?p=326 相信部分同学可能还是不太了解或者很少使用,这些监控工具是jdk5.0以上才会有的,有部分是liunx特有的. 了解这些工具再做压力测试和调优 ...

  5. pydensecrf安装报错1、UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 29: invalid start byte2、 LINK : fatal error LNK1158: 无法运行“rc.exe” error: command 'D:\\software\\vs2015\\VC\\BIN

    pydensecrf安装报错 1.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 29: invalid st ...

  6. Eclipse3.4以上使用dropins的插件安装方式

    Eclipse3.4以上版本支持使用dropins的插件安装方式,使用方便,共有四种使用方法: 1. 最简单的,直接将jar包放到dropins目录下eclipse/    dropins/ 2. 传 ...

  7. Google Chrome 浏览器的检索语言设置

    解决为何从一开始安装Google Chrome 浏览器的时候,使用Google 搜索,结果都是日文的问题. 藏的比较隐蔽,没法在“设置”那里修改. 1.问题:搜索内容均是日文: 2.注意到右边有一个“ ...

  8. [原创]如何让freeswitch转发客户端自定义的INFO消息

    如何让freeswitch转发客户端自定义的INFO消息 英文概述: this article is about how to configure freeswitch to forward self ...

  9. Knockout JS 演示样例

    五个小样例,来自Knockout JS官方站点. //tutorial 1 //following codes uses to demonstrate observable values and ta ...

  10. codeforces #364a Cards

    cf的a题没什么好说到,100的量级,每个人给2张牌,使每个人手中的牌点数相等.保证有一种分配方案. 对每个人,先计算出手中的牌的点数,然后循环两遍拿牌就可以.   A. Cards time lim ...