杭电ACM2058--The sum problem
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!=||b!=))
{
for (i=;i<=a;i++)
{
k = ;
for (j=i;j<=i+(int)sqrt((double)(*b))+;j++)
{
k = k+j;
if (k == b)
printf("[%d,%d]\n",i,j);
}
}
printf("\n");
}
return ;
}
穷举是穷举,还不能任意穷举,要不超时!
设初始值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)(*b));j>=;j--)
{
i = (*b/j+-j)/;
if (b == (*i+j-)*j/)
printf("[%d,%d]\n",i,i+j-);
}
printf("\n");
}
return ;
}
</span>
杭电ACM2058--The sum problem的更多相关文章
- 杭电oj_2058——The sum problem(java实现)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 思路:等差数列公式变形:sum = a1 * len + len *(len -1)/2 抽象成 ...
- 杭电oj An easy problem
</pre><h1 style="color: rgb(26, 92, 200);">An easy problem</h1><stron ...
- 杭电 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电 1003 Max Sum (动态规划)
参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...
- 杭电 5053 the Sum of Cube(求区间内的立方和)打表法
Description A range is given, the begin and the end are both integers. You should sum the cube of al ...
- 杭电1003 Max Sum 【连续子序列求最大和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...
- 杭电1003 Max Sum TLE
这一题目是要求连续子序列的最大和,所以在看到题目的一瞬间就想到的是把所有情况列举出来,再两个两个的比较,取最大的(即为更新最大值的意思),这样的思路很简单,但是会超时,时间复杂度为O(n^3),因为有 ...
- 杭电 1002 A + B Problem II【大数相加】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 解题思路:就是把大的数用数组存放起来,像小学的时候用竖式加法来算两个数相加那样算: 反思:思路很 ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评
最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...
随机推荐
- __attribute__((unused))
在gcc手册中找到了有关的解释: unused:This attribute, attached to a function, means that the function is meant to ...
- Linux中命令链接操作符的十个最佳实例
转载: http://www.linuxeden.com/html/softuse/20140112/147406.html http://www.tecmint.com/chaining-opera ...
- 关于解决 Failed to prepare partial IU:
在新版本的Eclipse(Luna)中安装插件经常会碰到Failed to prepare partial IU的错误,一把都是兼容性的问题,要下载个兼容包,步骤如下: 1.打开安装插件的页面:Hel ...
- 3.1html学习之列表
一.含义: ul:unorder list ol:order list li:list item dl:definition list dt:definition term dd:definition ...
- oracle数据库常用SQL语句
1)删除表的一列 ALTER TABLE 表名 DROP COLUMN 列名; 2)增加表的一列 且默认值为0 alter table 表名 add 字段名 类型 default '0'; 3)修改表 ...
- ListView的addAll方法
add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当 ...
- 通过layer的contents属性来实现uiimageview的淡入切换
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong)CALayer ...
- 安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用
转载: CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用 控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个V ...
- 重构8-Replace Inheritance with Delegation(委托替换继承)
继承的误用十分普遍.它只能用于逻辑环境,但却经常用于简化,这导致复杂的没有意义的继承层次.看下面的代码: public class Sanitation{ public String WashHand ...
- MySQL中/*!40100注释
MySQL中的注释 MySQL中的注释有三种: # 注释内容 -- 注释内容 /* 注释内容*/ 但是,在导出的SQL文件中,也会看到类似如下内容的注释: CREATE DATABASE `blog` ...