杭电oj —— 2058 (超时优化)

The sum problem——关于连续子数列的求和问题 The sum problem 阅读详情

超时代码:思路易想到,但是数据量太大

package com.demo3;
import java.util.Scanner;
/*
 * Given a sequence 1,2,3,......N, 
 * your job is to calculate all the possible sub-sequences(子序列) that the sum of the sub-sequence is M.
 */
public class HDU_oj2058 {
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		long n,m,sum,start,end=0;
		while(sn.hasNext()) {
			n = sn.nextLong();
			m = sn.nextLong();
			if(n == 0 && m == 0) {
				break;
			}
			for(long i = 1;i <= n;i++) {
				sum = 0; 
				start = i; //首先从i开始
				if(start <= m) { //如果刚好遍历到这个值等于m
					end = start;
					while(sum < m) { //遍历序列
						sum += end;
						end++;
					}
					if(sum == m) {
						end = end-1;
						System.out.println("[" + start + "," + end +"]");
					} else {
						continue;
					}
				}
			}
			System.out.println();
		}
		sn.close();
	}
}

正确代码如下:数据规模太大,需要整数分解,思路见代码注释

package com.demo3;
import java.util.Scanner;
/*
 * 整数分解:(首项+末项)*项数 /2 
 */
public class HDU_oj2058_1 {
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		int len, temp;
		while (sn.hasNext()) {
			int N = sn.nextInt();
			int M = sn.nextInt();
			if(N == 0 && M == 0)
				break;
			/*
			 * len = (int) Math.sqrt(M * 2.0);
			 * 思路分析:
			 * 等差数列,公差为1,假设首项是a,项数是b;  等差数列求和公式:(a1+an)*n / 2;
			 * 则 M = (a + (a+b-1))*b /2; 
			 * 所以:2M = (a + (a+b-1))*b = (2a + b - 1)*b 
			 * 且:a>=1
			 * 所以:2M >= b^2
			 * 即:项数b <= 根号2M
			 */
			len = (int) Math.sqrt(M * 2.0);
			
			/*
			 * temp = M - (i * i + i) / 2;
			 * 思路分析:
			 * M = (a + (a+b-1))*b /2; 
			 * 2M = 2ab + b^2 -b
			 * 2ab = 2M - b^2 + b
			 * ab = (2M - b^2 + b) / 2 = M - (b^2 - b)/2;
			 * 令temp = ab;
			 * temp / a = b 项数
			 * temp / b = a 首项
			 */
			for (int i = len; i >= 1; --i) { //这里的i就是思路中表示的b(项数)
				temp = M - (i * i - i) / 2; 
				if (temp % i == 0) { //首项为整数
					// 首项:(temp / i) 
					// 末项:(temp / i + i- 1) 首项+项数-1
					System.out.println("[" + (temp / i) + "," + (temp / i + i - 1) + "]");
				}
			}
			System.out.println();
		}
		sn.close();
	}
}

 

【题解】- hdu The sum problem【技巧】 The sum problem Given a sequence 1,2,3,…N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. Input Input contains multiple test cases. each case contains t... 阅读详情

相关推荐

保姆级教程:SAP ABAPer如何为VF01销售发票和MIRO发票校验配置凭证替代(含AC_DOCUMENT BADI完整代码示例)

本文详细解析了SAP ABAP中通过AC_DOCUMENT BADI实现VF01销售发票和MIRO发票校验的凭证替代技术。从技术选型到BADI实施步骤,再到核心逻辑代码示例,为ABAP开发者提供了一套完整的解决方案,帮助应对财务部门复杂的业务需求。

weixin_30421809的博客 360

杭电ojC++--2058 The sum problem AC代码

问题描述 Given a sequence 1,2,3,…N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. 输入 Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N =

weixin_42806016的博客 377

IPMI的SOL (Serial Over LAN) 和 UART

UART全名是Universal Asynchronous Receiver/Transmitter(通用異步接收/发送器),它是一个常用的異步串行传输协议。透过UART传输前,接收方须要先知道传输方的鲍率(Baud rate):单位时间内,讯号状态变化的次数。例如 115200 bps,表示每秒会传送115200个bits--> 因此读取率就是 1/115200=8.6微秒读取一次讯号。封包内的资料长度:通常是 8bits。

