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
 
 
 #include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int t,a[],ans[],n,nu;
void dfs(int s,int as,int sum)
{
int i;
if(sum==t)
{
nu++;
for(i=;i<as;i++)
if(i!=as-)
printf("%d+",ans[i]);
else printf("%d\n",ans[i]);
return ;
}
if(s>=n)return ;
for(i=s;i<n;i++)
{
ans[as]=a[i];
dfs(i+,as+,sum+a[i]);
while(i+<n&&a[i]==a[i+])i++;
}
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j;
while(scanf("%d%d",&t,&n),t||n)
{
nu=;
for(i=;i<n;i++)
scanf("%d",&a[i]);
cout<<"Sums of "<<t<<":"<<endl;
dfs(,,);
if(nu==)
cout<<"NONE"<<endl;
}
}
 #include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int t,n,a[][],ans[][],an,ok;
void dfs(int s,int anss,int sum)
{
//cout<<s<<" "<<anss<<" "<<sum<<endl;
int i,j;
if(sum>t)return;
if(sum==t)
{
for(i=; i<anss; i++)
{
for(j=; j<ans[i][]; j++)
if(i==anss-&&j==ans[i][]-)
printf("%d\n",ans[i][]);
else
printf("%d+",ans[i][]);
}
ok++;
return ;
}
if(s>=an)return;
for(j=a[s][]; j>=; j--)
{
ans[anss][]=a[s][];
ans[anss][]=j;
if(j)
dfs(s+,anss+,sum+a[s][]*j);
else dfs(s+,anss,sum+a[s][]*j);
}
return ;
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j,x;
while(scanf("%d%d",&t,&n),t||n)
{
ok=;
an=;
for(i=; i<n; i++)
{
scanf("%d",&x);
if(an==)
{
a[an][]=x,a[an++][]=;
}
else
{
if(x==a[an-][])
a[an-][]++;
else a[an][]=x,a[an++][]=;
}
}
cout<<"Sums of "<<t<<":"<<endl;
dfs(,,);
if(!ok)
cout<<"NONE"<<endl;
}
}
 

Sum It Up 广搜的更多相关文章

  1. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  2. HUST 1605 Gene recombination(广搜,位运算)

    题目描述 As a gene engineer of a gene engineering project, Enigma encountered a puzzle about gene recomb ...

  3. TZOJ 5279 马拉松比赛(广搜)

    描述 有一块矩形的海域,其中有陆地也有海洋,这块海域是CSUFT_ACM集训队的训练基地,这一天,昌神说要集训队的队员不能总是训练,于是昌神提出了中南林ACM集训队第一场环陆马拉松比赛,顾名思义就是围 ...

  4. PAT L3-004 肿瘤诊断(三维广搜)

    在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每张切片的尺寸(即每张切片 ...

  5. poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重

    挺不错的题目,很锻炼代码能力和调试能力~ 题意:初始格子状态固定,给你移动后格子的状态,问最少需要多少步能到达,如果步数大于30,输出-1. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...

  6. 69.广搜练习:  最少转弯问题(TURN)

    [问题描述] 给出一张地图,这张地图被分为n×m(n,m<=100)个方块,任何一个方块不是平地就是高山.平地可以通过,高山则不能.现在你处在地图的(x1,y1)这块平地,问:你至少需要拐几个弯 ...

  7. zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)

    题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...

  8. Catch That Cow(广搜)

    个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...

  9. Zoj2421 广搜

    <span style="color:#330099;">/* M - 广搜 加强 Time Limit:2000MS Memory Limit:65536KB 64b ...

随机推荐

  1. Linux Command Line learning

    https://www.codecademy.com/en/courses/learn-the-command-line Background The command line is a text i ...

  2. 常用bash命令

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...

  3. Python 学习系列----第一章:基础知识

    1.1 常量-----不能改变它的值 1.2 数 在Python 中数可以分为整数.浮点数和复数. PS:在Python中不用区分'long int'类型.默认的整数类型可以任意长.(译者注:长度应该 ...

  4. poj 1948二维01背包

    题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积. 思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600  ...

  5. git和github的重要性

    Git是一款免费.开源的分布式版本控制系统,github是全球最大的同性交友平台啊呸,说错了github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就 ...

  6. 避免subList/subString陷阱

    避免subList/subString陷阱 java.util.List 接口提供了一个实例方法 List<E> subList(int fromIndex, int toIndex), ...

  7. MacOS下免密码ssh登陆

       由于配置过程中需要频繁的进行ssh连接到开发服务器执行命令以及通过scp命令向服务器拷贝文件等依赖ssh连接的操作.所以,配置本地环境跟服务器之间的ssh免密码连接可以有效的提升工作效率.    ...

  8. spring+mybatis的简单配置示例

    简单代码结构: //Book.java package com.hts.entity; public class Book { private String id; private String bo ...

  9. 201521123095 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 Q1 List中指定元素的删除(题目4-1) 1.1 实验总结 对于删除函数 ...

  10. 201521044091 java 第一周总结

    1.本周学习总结 (1)第一次开始接触java语言,有些用法还是和c和c++有点差异,需要不断去学习 (2)java其实不仅仅一种高级语言,它包括的还有它的整套体系. 2. 书面作业 1.为什么jav ...