poj 1564 Sum It Up

题目连接

http://poj.org/problem?id=1564  

Sum It Up

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

dfs。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
bool f;
int n, tot, tar, A[N], B[N];
void dfs(int cur, int ret, int k) {
	if (ret == tar) {
		f = true;
		for (int i = 0; i < k; i++) {
			if (!i) printf("%d", B[i]);
			else printf("+%d", B[i]);
		}
		putchar('\n');
		return;
	}
	for (int i = cur; i < n; i++) {
		if (i == cur || A[i] != A[i - 1]) { // 判重
			B[k] = A[i];
			dfs(i + 1, ret + A[i], k + 1);
		}
	}
}
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	while (~scanf("%d %d", &tar, &n), n) {
		f = false;
		rep(i, n) scanf("%d", &A[i]);
		printf("Sums of %d:\n", tar);
		dfs(0, 0, 0);
		if (!f) { puts("NONE"); continue; }
	}
	return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4909439.html

优快云余额充值前必读:这5个隐藏条款,90%的开发者都忽略了(附iOS内购避坑指南) 本文揭示了优快云余额充值中90%开发者忽略的5个隐藏条款,包括不可退款规则、iOS内购限制、账号安全责任等关键内容,特别提供了iOS内购避坑指南,帮助开发者避免资金损失和账号风险。 阅读详情

相关推荐

从零开始:如何用PSINS工具箱构建你的第一个IMU误差模型

本文详细介绍了如何使用严恭敏教授开发的PSINS工具箱构建IMU误差模型,涵盖从基础参数设置到高级误差建模的全过程。通过imuerrset函数配置陀螺零偏、随机游走等核心参数,并结合实战案例展示消费级MEMS器件的误差建模方法,帮助用户提升惯性导航系统的精度与可靠性。

weixin_29284885的博客 322

搜索剪枝

搜索剪枝(dfs优化) 1.什么是剪枝 2.几种常见的剪枝 3.剪枝实例

skycol的博客 3856

Nacos 2.3.2与PostgreSQL的Docker化部署实战

本文详细介绍了Nacos 2.3.2与PostgreSQL的Docker化部署实战,包括环境准备、数据库配置、镜像获取与导入、容器部署及常见问题排查。通过Docker容器化技术,结合PostgreSQL的稳定性和扩展性,为微服务架构提供高效的服务发现和配置管理解决方案。

k5l6m的博客 281

数据结构与算法之限界搜索剪枝

数据结构与算法之限界搜索剪枝

weixin_47225948的博客 2072

用HFSS仿真平面线圈的电感量

平面线圈是指在平面上绕制而成的线圈,如PCB上的电感线圈、无线供电使用的金属丝绕制而成的线圈等。根据线圈的不同形状可将平面线圈分为方形线圈,六角形线圈、八角形线圈、螺旋原型线圈等。

johnboat的专栏 1万+

ESP32-S3射频测试与全球认证全栈实践指南

功率谱密度(PSD)和发射功率等级是无线通信设备射频合规的核心基础概念,其物理层原理直接决定自适应测试、阻塞容限及多国认证的通过路径。PSD ≥10 dBm/MHz触发强制性自适应测试,需验证LBT机制与跳频鲁棒性;而16级可编程发射功率等级(-24 dBm至+20 dBm)则为功耗、覆盖与法规平衡提供工程调控维度。该能力广泛应用于低功耗蓝牙IoT终端、智能家居网关及工业无线传感器等场景,尤其在ESP32-S3平台中,需结合eFuse校准、Sigma工具链与QuickTrack认证策略实现从实验室调试到量产

weixin_34945060的博客 285

搜索算法——剪枝

剪枝算法 定义: 在进行搜索算法的过程中,将已知无意义的情况排除的行为叫做剪枝 操作方法: 剪枝比较灵活,需要具体问题具体分析 有些是比较明显的,比如题目中的限制条件,有的则是需要分析得到的 剪枝可以分为 (1)可行性剪枝 (2)最优性剪枝 我们从例题分析: 洛谷P1535 最直接的思路: 直接开始深搜,但要注意以下几点: 限制搜索深度时间为t 可以重复经过一个地方 必须在规定的时间到达终点 但是这样会TLE,所以我们需要剪枝 容易发现,从点(x1,y1)到(x2,y2)最少需要abs(x1-x2)+ab

qq_51957239的博客 2099

IEEE Transaction期刊模板使用注意事项

本文将总结使用IEEE Transactions系列期刊的投稿模板时的一些注意事项,包括模板本身的使用规范、参考文献的正确格式等其他内容。

