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. JDK+Tomcat搭建JSP运行环境--JSP基础

    一.搭建JSP运行环境之前需要了解的基本知识 配置JSP运行环境之前,我们需要了解JSP的运行机制.只有了解JSP运行机制后,我们才能知道为什么要搭建JSP运行环境?如何去搭建JSP运行环境?为什么要 ...

  2. 多线程(七)JDK原生线程池

    如同数据库连接一样,线程的创建.切换和销毁同样会耗费大量的系统资源.为了复用创建好的线程,减少频繁创建线程的次数,提高线程利用率可以引用线程池技术.使用线程池的优势有如下几点:        1.保持 ...

  3. Cognos报表调度与作业管理

    本文针对Cognos的报表调度和作业管理做案例分析.为了测试报表定时调度功能,本文将报表定时输出到指定的归档目录. 1. 测试环境 Cognos  V11.0 2. 设置档案文件根目录 Cognos报 ...

  4. C++三种野指针及应对/内存泄露

     野指针,也就是指向不可用内存区域的指针.如果对野指针进行操作,将会使程序发生不可预知的错误,甚至可能直接引起崩溃.         野指针不是NULL指针,是指向"垃圾"内存的指 ...

  5. diff.js 列表对比算法 源码分析

    diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...

  6. 第3阶段——内核启动分析之创建si工程和分析stext启动内核函数(4)

    目标: (1)创建Source Insight 工程,方便后面分析如何启动内核的 (2)分析uboot传递参数,链接脚本如何进入stext的  (3) 分析stext函数如何启动内核:  (3.1) ...

  7. 软工+C(2017第2期) 分数和checklist

    // 上一篇:题目设计.点评和评分 // 下一篇:超链接 教学里,建立清晰明确的评分规则并且一开始就公布,对于教师.助教.学生都是重要的. 公布时机 在课程开始的时候,就需要确定并公布评分机制,随着课 ...

  8. JAVA基础第四组(5道题)

    16.[程序16]                   题目:输出9*9口诀.                  1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列. package com. ...

  9. 201521123118《java程序与设计》第4周作业总结

    1.本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点 1.2 使用常规方法总结其他上课内容. 为了不必要写重复的代码,可以运用继承,用关键字extends来定义一个类,被继承的类叫做父类,继 ...

  10. 201521123075 《Java程序设计》第10周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? fin ...