Sum It Up

Time Limit:1000MS     Memory Limit:32768KB     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 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

题目简单翻译:

给一个数n,然后给一个数m,接下来m个数,问有多少种情况使得若干个取自m个数中的数的和为n。没有则输出NONE

解题思路:

dfs,然后注意去重;

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int t[];
int Ans_Array[];
bool cmp(const int &a,const int &b)
{
return a>b;
}
int Find;
void dfs(int now,int length,int Sum)
{
if(Sum==n)
{
Find=;
for(int i=;i<length;i++)
{
if(i) printf("+");
printf("%d",Ans_Array[i]);
}
printf("\n");
return;
}
for(int i=now+;i<m;i++)
{
if(t[i]<=n-Sum&&(i==now+||t[i]!=t[i-]))//这是去重和剪枝
{
Ans_Array[length]=t[i];
dfs(i,length+,Sum+t[i]);
}
}
}
void solve()
{
Find=;
for(int i=;i<m;i++)
{
if(t[i]<=n&&(i==||t[i]!=t[i-]))//这是去重和剪枝
{
Ans_Array[]=t[i];
dfs(i,,t[i]);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
for(int i=;i<m;i++) scanf("%d",&t[i]);
sort(t,t+m,cmp);
printf("Sums of %d:\n",n);
solve();
if(!Find) puts("NONE");
}
return ;
}

POJ 1564 Sum It Up(DFS)的更多相关文章

  1. poj 1564 Sum It Up (DFS+ 去重+排序)

    http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...

  2. 【POJ - 3984】迷宫问题(dfs)

    -->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...

  3. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  4. Sum It Up---poj1564(dfs)

    题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...

  5. 【POJ - 1321】棋盘问题 (dfs)

    棋盘问题 Descriptions: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘 ...

  6. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  7. 【POJ - 1970】The Game(dfs)

    -->The Game 直接中文 Descriptions: 判断五子棋棋局是否有胜者,有的话输出胜者的棋子类型,并且输出五个棋子中最左上的棋子坐标:没有胜者输出0.棋盘是这样的,如图 Samp ...

  8. poj 1564 Sum It Up【dfs+去重】

    Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Accepted: 3475 Descrip ...

  9. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

随机推荐

  1. js与objective-c的交互-备

    在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要从现在的网页跳到另外一个网页的时候,就会去修改 window.location.href 的位置:在我们的 Ob ...

  2. 开心菜鸟学习系列-----javascript(2)

    最小全局变量 :        1)每个javascript环境有一个全局对象,当你在任意的函数外面使用this的时候可以访问到,你创建的每一个全部变量都成了这个全局对象的属性,在浏览器中,方便起见, ...

  3. android核心分析--转

    http://blog.csdn.net/column/details/androidcore.html http://simon-fu.vicp.cc/?p=999 http://www.uml.o ...

  4. hdu 3461 Code Lock

    http://acm.hdu.edu.cn/showproblem.php?pid=3461 并差集和幂取模 这道题主要是求不可操作区间. #include <cstdio> #inclu ...

  5. strace基本操作

    可以发现很多真正在系统层面发生的调用,以及很细微的返回错误信息,用于调试工作.(比如,软件出错,或是性能变慢...) strace -p 32000 -o strace.txt 基本上完整的用法是这样 ...

  6. hdu 1011 Starship Troopers_树状dp

    题目链接 题意:给你一棵树(必须从根节点出发),每个节点上都有bug和value,你有m个骑士,每个骑士能消灭20个bug,你必须消灭该节点的全部bug才能拿到该节点的value,问最多能拿到valu ...

  7. linux内核--进程空间(二)

        内核处理管理本身的内存外,还必须管理用户空间进程的内存.我们称这个内存为进程地址空间,也就是系统中每个用户空间进程所看到的内存.linux操作系统采用虚拟内存技术,因此,系统中的所有进程之间虚 ...

  8. poj 3616 Milking Time DP

    题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...

  9. 04747_Java语言程序设计(一)_第5章_图形界面设计(一)

    例5.1一个用JFrame类创建窗口的Java应用程序. import javax.swing.*; public class Example5_1 { public static void main ...

  10. js jquery 验证写法

    <?php header("Content-type: text/html; charset=utf-8"); ?> <script src="jque ...