HDU-2058-The sum problem(数学题技巧型)
题目链接
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(数学题技巧型)的更多相关文章
- HDU 2058 The sum problem 数学题
		
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
 - HDU 2058 The sum problem(枚举)
		
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
 - hdu 2058 The sum problem(数学题)
		
一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...
 - 题解报告:hdu 2058 The sum problem
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...
 - hdu 2058 The sum problem(简单因式分解,,)
		
Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...
 - HDU 2058 The sum problem
		
传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...
 - HDU - 2058 The sum problem(思路题)
		
题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...
 - HDU 2058 The sum problem (数学+暴力)
		
题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...
 - hdu 4961 Boring Sum(数学题)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...
 
随机推荐
- (转)MultipleOutputFormat和MultipleOutputs
			
MultipleOutputFormat和MultipleOutputs http://www.cnblogs.com/liangzh/archive/2012/05/22/2512264.html ...
 - 基于Verilog HDL 的数字时钟设计
			
基于Verilog HDL的数字时钟设计 一.实验内容: 利用FPGA实现数字时钟设计,附带秒表功能及时间设置功能.时间设置由开关S1和S2控制,分别是增和减.开关S3是模式选择:0是正常时钟 ...
 - Windows Server 2012 在个人终端上使用的推荐设置
			
Windows Server 2012,也就是 Windows 8 的服务器版本,相对于 Windows 8 企业版而言,增强了作为服务器的功能,弱化了作为终端系统的功能. 目前微软官方提供了 Win ...
 - JAVA基础--super关键字
			
子类对象new的时候堆里的内存分配: 1. 父类成员变量 2. 子类成员变量 3. this对象指向自己 4. super对象指向父类 super调用父类的方法: class FatherClass ...
 - CodeForces 614C Peter and Snow Blower
			
简单计算几何,只要算出圆心到多边形上的最短距离和最长距离即可 #include<cstdio> #include<cstring> #include<cmath> ...
 - SDWEBImage和collectionView的组合,以及collectionView的随意间距设置
			
#import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+Web ...
 - 11.10document对象练习
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - (简单) POJ  2253 Frogger,Dijkstra。
			
Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Fro ...
 - 3、手把手教你Extjs5(三)MVVM特性的简单说明
			
下面我们来看一下自动生成的代码中的MVVM架构的关系.Main是一个可视的控件,MainController是这个控件的控制类,MainModel是这个控件的模型类. 在上面的图片中,左边是Main. ...
 - ubuntu apache2 ssl配置
			
Ubuntu下HTTPS配置非常简单,对大部分用户而言,使用普通的自签名证书,只需按照步骤进行就可以了,无需了解密钥.证书的更多知识,更深的背景 知识还有RSA算法.DES算法.X509规范.CA机构 ...