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. C#解leetcode 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  2. C#之—委托

    (1)定义委托:(百度百科样例,只有写了才有收获) namespace Entrust { public delegate void GreetingDelegate(string name); // ...

  3. css.day01.eg

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. HTTPS双向认证

    生成证书 openssl genrsa -des3 -out server.key 2048 openssl req -new -x509 -key server.key -out ca.crt -d ...

  5. (转)一步一步学习PHP(4)——函数

    相信每个人在学习PHP之前至少都有着一定的C语言,或者是C++/Java/C#等其他语言的基础,所以在这里也不从头开始说起,只是来谈谈PHP方法的独特之处. 1. 解决作用域问题 在上一节谈到了PHP ...

  6. ASP.NET常用编程代码(一)

    1.为按钮添加确认对话框 Button1.Attributes.Add("onclick","return confirm(’确认?’)");button.at ...

  7. Swift - 14 - 字符串的基础操作

    //: Playground - noun: a place where people can play import UIKit // 拼接 var str = "Hello, playg ...

  8. SQL UPDATE 经典

    1 sql中用另一个表的一列来更新数据库表 SELECT TOP 1000 [a] ,[b] ,[c] FROM [单元测试项目].[dbo].[A] SELECT TOP 1000 [a] ,[b] ...

  9. 你好,C++(37)上车的人请买票!6.3.3 用虚函数实现多态

    6.3.3  用虚函数实现多态 在理解了面向对象的继承机制之后,我们知道了在大多数情况下派生类是基类的“一种”,就像“学生”是“人”类中的一种一样.既然“学生”是“人”的一种,那么在使用“人”这个概念 ...

  10. MyBatis学习笔记(3)—— 利用mybatis灌入假数据

    由于第三方厂商未能按时提供实时数据,故需要纯手动导入一些实时数据,用于统计分析.正好最近自己学习了mybatis .因此使用mybatis 配置一个select.insert 的简单操作语句,用于灌入 ...