题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058

问题描述

  给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总和为M.

输入

  输入包含多个测试用例。 每个情况包含两个整数N,M(1 <= N,M <= 1000000000)。输入以N = M = 0结束。

输出

   对于每个测试用例,打印其总和为M的所有可能的子序列。格式显示在下面的示例中。在每个测试用例后打印一个空行。

示例输入

20 10

50 30

0 0

示例输出

[1,4]

[10,10]

[4,8]

[6,9]

[9,11]

[30,30]

解题思路:这道题要转换一下角度来思考,否则会一直WA~_~。。

不妨假设i为区间左值,j为区间元素个数(j至少为1),则区间为[i,i+j-1]。

若这个区间合法,那么由等差数列求和公式,得(i+(i+j-1))*j/2==M(1式),即(2*i+j-1)*j/2==M(2式),故得i=(2*M/j-j+1)/2,

将i,j代回2式,若1式成立则[i,i+j-1]满足条件。注意j最小为1,(区间右值大于或等于区间左值),而由2式,得(j+2*i)*j=2*M,

由题目条件得i>=1,则j*j<=2*M,即j<=(int)sqrt(2*M)。综上,遍历区间长度j即可。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
int main()
{
int N,M,i,j;//数学规律,等差数列
while(cin>>N>>M && (N+M)){
for(j=(int)sqrt(*M);j>;j--){
i=(*M/j-j+)/;//推导出来的公式
if(j*(*i+j-)/==M)cout<<'['<<i<<','<<i+j-<<']'<<endl;//把i,j分别代入推导出来的表达式看是否相等
}
cout<<endl;//每个测试后面空出一行
}
return ;
}

题解报告:hdu 2058 The sum problem的更多相关文章

  1. HDU 2058 The sum problem(枚举)

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

  2. hdu 2058 The sum problem(简单因式分解,,)

    Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...

  3. HDU 2058 The sum problem

    传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...

  4. HDU 2058 The sum problem (数学+暴力)

    题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...

  5. HDU 2058 The sum problem 数学题

    解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...

  6. HDU - 2058 The sum problem(思路题)

    题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...

  7. hdu 2058 The sum problem(数学题)

    一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...

  8. HDOJ 2058 The sum problem

    Problem Description Given a sequence 1,2,3,--N, your job is to calculate all the possible sub-sequen ...

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

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

随机推荐

  1. 利用Druid实现应用和SQL监控

    一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可 ...

  2. [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through

    Understanding sources and subscribers makes it much easier to understand what's going on with mergeM ...

  3. vue :src 文件路径错误

    首先先说明下vue-cli的assets和static的两个文件的区别,因为这对你理解后面的解决办法会有所帮助 assets:在项目编译的过程中会被webpack处理解析为模块依赖,只支持相对路径的形 ...

  4. MVC Web Api 发布到Azure报错

    I fixed this by reinstalling the NuGet package, which corrects broken dependencies. From the package ...

  5. JSONArray ja = JSONArray.fromObject(list);//特殊类 用于将list转化为JSON 数据并返回 out.print(ja);

    JSONArray ja = JSONArray.fromObject(list);//特殊类 用于将list转化为JSON 数据并返回out.print(ja);

  6. 超线程技术——超线程技术让(P4)处理器增加5%的裸晶面积,就可以换来15%~30%的效能提升,本质单核模拟双核!和异步编程的思想无异。

    超线程是Intel 所研发的一种技术,于2002年发布.超线程的英文是HT技术,全名为Hyper-Threading,中文又名超线程.超线程技术原先只应用于Intel Xeon处理器中,当时称为Sup ...

  7. vue 使用过程中自己遇到的bug

    需要安装npm git(windows系统需要安装) npm 是node的包管理工具 npm 国内的网站比较慢,推荐使用cnpm(淘宝的镜像) cnpm(npm) install 创建依赖-----因 ...

  8. 【position】

    background-position 定位的position的区别 background-position 相对的位置是(容器的宽度-子元素的宽度) 当子元素的宽度大于容器的宽度 backgroun ...

  9. Overview of MIDI

    东拼西凑的介绍 MIDI which means Musical Instrument Digital Interface, introduced in 1980's provided a inter ...

  10. 使用git管理远程仓库

    1.从现有仓库克隆 git clone git://github.com/schacon/grit.git 2.检查当前文件状态 git status 3.跟踪新文件 git add XXX 4.忽略 ...