HDU 2058 The sum problem [数学]
来源:互联网 发布:如何做好数据化管理 编辑:程序博客网 时间:2024/06/04 17:54
从今天开始 水题再不记!
凡是记得题,都是不水的。
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"); }}
0 0
- HDU 2058 The sum problem [数学]
- hdu 2058 The sum problem (数学问题)
- hdu 2058 The sum problem
- hdu 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- hdu---2058The sum problem
- The sum problem hdu 2058
- hdu 2058 The sum problem
- HDU 2058 The Sum Problem
- HDU-2058 The sum problem
- hdu 2058 The sum problem
- HDU 2058 The sum problem
- 12C CDB模式下RMAN备份与恢复
- 深入理解乐观锁与悲观锁
- 一些思维性的东西
- VB.NET中使用委托(Delegate)来注册回调(callback)函数
- Asio 核心概念和功能文档学习(一)
- HDU 2058 The sum problem [数学]
- windows 常用端口连接查看命令
- Android设置系统开机自动永不休眠
- Codeforces C1. Brain Network (easy)
- iOS中自定义界面统计(统计一定时间内访问次数,以及停留时间)
- Linux的inode的理解
- 【hibernate】一对多(客户与订单双向关联)遇到的异常
- UIView的layoutSubviews和drawRect方法何时调用
- DebugView——经典调试输出工具