I深搜
<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深搜的更多相关文章
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 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 ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 深搜+DP剪枝 codevs 1047 邮票面值设计
codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- 【wikioi】1049 棋盘染色(迭代深搜)
http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...
- poj1190 生日蛋糕(深搜+剪枝)
题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...
- HDU 4597 Play Game(记忆化搜索,深搜)
题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...
随机推荐
- jquery实现鼠标焦点十字效果
系统开发时很多地方需要有焦点效果,例如:鼠标点击聚焦,地图定位,在图片上突出显示,焦点定位页面元素. 本小功能通过jquery和graphics二次开发,实现通过鼠标点击页面任何区域,聚焦当前点击位置 ...
- [置顶] Ftp客户端概要设计
Ftp客户端概要设计 1.概述 ftp是基于TCP的文件传输协议,主要是用于控制远程文件,如下载.上传.续传.重命名.删除等.其命令是基于可见字符,易于理解的方式交互的.客户端与服务器端的交互遵循一应 ...
- Android编程心得-Handler与子线程的交互初步
在编写项目的时候,本人发现一个关于线程与Handler很容易犯的错误. 我有两个Activity,一个Activity在后台创建了一个线程并且启动,这个线程对象对应的实体实在另外一个Activity的 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- KVM 实现机制
1.1. KVM简介 KVM是一个基于Linux内核的虚拟机,它属于完全虚拟化范畴,从Linux-2.6.20开始被包含在Linux内核中.KVM基于x86硬件虚拟化技术,它的运行要求Intel ...
- 带着项目学PHP第九讲 - 如何给ecshop的wap版本首页和商品页添加商品图片
ecshop的wap版本自身不带图片, 所以看起来光秃秃的,非常不讨人喜欢, 网络上关于wap的模板就不像pc版那么多,容易找到, 而且能找到的都是要花钱买的, 虽然这个小小的改动不能替代找个合适的模 ...
- 单页应用Scrat实践
单页应用Scrat实践 1.开始 随着前端工程化深入研究,前端工程师现在碉堡了,甚至搞了个自己的前端网站http://div.io/需要邀请码才能注册,不过里面的技术确实牛.距离顶级的前端架构,目前博 ...
- CF 316C2(Tidying Up-二分图最大边权)
C2. Tidying Up time limit per test 4 seconds memory limit per test 256 megabytes input standard inpu ...
- 优酷m3u8视频源地址获取失败
昨天和今天上午,优酷站点视频全然没有办法播放,可是我是获取的优酷视频的视频原地址,所以app还是能够正常播放而且有下载功能.今天下午開始,优酷视频网页能够訪问了,可是视频原地址却不在了.我全部的app ...
- Cocos2dx3.4 VS2013无法打开包括文件extensions/ExtensionExport.h解决的方法
本来打算看白鹭引擎的.可是又被叫回来研究新引擎呢,不搞不知道,一搞发现cocos2dx新版本号3.4又有了一些变化. 我查了网上的资源,都没有解决的方法.我如今应该是第一个出这个问题的解决的方法哦.. ...