题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2058

思路:

这题的n,m都很大,很显然直接暴力,会超时,那就不能全部都找了,利用等差数列求和公式,

(1)sn=n*(a1+an)/2; 即可代入公式,(2)m=(e-s+1)*(s+e)/2                         注释*******//s代表起点,e代表终点。

则由(2)推出,(3)e=(int)(2*m+s*s-s),根据e点找s点,代入(1)成立则输出[s,e]; 但新的

问题又出现了由于m可能很大所以(3)中e可能太大而溢出。也不行,

同样的思路,可以换一种写法,(4)m=j*(i+i+j-1)/2                                                                 注释*****//i代表起点,j代表数的个数;

那么 j=sqrt(2*m);  j>=1;j-- 一直递减找下去,由j算出i,代入i,j 如果 (4)成立

则输出【i,i+j-1】;

我的AC代码

#include<stdio.h>
#include<math.h>
int main(void)
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)==2&&(n+m))
{
for(j=sqrt(2*m);j>0;j--)
{
i=(2*m/j-j+1)/2;
 if(i+j-1<=n)                                      //这个题,数据很不全,这一行如果不写同样可过,但是错的, 如 n< m 错误就体现出来了。
if(j*(2*i+j-1)==2*m)
{
printf("[%d,%d]\n",i,i+j-1);
}
}
printf("\n");
}
return 0;
}

HDU-2058-The sum problem(数学题技巧型)的更多相关文章

  1. HDU 2058 The sum problem 数学题

    解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...

  2. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

  3. hdu 2058 The sum problem(数学题)

    一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...

  4. 题解报告:hdu 2058 The sum problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...

  5. hdu 2058 The sum problem(简单因式分解,,)

    Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...

  6. HDU 2058 The sum problem

    传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...

  7. HDU - 2058 The sum problem(思路题)

    题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...

  8. HDU 2058 The sum problem (数学+暴力)

    题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...

  9. hdu 4961 Boring Sum(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...

随机推荐

  1. 绑定网关mac,防arp攻击

    netsh i i show innetsh -c i i add neighbors 16 192.168.1.1 08-57-00-51-19-7c

  2. aapt: error while loading shared libraries: libstdc++.so.6: wrong ELF class: ELFCLASS64

    前阵子在ubuntu上搭载安卓的开发环境(Eclipse+Sdk+Adt),搭载是完成了,但是却出现了该问题: aapt: error while loading shared libraries: ...

  3. 发布时去掉 debug 和 提醒日志,简单无侵入

    在 proguard 文件中加入下面代码,让发布时去掉 debug 和 提醒日志,简单无侵入! -assumenosideeffects class android.util.Log { public ...

  4. Mac系统下安装Tomcat,以及终端出现No such file or directory的错误提示解决方案

    Tomcat,作为一个免费的服务器口碑实在太好,本想安装一个研究研究,无奈电脑是mac系统,在网上搜了一些安装方法总是出错,直到遇到了这篇博客,http://www.cnblogs.com/qingy ...

  5. 解决adb command not found以及sdk环境配置

    解决adb command not found以及sdk环境配置 分类: mark 2013-10-02 09:41 2975人阅读 评论(0) 收藏 举报 原文地址:http://www.cnblo ...

  6. php查找文件内容

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><?ph ...

  7. <meta http-equiv="refresh" content="0; url=">

    原文:http://www.cnblogs.com/net2/archive/2010/11/29/1890874.html 页面定期刷新,如果加url的,则会重新定向到指定的网页,content后面 ...

  8. Highcharts选项配置详细说明文档

    Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,目前官网只提供英文版的开发配置说明文档,而中文版的文档网上甚少,且零散不全.这里,我把Highcharts常用的最核心的参 ...

  9. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. 转 [ javascript面向对象技术

    以下文章来自iteye,作者是 sdcyst ,个人主页 http://www.iteye.com/topic/288813 类变量/类方法/实例变量/实例方法先补充一下以前写过的方法:在javasc ...