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. spring定时器任务多任务串行执行问题排查

    最近发现个生产问题,定时器任务某些任务没有及时执行.经过研究排查发现spring 定时器任务scheduled-tasks默认配置是单线程串行执行的,这就造成了若某个任务执行时间过长,其他任务一直在排 ...

  2. Python | 基础系列 · Python为什么没有switch/case语句?

    与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...

  3. COGS 渡轮问题 (LIS规定字典序输出方案数)

    /* 下标字典序最小 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1 ...

  4. codevs 1242 布局(查分约束+SPFA)

    /* 查分约束. 给出的约束既有>= 又有<= 这时统一化成一种 Sb-Sa>=x 建边 a到b 权值为x Sb-Sa<=y => Sa-Sb>=-y 建边 b到a ...

  5. jQuery的选择器中的通配符[id^='code']或[name^='code']

    这两天在做一个专题的时候遇到了一个通配符的问题 //弹层操作$(function(){ //视频播放 $("a[href^='#video']").each(function(in ...

  6. jquery对同级的td做radio限制

    <html> <head> <title></title> <script src="http://libs.baidu.com/jqu ...

  7. project facet java version 1.6 is not supported

    可能你用的jdk1.5的包,而开发是用的jdk1.6,不允许1.5进行安装 法1,选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Vers ...

  8. .net 计算当前时间距离今晚00:00:00还有多少分多少秒

    string dateDiff = null; DateTime DateTime1 = DateTime.Now; //第二天的0点00分00秒 DateTime DateTime2 = DateT ...

  9. SQL 查询的执行过程

    所述内容均来自互联网,文章仅作为学习笔记,备忘使用. 有时候我在想我们总是在谈优化,FA 优化结构.优化框架.优化程序…,可是我真的了解将要进行的操作[优化]吗?以最近我的工作-优化SQL为例,我真的 ...

  10. 报错:ORA-02287: 此处不允许序号

    CREATE TABLE MY_TAB (N1 NUMBER(5),N2 DATE);          SELECT  * FROM MY_TAB;          CREATE SEQUENCE ...