The sum problem

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

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]   这也是一道水题,不过一开始没看懂题意,后面读了几遍就懂了。 题目给两个数N,M,要求找出从1开始到N里的子序列的和是M,并全部输出,一开始可能会想到求和公式,首项加末项乘以项数除以二,公式里有两个变量,题目告知M可以是一个很大的数,如果利用公式直接穷举去遍历所有的数的话的话会溢出,所以要换个角度去想。   考虑子串的长度为j,首项为i的数列满足要求,则(i+i+j-1)*j/2=m,==>(2*i-1+j)*j=2m,其中2*i-1>0; 所以j*j<2*m;j<sqrt(2*m);这样确定子串的长度了; 利用j和m,求出i的表达式,接下来就很简单了:  
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n,m,i;
while(cin>>n>>m && !(n==0 && m==0)){
int j = sqrt((double)2*m);
for(j;j>=1;j--){ //去掉douubl会在提交时显示编译错误,要声明函数重载时的类型
i =(2*m/j+1-j)/2;
if((2*i-1+j)*j/2==m)
cout<<"["<<i<<","<<i+j-1<<"]"<<endl;
}
cout<<endl;
}
return 0;
}

HD2058The sum problem的更多相关文章

  1. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  2. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  3. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  5. HDU 2058 The sum problem(枚举)

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

  6. NYOJ--927--dfs--The partial sum problem

    /* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...

  7. 动态规划法(三)子集和问题(Subset sum problem)

      继续讲故事~~   上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界.   这天,丁丁刚回到家,他 ...

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

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

  9. Problem-1001:Sum Problem

    Sum Problem Sample code : #include <stdio.h> int main() { int i,n; int sum; while(scanf(" ...

随机推荐

  1. Repeater上下排序按钮

    aspx代码 <table cellspacing="0" cellpadding="0" width="100%" align=&q ...

  2. [转载]mysql的binlog安全删除

    [转载]mysql的binlog安全删除  理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越 ...

  3. Odoo 的库存管理与OpenERP之前的版本有了很大的不同,解读Odoo新的WMS模块中的新特性

    原文来自:http://shine-it.net/index.php/topic,16409.0.html 库存移动(Stock Move)新玩法Odoo的库存移动不仅仅是存货在两个“存货地点”之间的 ...

  4. php开启curl扩展

    配置方法: 1.拷贝PHP目录中的libeay32.dll 和 ssleay32.dll 两个文件到 system32 目录. 2.修改php.ini:配置好 extension_dir ,去掉 ex ...

  5. 自己构建MVC中的M

    /** * @ description Model MVC中M 数据模型 * @ Object * @ public * @ create method IE不支持 */ if(typeof Obje ...

  6. 【C#学习笔记】从粘贴板复制文本

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 隐藏 php apache 的版本号

    隐藏Apache版本信息 找到apache安装路径,譬如\Apache\conf\httpd.conf 修改httpd.conf ServerTokens ProductOnly ServerSign ...

  8. Linux/Unix shell 监控Oracle实例(monitor instance)

    使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等.本文给出Linu ...

  9. mysql innodb锁简析(1)

    说好的每天一个技术博客,选了iteye,但是,那个界面真的好丑啊,丑的让我都没写下去的欲望了,所以,还是转移到博客园里面吧,虽然这里也是很丑的! 直接步入正题: 1. 数据库锁包括:读锁(可共享锁)和 ...

  10. 使用git自动将子工程发布到百度开放云上

    我的项目中包含多个子工程,如web工程.python工程等.我在项目的根目录下建立了git管理,因此如果使用git push只能把整个项目推送上去,但我只想推送web工程目录.因此,编写了cmd脚本如 ...