从今天开始 水题再不记!
凡是记得题,都是不水的。
Description
输入N M
在1..N中找到连续字串的和==M
每一个输出之后空一行
Sample Input
20 10
50 30
0 0
Sample Output
[1,4]
[10,10]
[4,8]
[6,9]
[9,11]
[30,30]
Algorithm
数学题
设要输出的 [x,x+y]
则有
(x + x+y)*(y+1)/2 = M
2M = (2x+y)*(y+1)
2M = 2x(y+1) + y(y+1)
x = (2M-y(y+1))/2(y+1)
x = M/(y+1) - y/2
枚举y就可以找到x
当然了,一开始还有点问题,比如以为y不需是2的倍数,看Sample不就知道不是了= =
当然了,y的范围一开始也没确定,以为是sqrt(m),然后WA
在王老师的指导下继续满足x>0
则有
x = M/(y+1) - y/2 > 0
M - (y+1)y/2 > 0
y(y+1) < 2M
大概sqrt(2M)+1可以吧,我提交的时候改成了+10
Code
#include <cmath>
#include <cstdio>
#include <iostream>
#include <climits>
using namespace std;
int main()
{
// freopen("input.txt", "r", stdin);
for (;;) {
long long n, m;
scanf("%lld%lld", &n, &m);
if (n == 0 && m == 0) break;
int i = sqrt(2 * m) + 1;
for (; i >= 0; i--) {
double x = double(m)/(i + 1) - double(i)/2;
if (x > 0 && x + i <= n && x == floor(x)) {
long long ansx = floor(x);
printf("[%lld,%lld]\n", ansx, ansx + i);
}
}
printf("\n");
}
}
本文介绍了一种求解连续子序列和等于给定值M的算法,通过枚举子序列长度并计算起始位置的方法解决该问题。文章提供了完整的算法思路及C++实现代码。
1124

被折叠的 条评论
为什么被折叠?



