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. Error Handling in ASP.NET Core

    Error Handling in ASP.NET Core 前言  在程序中,经常需要处理比如 404,500 ,502等错误,如果直接返回错误的调用堆栈的具体信息,显然大部分的用户看到是一脸懵逼的 ...

  2. 深入理解 JavaScript 中的 replace 方法(转)

    replace方法是属于String对象的,可用于替换字符串. 简单介绍: StringObject.replace(searchValue,replaceValue) StringObject:字符 ...

  3. PHP初入,(特效的使用)

    <body> <input id="btn1" type="button" value="按钮" /> <in ...

  4. 转:【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17965867   方法解析 Class文件的编译过程中不包含传统编译中的连接步骤,一切方法 ...

  5. unity3D HTC VIVE开发-物体高亮功能实现

    在VR开发时,有时需要用到物体高亮的功能.这里使用Highlighting System v3.0.1.unitypackage插件实现. Highlighting System v3.0.1的介绍访 ...

  6. Beta版本冲刺计划安排

    1.介绍小组新加入的成员,Ta担任的角色 王婧:web界面以及前端和后台的交互 柯怡芳:PM以及文档 陈艺菡:修复bug以及文档 钱惠:web界面以及前端和后台的交互 林凯:测试人员 吴伟君(新成员) ...

  7. 201521123033《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. answer: 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1.List中指定元 ...

  8. 201521123052《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...

  9. 201521123014 《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 Q1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...

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

    #1. 本周学习总结(1)学习使用码云存储代码(2)掌握了常见数据类型的使用.转换(3)回顾了前面学过的基本语法(4)复习一二三章内容 #2. 书面作业**Q1.使用Eclipse关联jdk源代码,并 ...