传送门

Description

Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.

Input

Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.

Output

For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.

Sample Input

20 10 50 300 0

Sample Output

[1,4] [10,10]

[4,8] [6,9] [9,11] [30,30]

思路

((首项+末项)*项数)/2=m以及 末项=首项+项数-1联立方程组可以得到一个关于左区间项,项数,M的一个方程。
假设首项是1,我们代入,很容易得到n(n+1)=2m这个公式(n是项数)。 这里可以把n+1看成n,n^2=2m,n=sqrt(2m); 也就是说项数最多的情况也只有sqrt(2m)。大大减小了枚举长度。另外,(a+a+len)*(len+1)/2 = m => a = m/(len+1)-len/2
#include<stdio.h>
#include<math.h>

int main()
{
    int N,M,val,len;
    while (~scanf("%d%d",&N,&M) && N && M)
    {
		len = (int)sqrt(2*M);
        while (len--)
        {
            val = M / (len + 1) - len /2;
            if ((2*val+len) * (len+1) / 2 == M)
                printf("[%d,%d]\n", val, val+len);
        }
        printf("\n");
    }
    return 0;
}

HDU 2058 The sum problem的更多相关文章

  1. HDU 2058 The sum problem(枚举)

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

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

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

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

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

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

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

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

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

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

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

  7. HDU 2058 The sum problem 数学题

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

  8. HDOJ 2058 The sum problem

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

  9. HDU 2058:The sum problem(数学)

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. Scala入门之Array

    /** * 大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: * 1,数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储现在比较经典的是使用Hadoop,也有很多情况使用Kaf ...

  2. ALinq Dynamic 使用指南——慨述(上)

    一.使用 1.程序集与命名空间的引用使用 ALinq Dynamic,你需要引用ALinq.Dynamic.dll(ALinq用户)或者System.Data.Linq.Dynamic.dll (Li ...

  3. 东大OJ-一元三次方程解的个数

    1043: Fixed Point 时间限制: 5 Sec  内存限制: 128 MB 提交: 26  解决: 5 [提交][状态][讨论版] 题目描述 In mathematics, a fixed ...

  4. 东大OJ-1430-PrimeNumbers

    题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 The ...

  5. 转:PHP中防止SQL注入的方法

    [一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最 ...

  6. 1025基础REDIS

    -- 登录AUTHPING -- 通用命令EXISTS KEY EXPIRE KEY seconds 为给定 KEY 设置过期时间 -- 字符SET runoobkey redisDEL runoob ...

  7. 模拟发送http请求

    1.httpie 2.postman:Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件. 3.fiddler

  8. 利用ajaxfileupload.js异步上传文件

    1.引入ajaxfileupload.js 2.html代码 <input type="file" id="enclosure" name="e ...

  9. 选项卡js

    趁着公司不忙,抓紧充充电,开始可能会写的不好,但是每写一个都是一点进步,哈哈,加油 用js实现选项卡切换 1.获取元素 2.初始状态 3.通过循环清空元素状态 4.点击操作以及对应的内容切换 5.自定 ...

  10. REST服务返回自定义的HttpResponseMessage

    WebApi框架中对资源的操作,都是通过其Controller提供的各种方法(GET,POST,PUT,DELET等)来实现,而这些方法的返回信息有以下几种形式: 方法返回类型 HttpRespons ...