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. 帮小黎解决问题C++巩固获得数字每个位置上的数

    现在有一个数字 a= 12345; 想要取得这个数字上的没一个数字 使用 除法 +模除 的方法可以获得 原理:除(/)得到的是商     模除(%)的到的是余数 采用这种方式,先将要求的数的某一位   ...

  2. “假如花千骨在杭州拍摄” 主题Cosplay

    “假如花千骨在杭州拍摄” 主题Cosplay 今天,2015年7月23日,本周三:此刻,现场正在中国杭州西湖举办“花千骨cosplay”大型分享活动,现场有超凡而孤高,冰凉而淡漠 ,温润如玉又云淡风清 ...

  3. BZOJ 1877 晨跑

    http://www.lydsy.com/JudgeOnline/problem.php?id=1877 思路:拆点费用流,答案就是最大流量和最小费用. #include<algorithm&g ...

  4. JS--回到顶部代码

    原文地址:http://www.cnblogs.com/liguiqiang1986/articles/3132023.html JS--回到顶部代码 <!DOCTYPE html PUBLIC ...

  5. 初探JS正则表达式

    1.概述     正则表达式是一个描述字符模式的对象.Javascript的正则表达式语法的是Perl5的正则表达式的子集.JS正则表达式有两种使用方式,文本模式和RegExp对象模式,实例如下: v ...

  6. 北京Uber优步司机奖励政策(12月3日)

    用户组:人民优步(适用于12月3日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://w ...

  7. LinQ to SQL 增,删,改 代码演示

    NorthwindDBDataContext dc = new NorthwindDBDataContext(); protected void Page_Load(object sender, Ev ...

  8. qt绘制设备

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  9. Monkey Tradition(中国剩余定理)

    Monkey Tradition Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submi ...

  10. 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation

    最近在整一个很简单的通讯录相关的项目,通讯录当然就少不了按首字母或者汉字拼音首字母分组排序索引.因为按照我一贯的的做法,都是想要做成更通用的.支持本地化的,所以这就纠结了,世界各地的语言啊我去,我顶多 ...