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. 深入理解Azure自动扩展集VMSS(1)

    前文中已经详细介绍了如何配置和部署Azure的虚拟机扩展集VMSS进行自动扩展,但在实际使用过程当中,用户会出现更进一步使用的一些问题,VMSS基本扩展原理及怎么简单调试?如何进行手动扩展?怎么使用自 ...

  2. C语言中的字符串截取函数

    /*======================================================== 子数整数 源程序名 num.??? (pas,c,cpp) 可执行文件名 num. ...

  3. Android Development Tools 发生checkAndLoadTargetData错误

    之前使用时没有出现任何问题的,我把D:\IDE\ADT\adt-bundle-windows-x86_64-20140321\eclipse目录下面的 eclipse.exe重名名为adt.exe并设 ...

  4. The Most Wanted Letter

    The Most Wanted Letter You are given a text, which contains different english letters and punctuatio ...

  5. 极简AWR报告收集指导

    1.以oracle用户登录oracle数据库,执行如下命令登录数据库: sqlplus / as sysdba 2.运行如下命令: @?/rdbms/admin/awrrpt.sql 3.出现如下信息 ...

  6. 如何判断是REQUEST请求是来自移动终端还是来自PC端

    public bool IsMoblie()        {            string agent = (Request.UserAgent + "").ToLower ...

  7. Openstack no valid hot

    错误: 创建实例 "ce" 失败: 请稍后再试 [错误: No valid host was found. ].

  8. virtual hard disk

  9. LVM(1)

    DM: DM: Device Mapper    逻辑设备        RAID, LVM2        DM: LVM2    快照    多路径

  10. VS快捷编码方式

    概念: 代码段是将预先定义好的可重用代码块快速插入到代码文件中,代码段提高了开发效率,增强了代码的可重用性:既节约了时间,又实现了不同开发人员间代码的共享.同时也可保证同一项目中代码风格的统一.   ...