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部署 · GIT】在linux系统安装git和配置实现SSH

    领导给了一个不开放ftp的测试库,让我部署项目.拿到一个全新的环境,真是个练手的好机会. 该操作系统为:CentOs release 6.5(Final) 由于不开放ftp,所以上传下载代码是非常麻烦 ...

  2. 【搬运工】——初识Lua(转)

    使用 Lua 编写可嵌入式脚本 Lua 提供了高级抽象,却又没失去与硬件的关联. 虽然编译性编程语言和脚本语言各自具有自己独特的优点,但是如果我们使用这两种类型的语言来编写大型的应用程序会是什么样子呢 ...

  3. 用JS控制CSS基本样式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp29 用JS控制CSS基本样式的方法 CSS code .class1 { ...

  4. 机器学习实战之 第10章 K-Means(K-均值)聚类算法

    第 10 章 K-Means(K-均值)聚类算法 K-Means 算法 聚类是一种无监督的学习, 它将相似的对象归到一个簇中, 将不相似对象归到不同簇中.相似这一概念取决于所选择的相似度计算方法.K- ...

  5. Unity3d&C#分布式游戏服务器ET框架介绍-组件式设计

    前几天写了<开源分享 Unity3d客户端与C#分布式服务端游戏框架>,受到很多人关注,QQ群几天就加了80多个人.开源这个框架的主要目的也是分享自己设计ET的一些想法,所以我准备写一系列 ...

  6. js实现换肤效果

    一,js换肤的基本原理 基本原理很简单,就是使用 JS 切换对应的 CSS 样式表文件.例如导航网站 Hao123 的右上方就有网页换肤功能.除了切换 CSS 样式表文件之外,通常的网页换肤还需要通过 ...

  7. Windows10下通过anaconda安装tensorflow

    博主经历了很多的坎坷磨难才找到一个比较好的在win10下安装TensorFlow的方法: 首先需要说明的是如果你想通过Anaconda来安装tensorflow的话,首先要确认你的python的版本是 ...

  8. 最近一直在做java爬虫,有些感悟心得,分享给大家;

    首先,看完这篇文章,不能保证你成为大神,但是却可以让你懂得什么是爬虫,如何使用爬虫,如何利用http协议,侵入别人的系统,当然只是一些简单的教程,拿到一些简单的数据: 先上代码,在一步一步讲解: 这是 ...

  9. [转载]浏览器事件window.onload、onfocus、onblur、ons

    原文地址:浏览器事件window.onload.onfocus.onblur.onscroll和resize作者:lilyxiao <html> <head> <titl ...

  10. About Cheating and Plagiarism

    我先描述一下此次事件的具体经过.昨天3月15号的晚上十点,是第四次作业的deadline.在15号之前,只有五位同学提交了作业,而在临近deadline的这几个小时内密密麻麻地提交了二十多份作业.和第 ...