The sum problem

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]

 
Sample Input
本来想前缀和搞搞的,一看1 <= N, M <= 1000000000,看来是公式题,给出一个M,不可能从1枚举到1000000000,所以要找一个枚举范围,等差数列求和公式:sum=n*a1+n*(n-1)/2,n要最大,就要a1=1,所以sum=n*(n+1)/2,变一下n<sqrt(2*sum),所以取n=sqrt(2*sum),接下来n从大到小枚举,用公式变形:a1=sum/n-(n-1)/2求出a1,求和如果等于M,输出[a1,a1+n-1].
 
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <cstring>
  6. #include <stack>
  7. #include <queue>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <map>
  11. #define PI acos(-1.0)
  12. #define ms(a) memset(a,0,sizeof(a))
  13. #define msp memset(mp,0,sizeof(mp))
  14. #define msv memset(vis,0,sizeof(vis))
  15. using namespace std;
  16. //#define LOCAL
  17. int main()
  18. {
  19. #ifdef LOCAL
  20. freopen("in.txt", "r", stdin);
  21. //freopen("out.txt","w",stdout);
  22. #endif // LOCAL
  23. ios::sync_with_stdio(false);
  24. int n,m;
  25. while(cin>>n>>m,n||m)
  26. {
  27. int t=sqrt(*m);
  28. while(t)
  29. {
  30. int ans=m/t-(t-)/;
  31. if(ans*t+(t*(t-)/)==m)
  32. printf("[%d,%d]\n",ans,ans+t-);
  33. t--;
  34. }
  35. printf("\n");
  36. }
  37. return ;
  38. }

HDU 2058 The sum problem(枚举)的更多相关文章

  1. HDU 2058 The sum problem

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

  2. 题解报告:hdu 2058 The sum problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...

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

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

  4. HDU 2058 The sum problem 数学题

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

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

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

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

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

  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 4143 A Simple Problem(枚举)

    题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了, ...

随机推荐

  1. java--调整JVM内存的大小

    默认占用:64M的内存 修改内存的方式: 1.某一类,右键选择--Run Configurations 2.选择--Arguments 3.在VM argments中输入内容,如:-Xmx80m

  2. iOS 发布项目到CocoaPods其实没那么复杂😆

    首先大家必须要了解一下CocoaPods (如果你连CocoaPods是啥都不知道可以不用往下看了

  3. Python中fileinput模块使用

    fileinput模块可以对一个或多个文件中的内容进行迭代.遍历等操作.该模块的input()函数有点类似文件 readlines()方法,区别在于前者是一个迭代对象,需要用for循环迭代,后者是一次 ...

  4. C# 语言规范_版本5.0 (第9章 命名空间)

    1. 命名空间 C# 程序是利用命名空间组织起来的.命名空间既用作程序的“内部”组织系统,也用作“外部”组织系统(一种向其他程序公开自己拥有的程序元素的方法). using 指令(第 9.4 节)用来 ...

  5. sqlserver 操作xml

    1.xml.exist    输入为XQuery表达式,返回0,1或是Null.0表示不存在,1表示存在,Null表示输入为空 2.xml.value    输入为XQuery表达式,返回一个SQL ...

  6. 顶层const和底层const

    As we’ve seen, a pointer is an object that can point to a different object. As a result,we can talk ...

  7. 在GNU/Linux下使用命令行自动挂载与卸载USB磁盘

    在命令行环境下如果每次都是靠手动敲入mount与umount命令来挂载与卸载USB磁盘是件很麻烦的事情.尤其是mount命令的参数非常多.比如,磁盘的分区类型(vfat.ntfs等),挂载的目录节点, ...

  8. Java笔试题目-my

    1.BuildString  和 BefferedString  默认初始容量为 16 ,  超出就是 16*2 + 2;   可以看源代码得知; 2.ArrayList 默认的构造方法是一个空的Ob ...

  9. 利用requestjs优化响应式移动端js加载

    html: <script data-main="main" src="require.js"></script> main.js re ...

  10. [Jmeter]jmeter之参数化

    一.同一个服务器不同界面访问 a 准备工作: 1.启动jmeter: 2.创建需要访问的url文件,内容示例如下: 即比如:http://www.cnblogs.com/amberly/p/59651 ...