The sum problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20726    Accepted Submission(s): 6100

Problem 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 30
0 0

Sample Output

[1,4]
[10,10]

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

Author

8600

Source

校庆杯Warm Up

Recommend

linle   |   We have carefully selected several similar problems for you:  20592062206020722061

Statistic | Submit | Discuss | Note

这道题显然不可能直接暴力做出来。技巧在于所有可能数列的长度有最大值。设数列为a+1,a+2 ,…… a+len,则它的长度为len。

根据公式M=[(a+1)+(a+len)]*len/2化简得出M*2=len*len+(2a+1)*len。所以len一定小于sqrt(2*M)接下来就可以枚举lenAC这道题。

 #include<stdio.h>
#include<math.h> int main()
{
int N, M;
while(~scanf("%d%d", &N, &M)) {
if(M == && N == ) break;
int len = sqrt(double(M * ));//如果不加double会出现编译错误,sqrt的参数是浮点型的。
while(len) {
int a = M / len - ( + len) / ;
if((a + + a + len) * len / == M) printf("[%d,%d]\n", a + , a + len);
len--;
}
puts("");
}
return ;
}

HDU2058的更多相关文章

  1. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

  2. HDU100题简要题解(2050~2059)

    HDU2050 折线分割平面 题目链接 Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以 ...

随机推荐

  1. Java基础知识强化之集合框架笔记06:Collection集合存储自定义对象并遍历的案例

    1.练习:用集合存储5个学生对象,并把学生对象进行遍历. 分析: (1)创建学生类(2)创建集合对象(3)创建学生对象(4)把学生添加到集合(5)把集合转成数组(6)遍历数组 2. 代码示例: Stu ...

  2. Android滚动截屏,ScrollView截屏

    在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...

  3. BestCoder冠军赛 - 1009 Exploration 【Tarjan+并查集缩点】

    [题意] 给一个图,这个图中既有有向边,又有无向边,每条边只能走一次,问图中是否存在环. 最多10^6个点,10^6个无向边,10^6个有向边 [题解] 因为既有有向边又有无向边,所以不能单纯的用ta ...

  4. centos 6+安装山逗斯骚尅特(本文内容来自都比更具帝)

    系统支持:CentOS 6+,Debian 7+,Ubuntu 12+ 内存要求:≥128M 关于本脚本 一键安装 Shadowsocks-Python, ShadowsocksR, Shadowso ...

  5. mac 卸载java

    由于电脑上的jdk版本和项目组使用的版本不一致,因此需要卸载,但是作为一个新人小白加没有使用mac的过多经验,还是稍微费了一些些功夫的,从网上查的资料,终于解决这个问题,因此记录一下. 参考博客:ht ...

  6. 一些 Windows 系统不常见的 鼠标光标常数

    一些 Windows  系统不常见的 鼠标光标常数 Private Declare Function SetCursor Lib "user32" (ByVal hCursor A ...

  7. HTML简要内容

    1.  html基础 html是用来制作网页的标记语言,不需编译,直接由浏览器执行.大小写不敏感,推荐使用小写.html文件必须使用html或htm为文件名后缀. html主体结构: (1)DTD头: ...

  8. 应用app首次进入导航页动画

    import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActi ...

  9. bootstrap新闻模块样式模板

    <!-- news beginning --> <div class="container mp30"> <div class="row&q ...

  10. 二维码生成Demo

    在C#中直接引用ThoughtWorks.QRCode.dll 类, 下载 dll 类 http://file.111cn.net/download/2013/06/29/20120516165420 ...