HOJ 2058 The sum problem
HOJ 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 contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.
Output#
For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.
Sample Input#
20 10
50 30
0 0
Sample Output#
[1,4]
[10,10]
[4,8]
[6,9]
[9,11]
[30,30]
滑动窗口#
本来觉得这题没难度,就滑动窗口直接做了。
#include "iostream"
#include "cstdio"
using namespace std;
__int32 min(__int32 x, __int32 y) {
return x > y ? y : x;
}
int main() {
__int32 N,M;
__int32 left, right,sum;
while (scanf("%d %d", &N,&M) != EOF) {
if (N == 0 && M == 0)break;
left = right = 1;
sum = 1;
while (right <= min(N,M)) {
if (sum < M) {
sum += ++right;
continue;
}
if (sum == M) {
printf("[%d,%d]\n", left, right);
}
if (sum >= M) {
sum -= left++;
continue;
}
}
printf("\n");
}
return 0;
}
但是当数据量过大,还是会挂。
优化#
看了别人的代码,优化了一下
题目是要求等差数列[1,N]中的某一段累加等于M,根据等差数列求和公式,也就是说我们要找到。(len是等差数列中的n,因为题中已有一个N,避免混淆。)
所以题目转换成了,我们找到一个起始位置和一个结束位置,如果满足上面的公式,则输出这两个位置。
但如果硬找起始位置和结束位置,我们发现还是要很高的复杂度,我们从长度入手,发现把上面的公式变化形态,得到这样的公式:
把这个数列所有可能的长度遍历一遍,就能求出并且
#include "iostream"
#include "cstdio"
using namespace std;
int main() {
int N,M,len,maxLen,t;
while (scanf("%d %d", &N,&M) != EOF) {
if (N == 0 && M == 0)break;
maxLen = sqrt(M*2);
for (len = maxLen; len > 0; len--) {
t = M - len * (len - 1) / 2;
if (t % len == 0)
printf("[%d,%d]\n", t / len, t / len + len -1);
}
printf("\n");
}
return 0;
}
作者:Yudoge
出处:https://www.cnblogs.com/lilpig/p/13909360.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
欢迎按协议规定转载,方便的话,发个站内信给我嗷~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)