<span style="color:#330099;">/*
I - 深搜 基础
Time Limit:1000MS Memory Limit:10000KB 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 x 1 , . . . , x n . 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 x 1 , . . . , x n 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 distinct; the same sum cannot 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
By Grant Yuan
2014.7.14
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
int a[13];
int fre[13];
int ffre[100];
int t,n;
int s[100];
bool mark;
int num;
int top;
int top1;
int sum;
int first;
void sort()
{int t1;
for(int i=0;i<top1;i++)
for(int j=i;j<=top1;j++)
{
if(a[i]<a[j]){
t1=a[i],a[i]=a[j],a[j]=t1;
t1=fre[i],fre[i]=fre[j],fre[j]=t1;
}
}
} void pt()
{ int bear=0;
for(int i=0;i<=top;i++) if(ffre[i]){
for(int j=1;j<=ffre[i];j++)
{ if(bear==0)
{
cout<<s[i];
bear=1;}
else
printf("+%d",s[i]);} }
cout<<endl;
} void dps(int k)
{
if(k>top1){
if(sum==t)
{mark=1;
if(first==0)
printf("Sums of %d:\n",t);
first=1;pt();num++;}
return;
}
for(int i=fre[k];i>=0;i--)
{if(sum+a[k]*i<=t){
s[++top]=a[k];
ffre[top]=i;
sum+=a[k]*i;
dps(k+1);
top--;
sum-=a[k]*i;
} }
} int main()
{
while(1){
cin>>t>>n;
top1=-1;
top=-1;
sum=0;
first=0;
mark=0;
memset(fre,0,sizeof(fre));
memset(ffre,0,sizeof(ffre));
num=0;
if(n==0)
break;
int m;
bool flag1;
for(int i=0;i<n;i++)
{flag1=0;
cin>>m;
for(int j=0;j<=top1;j++)
{
if(m==a[j])
flag1=1,fre[j]++;
}
if(flag1==0)
{a[++top1]=m;
fre[top1]=1;}}
sort();
dps(0);
if(mark==0)
{printf("Sums of %d:\n",t);
printf("NONE\n");}
}
return 0;
}
</span>

I深搜的更多相关文章

  1. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  2. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  3. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  4. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  5. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  6. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  7. 深搜+DP剪枝 codevs 1047 邮票面值设计

    codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description ...

  8. 【wikioi】1049 棋盘染色(迭代深搜)

    http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...

  9. poj1190 生日蛋糕(深搜+剪枝)

    题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...

  10. HDU 4597 Play Game(记忆化搜索,深搜)

    题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...

随机推荐

  1. Unicode 字符集与它的编码方式

    正式内容開始之前,我们先来了解一个基本概念,编码字符集. 编码字符集:编码字符集是一个字符集,它为每个字符分配一个唯一数字.Unicode 标准的核心是一个编码字符集,字母"A"的 ...

  2. linux下安装node.js

    1.下载 wget http://nodejs.org/dist/v0.10.32/node-v0.10.32-linux-x64.tar.gz 2.解压 tar -xvf node-v0.10.32 ...

  3. python基础教程_学习笔记1:序列-1

    序列 数据结构:通过某种方式组织在一起的数据元素的集合,这些数据元素能够是数字或者字符,甚至能够是其它数据结构. python中,最主要的数据结构是序列. 序列中的每一个元素被分配一个序号--即元素的 ...

  4. 应用程序初始化正常(0xc015002)失败解决方法

    VS2005 sidebyside manifest error Microsoft.VC80.MFC Microsoft.VC80.CRT Microsoft.VC80.MFCLOC msvcr80 ...

  5. Twenty Newsgroups Classification实例任务之TrainNaiveBayesJob(一)

    接着上篇blog,继续看log里面的信息如下: + echo 'Training Naive Bayes model' Training Naive Bayes model + ./bin/mahou ...

  6. Enthought科学计算,数据分析

    Enthought Canopy: Easy Python Deployment Plus Integrated Analysis Environment for Scientific Computi ...

  7. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Server常见问题

    1.ArcGIS 10.2有无测试版程序? 10.2没有正式的测试版程序,但是我们计划发布两个早期cuts软件给国际经销商.选中的用户和合作伙伴. 2.微软Azure云的ArcGIS forServe ...

  8. Visual Prolog 的 Web 专家系统 (8)

    GENI核心 -- 推理引擎(2)流量控制 1.阐述fail."!"而回溯 与其他语言相比,,Prolog最大的特点.这是回溯机制. 回溯机制,还有的主要手段2个月,首先,通过使用 ...

  9. 30岁生日,媳妇赏的,U-BOAT手表一枚-数字尾巴

    http://bbs.dgtle.com/thread-139611-1-1.html

  10. 服务器编程入门(10)TCP回射服务器实现 - 并发

    问题聚焦:     在前面我们大概浏览了一下服务器编程需要掌握的一些知识和技术,以及架构思想.        实践,才是检验真理的唯一标准..从这节起我们将在这些技术的基础上,一步步实现以及完善一个服 ...