HDU 2058 The sum 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 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的更多相关文章
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- 题解报告:hdu 2058 The sum problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...
- hdu 2058 The sum problem(简单因式分解,,)
Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...
- HDU - 2058 The sum problem(思路题)
题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...
- HDU 2058 The sum problem (数学+暴力)
题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...
- hdu 2058 The sum problem(数学题)
一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...
- HDU 2058 The sum problem 数学题
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
- HDOJ 2058 The sum problem
Problem Description Given a sequence 1,2,3,--N, your job is to calculate all the possible sub-sequen ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- ubuntu-16.4TLS安装QQ
01下载Winqq 下载地址:http://www.ubuntukylin.com/application/show.php?lang=cn&id=27902解压zip unzip wine- ...
- 1104关于优化mysql服务器几个参数和思路
转自http://www.cnblogs.com/AloneSword/p/3207697.html 按照从大到小,从主要到次要的形式,分析 mysql 性能优化点,达到最终优化的效果. 利用 min ...
- 通过ADO方式连接数据库
首先你正确安装了MySQL的数据库驱动程序(mysql-connector-odbc-5.1.6-win32.msi )或者64位的 本地连接: 1.在桌面上新建一个空的文本文件mysql.txt,重 ...
- ASP.NET 返回字符串 IE6乱码问题
项目A,所有的文件编码和内容编码都是UTF-8. 项目B,Index.aspx文件编码和页面内容编码都是GB2312. 项目A返回JSON格式数据给项目B时,其它浏览器都可以就是IE不行.后来在网上找 ...
- php csv导出
/** * 下载csv * @param unknown $orders_id * @param unknown $orders_date_start * @param unknown $orders ...
- Android性能优化文章转载
今天看到几篇比较好的文章就转了!(链接如下) 转载注明出处:Sunzxyong Android性能优化之Bitmap的内存优化 Android性能优化之常见的内存泄漏 Android最佳实践之Syst ...
- oracle中SET DEFINE意思
et define off关闭替代变量功能 在SQL*Plus中默认的"&"表示替代变量,也就是说,只要在命令中出现该符 号,SQL*Plus就会要你输入替代值.这就意味着 ...
- 三、基础功能模块,用户类别管理——锁、EF并发处理、领域服务、应用服务的划分
在上一章节中,我们处理了MVC多级目录问题,参见<二.处理MVC多级目录问题——以ABP为基础架构的一个中等规模的OA开发日志>.从这章开始,我们将进入正式的开发过程.首先,我们要完成系统 ...
- mysql数据库默认存放位置修改
windows: 方式一 使用符号连接 假设你的mysql安装在c:\mysql,数据目录就是c:\mysql\data 现在你想在D 盘建立一个名为foo的数据库,路径为d:\data\foo. ...
- ArrayList实现线程的几种方法
第一种.给方法名加synchronized Public synchronized void method(){ //-. } 第二种 New synchronized arraylist(); 第三 ...