yeiris的博客 1万+

杭电OJ 2058 The sum problem C++

len--) { // len表示项数。if ((2 * a + len - 1) * len / 2 == m) { // 求和。

Estelle1412的博客 670

PCB层叠结构与阻抗计算

1.Core 和 PP PCB两个重要组成部分:Core 和 Prepreg(半固态片,简称PP)。 如下图所示, Core 的两个表面都铺有铜箔,可作为信号层、电源层、地层等导电层,Core的上下表面之间填充的是固态材料; PP的表面不铺铜,在PCB中起填充作用,其材质是半固体的树脂材料,因此比Core略软一些。 在制作多层板时,需配合使用Core和PP,一般在两个Core 之间应选PP作为填...

sinat_15677011的博客 1万+

AUTOSAR进阶图解==>AUTOSAR_SWS_DefaultErrorTracer

AUTOSAR Default Error Tracer (DET)是AUTOSAR架构中的核心错误追踪模块,提供统一的错误报告机制。DET能够处理开发错误、运行时错误和瞬态故障三类问题,支持模块级和实例级的配置管理。模块架构上,DET通过Det_API接口与其他模块交互,可配置转发错误至诊断事件管理器(DEM)或调试日志追踪(DLT)。其配置结构包含全局参数和模块实例参数,允许灵活控制错误报告行为。DET在AUTOSAR 4.3.0版本中从DevelopmentErrorTracer扩展而来,增强了错误处

一名留德华的,康德的信徒的,专注于汽车电子领域的博客 916

杭电oj刷题(2058)

The sum problem 题目描述: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. Input Input contains multiple test cases. each...

lucy girl 744

Python爬虫实战:美团商家数据采集系统

本文将详细介绍如何使用Python最新技术栈构建高效的美团商家信息采集系统。内容涵盖异步爬虫、反爬对抗、数据存储、可视化分析等完整流程,提供可落地的代码方案和工程实践。

2201_76125261的博客 2669

【杭电oj】2058 - The sum problem(高斯公式)

点击打开题目 The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 22089    Accepted Submission(s): 6508 Problem Description

wyg1997 1932

AI穿透式智慧粮仓

本报告基于 2025-2026 年全国多地智慧粮储实践案例,系统分析了 "AI + 穿透式" 智慧粮储体系的技术架构、AI 算法应用、提质增效成效、区域差异化特点及未来发展趋势。该体系通过多模态感知、智能算法与管控平台的深度融合,实现粮食存储全流程精准感知、智能预警与科学调控,显著降低储粮损耗与运营成本,大幅提升储粮安全水平与管理效率。报告梳理了各地差异化实践路径,提出针对性发展建议,为推动我国粮食仓储高质量发展、筑牢国家粮食安全防线提供参考。

reset2021的博客 236

机器学习实战4-教育领域:学生成绩的可视化分析与成绩预测-详细分析

大家好,我是微学AI,今天给大家带来机器学习实战4-学生成绩的可视化分析与成绩预测,机器学习在教育中的应用具有很大的潜力,特别是在学生成绩的可视化分析与成绩预测方面。机器学习可以通过对学生的父母教育情况和学校表现等数据进行分析和挖掘,从而揭示潜在的学习模式和趋势。

微学AI的博客 7689

杭电OJ2058

杭电OJ2058 我写的超时了 下面是不超时的 #include <stdio.h> #include <math.h> int main() { int n, m, i, j; while(scanf("%d%d", &n, &m) != EOF) { if(n==0 && m==0) break; for(j=(int)sqrt((double)(2*m)); j>=1; j--) { i = (

大权子的博客 326

组合导航 | RTK、IMU与激光雷达组合导航算法:原理、实现与验证

当激光雷达噪声增加至0.1m时,系统位置RMSE仅上升18%,验证了滤波算法的鲁棒性[110]:融合方案在RTK信号丢失30秒后,位置误差仍控制在1.5m内,显著优于IMU单独导航[28]:ESKF框架能有效建模传感器误差特性,状态向量需包含外参与时间偏移等关键参数。:传感器标定精度直接影响系统性能,建议每3个月进行一次重新标定。:IMU预积分与特征匹配是系统实时性的关键瓶颈,需重点优化。:维持10秒窗口内的关键帧,平衡精度与实时性[132]:特征匹配占总耗时的65%,可通过。

尘世冰封的专栏 959

杭电oj HDOJ 2058 The sum problem(寻找子列)

杭电oj HDOJ 2058 The sum problem Problem Description Given a sequence 1,2,3,…N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. Input Input contains multip...

qq_39880604的博客 583

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

杭电oj2058

The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 32853 Accepted Submission(s): 9775 Problem Description Given a sequence 1,2,...

Sup_klz的博客 909

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

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

杭电2058——The sum problem

杭电acm2058————The sum problem(C语言)原题: Problem DescriptionGiven a sequence 1,2,3,……N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.InputInput contains mu

Ember_Sky 2279

2058杭电oj思路java_杭电 acm 2058 ( The sum problem )

The sum problemTime Limit:5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K(Java/Others)Total Submission(s): 11869 Accepted Submission(s):3582Problem DescriptionGiven a sequence 1,2,3,......N, yo...

weixin_33587731的博客 281

HDOJ 2058 The sum problem

Problem Description Given a sequence 1,2,3,……N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.Input Input contains multiple test cases. each case conta

谙绿Acm 493

杭电oj第2058题:The sum problem

每个情况包含两个整数 N, M( 1 <= N, M <= 1000000000)。而又因为 k * t = 2 * m 是固定的,就相当于你 2*5 = 10,你可以是序列长度为2,序列和是5.也可以是序列长度为5,4、这样就可以通过枚举k(序列长度)来计算,循环的次数就是(2*m)^1/2。所以一个k肯定只会对应一个大于sqrt(2m)的长度,就是t了,因为。你这里讨论的是看k大于(2m)^1/2的情况,就是用t作为序列长度。k小于(2m)^1/2的情况,就是用k作为序列长度。k和t是可以互换的。

2401_86314529的博客 630

杭电OJ2058 —— 关于和的故事

另外,由于我们是建立在式①成立的基础之上的,所以在区间长度k符合要求的情况下,只需要 以序列起点a是否是整数来判断 这组a和k是否是符合要求的答案 即可。为了按照题目要求输出结果,k不可以从1开始增加,否则先输出的会是 比如20 10对应的就是[10,10] [1,4]。,在这题的情境下,公差d为1,而首项a_1可以设为a,项数n可以设为k,和Sn就对应着m。这是解决问题的核心要点。而在满足此式的基础之上我们知道,k和a始终是大于等于1的整数,所以有。,如首项为1,项数为4,则表示的是[1,4]。

weixin_55592563的博客 1523

杭电acm2058

J 如果不这样写会超市#include #include int main() { int n, m, i, j; while(scanf("%d%d", &n, &m) != EOF) { if(n==0 && m==0) break; for(j=(int)sqrt((double)(2*m)); j>=1; j--) { i = (2*m/j+1

寂寞灵魂 1504

杭电ACM 第2058题

#include&lt;iostream&gt; #include&lt;string&gt; #include&lt;math.h&gt; #include&lt;stdlib.h&gt; using namespace std; void main(){ long int n,m; while(cin&gt;&gt;n&gt;&gt;m&amp;&amp;(n!=0&

PeixinYe的博客 499

杭电ACM——2058

看题: Given a sequence 1,2,3,…N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. Input contains multiple test cases. each case contains two integers N, M( ...

shamansi99的博客 679

杭电oj2050-2058————C语言

2050.折线分割平面 http://acm.hdu.edu.cn/showproblem.php?pid=2050 直线:第n条与之前的有 n-1 个交点,(n-1)+1 个平面; 折线:第n条最多与之前的有2∗2(n−1)交点(一折两直), 2∗2(n−1)+1个平面, 递推f(n)=f(n−1)+4n−3 第一种: #include <stdio.h> int cal(int n...

AKA-BAO的博客 820

HDOJ2058_The sum problem

Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.   Input Input contains multiple test cases. each

BarryLee的博客 411

The sum problem

The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16197 Accepted Submission(s): 4843 Problem Description Given a sequence 1,

Briup-z.changhai 697
上一篇: 杭电oj —— 2057(十六进制A+B)
下一篇: 杭电oj —— 2056(求两个矩形相交部分面积)
LiLi_code
博客等级 码龄8年 40粉丝 154原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值