weixin_39529207的博客 2万+

搜索(剪枝)

剪枝,就是减少搜索树的规模、尽早排除搜索树中不必要分支的一种手段。

2401_84027054的博客 1259

Jacobi正交多项式在数值分析中的应用与实现

本文深入探讨了Jacobi正交多项式在数值分析中的核心应用与实现。作为最通用的正交多项式族,Jacobi多项式通过参数α和β的调节,可退化为切比雪夫、勒让德等经典多项式,为函数逼近、奇异积分和微分方程求解提供了统一框架。文章重点解析了其在加权高斯积分(高斯-雅可比积分)和谱方法求解微分方程中的关键作用,并提供了基于递归关系和SciPy的稳定高效实现方案,帮助读者掌握这一强大的数值计算工具。

weixin_29306011的博客 244

交换机初始化配置详解

MGMT或称MGT(Management Port,管理端口)是一个可以连接计算机的三层主机端口,如下图所示。只需使用一条双绞线跳线,就可以将计算机的以太网端口与交换机的MGMT端口连接在一起,实现对交换机的配置与管理。也就是说,可以使用MGMT端口代替Console端口实现对交换机或交换机堆叠的网络管理。MGMT端口既可用于交换机的初始化,也可以在交换机初始化完成后,以Telnet、Web浏览器或Cisco CNA方式进行管理。当将计算机连接到独立的交换机时,连接方式如下图所示。

悦分享 1992

算法——Alpha-Beta剪枝搜索

(1)将走棋方定为MAX方,因为它选择着法时总是对其子节点的评估值取极大值,即选择对自己最为有利的着法;(2)将应对方定为MIN方,因为它走棋时需要对其子节点的评估值取极小值,即选择对走棋方最为不利的、最有钳制作用的着法。(3)在对博弈树(博弈树是指由于动态博弈参与者的行动有先后次序,因此可以依次将参与者的行动展开成一个树状图形。)采取深度优先的搜索策略时,从左路分枝的叶节点倒推得到某一层MAX节点的值,可表示到此为止得以“落实”的着法最佳值,记为α。(4)显然此值可作为MAX方着法指标的下界。

oopxiajun博客专栏 5365

【红外DDE算法】一种应用于高动态范围红外可视化的算法

【红外DDE算法】一种应用于高动态范围红外可视化的算法 - 这个算法背后的动机 - 展示这个算法的流程 - 然后讨论各个可调参数的意义。

sunwindl91181的博客 1682

DFS入门:全排列算法及POJ 1564 Sum it up详解

全排列算法是DFS(深度优先搜索)的一个简单入门应用。 假设我们现在给过一个数n, 要求我们输出从1到 n的n个数的所有排列方式。 详细要求可参考codevs中 1294全排列问题。首先给出程序的主体及各个变量的作用:#include<stdio.h> #include<stdlib.h> int main(void){ int n; scanf("%d", &n); i

日复一日,年复一年 822

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

poj1564 Sum it up

搜索

大洋深处 1607

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

poj1564Sum It Up【dfs】

Language: Default Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7035   Accepted: 3673 Description Given a specified total t and a list of n i

走一步再走一步 623

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

POJ 1564 Sum It Up——DFS

POJ DFS基础题

Vontroy 1064

POJ 1564 Sum It Up

题目链接 #include<iostream> #include<algorithm> using namespace std; const int N=20; int t,n; int a[N],ans[N],sum; bool flag; bool cmp(int a,int b){ return a>b; } void dfs(int x,int su...

moyangxian 152

poj 1564 Sum It Up

Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5301   Accepted: 2672 Description Given a specified total t and a list of n integers, find all dist

记录成长的点点滴滴. . . . . 873

POJ 1564 Sum It Up dfs

来源:http://poj.org/problem?id=1564 题意:给你一个数sum,然后再给你一个数n,再给你n个数,从n个数中找出一些数,让这些数的和是n。所给的n个数是递减的,让输出的结果也是按照递减的形式输出。 思路:由于n最大为12,所以很明显可以暴力搜出所有的情况,然后判断和是否为sum,如果为sum,则输出即可。需要注意两点的是:首先有一个去重的过程,我的处理是把合适的全部

907

Poj 1564 Sum It Up

Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7921   Accepted: 4065 Description Given a specified total t and a list of n integers, find all distin

guihaiyuan123的博客 378

poj 1564 Sum It Up DFS

题目描述: DescriptionGiven 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,

ww的博客 278
上一篇: hdu 1427 速算24点
下一篇: poj 1330 Nearest Common Ancestors
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值