传送门

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. iptables/Netfilter 学习

    开始学iptables,因为它是和路由器技术紧密结合在一起的. iptables的命令看起来眼花缭乱,随便找两个: iptables -A FORWARD -p tcp -s -d -j ACCEPT ...

  2. .html 、.htm 、 .shtml 以及 .shtm 四种扩展名的文件区别

    新增了一个分类,叫做 Personals,中文我把它解释成 "个人恶趣味",这里将记载一些对工作无关紧要,但是个人又一时有兴趣了解的东西. 今天要讲的是如题的 4 种扩展文件的区别 ...

  3. C# StopWatch的使用

    在做项目的时候,需要输出数据库操作的耗时,自己写了个方法.老大看到后,奇怪我为什么不用现成的.才知道有StopWatch这个类. 属性       名称 说明 Elapsed 获取当前实例测量得出的总 ...

  4. Android开发自学笔记(Android Studio1.3.1)—1.环境搭建

    一.引言 .Google推出的 毫无疑问,这个是它的最大优势,Android Stuido是Google推出,专门为Android"量身订做"的,是Google大力支持的一款基于I ...

  5. fstab 中 通过UUID挂载 参数解释

    UUID=cf474122-1d51-4953-846d-9ce1c8d23ae6 / ext4 defaults 1 1UUID=ef21d494-0dc7-41ec-95b2-a691bfd4e5 ...

  6. C#操作Excel时的格式设定(转)

    Excel报表打印的格式设定 1.     表头的设置 Excel._Worksheet myWorksheet; myWorksheet.PageSetup.Orientation = Excel. ...

  7. 【转】关于Class.forName(“com.mysql.jdbc.Driver”)

    原文:http://www.cnblogs.com/gaojing/archive/2012/03/23/2413638.html 传统的使用jdbc来访问数据库的流程为: Class.forName ...

  8. chgrp 简明笔记

    改变与文件相关联的组 chgrp [options] group file-list 参数 group 为新组的名称或者数值ID,file-list 为要改变其相关联组的文件路径名列表 选项 -c   ...

  9. 3-cd 命令总结

  10. webpack入坑之旅(三)webpack.config入门

    这是一系列文章,此系列所有的练习都存在了我的github仓库中vue-webpack,在本人有了新的理解与认识之后,会对文章有不定时的更正与更新.下面是目前完成的列表: webpack入坑之旅(一)不 ...