杭电ACM2058--The sum problem
来源:互联网 发布:开源淘宝客系统 编辑:程序博客网 时间:2024/06/14 11:00
http://acm.hdu.edu.cn/showproblem.php?pid=2058
以为简单的穷举就完了,结果是一直Time Limit Exceeded。。
这是代码:
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a,b;int i,j,k;while (scanf("%d%d",&a,&b)!=EOF&&(a!=0||b!=0)){for (i=1;i<=a;i++){k = 0;for (j=i;j<=i+(int)sqrt((double)(2*b))+1;j++){k = k+j;if (k == b)printf("[%d,%d]\n",i,j); }}printf("\n");}return 0;}
穷举是穷举,还不能任意穷举,要不超时!
设初始值i,个数为j的数列满足要求,则(i+i+j-1)*j/2=m
==>(2*i-1+j)*j=2m
所以j肯定小于等于sqrt(2*m),穷举。
之后才发现数据量之大,然后找到了解决办法。
<span style="font-size:24px;">#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a,b;int i,j,k;while (scanf("%d%d",&a,&b)!=EOF&&a&&b){for (j=(int)sqrt((double)(2*b));j>=1;j--){i = (2*b/j+1-j)/2;if (b == (2*i+j-1)*j/2)printf("[%d,%d]\n",i,i+j-1);}printf("\n");}return 0;}</span>
0 0
- 杭电ACM2058--The sum problem
- 杭电acm2058
- 杭电ACM 2058 The sum problem
- 杭电2058-The sum problem
- 杭电 2058 The sum problem
- 杭电---2058 The sum problem
- 杭电2058 The sum problem
- 杭电2058 The sum problem
- 杭电 2058 the sum problem
- 杭电Problem 5053 the sum of cube 【数学公式】
- 杭电acm1001 Sum Problem
- 杭电1001 Sum Problem
- 杭电 1001 Sum Problem
- 杭电1001 - Sum Problem
- 杭电1001 Sum Problem
- 杭电P1001 Sum Problem
- 杭电1001-Sum Problem
- 【杭电】[1001]Sum Problem
- 检测版本更新
- Android Volley完全解析(四),带你从源码的角度理解Volley
- 在cocos2d-js实现自动绑定cocostudioUI控件与事件(二)
- Android IOS WebRTC 音视频开发总结(八)
- [前端] IE6、IE7、IE8实现盒子阴影shadow
- 杭电ACM2058--The sum problem
- ACE_INET_Addrconnect: address not available 问题的解决
- JS页面跳转代码
- select地址下拉框模拟(宽度根据内容自适应)
- C++定义比较函数的几种方法
- android后台截屏实现(1)--源码编译
- OpenFire源码学习之三十三:windows打包
- Android IOS WebRTC 音视频开发总结(九)
- MVC 中使用TreeView