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].
 
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m,n||m)
{
int t=sqrt(*m);
while(t)
{
int ans=m/t-(t-)/;
if(ans*t+(t*(t-)/)==m)
printf("[%d,%d]\n",ans,ans+t-);
t--;
}
printf("\n");
}
return ;
}

最新文章

  1. alter system switch logfile与alter system archive log current的区别
  2. easyui 中datagrid 点击行的事件
  3. C 和 Object- C 中得 #ifdef 和#ifndef
  4. JFrame中setDefaultCloseOperation的参数含义
  5. 如何将Log4Net 日志保存到mongodb数据库之实践
  6. CXF之jaxws:endpoint对spring bean的引用
  7. swift获取图片像素颜色值
  8. ognl.NoSuchPropertyException(没有对应属性异常)
  9. java可访问修饰符
  10. IDEA + Maven + JavaWeb项目搭建
  11. Percona XtraBackup的部分备份与恢复/单库备份/单表备份/指定库备份/指定表备份
  12. Mina框架(实战详解)
  13. 从零开始一起学习SLAM | 掌握g2o顶点编程套路
  14. LaTex 使用特殊章节符号 (&#167;)
  15. Redis广播
  16. emmet-前端开发神器的几种写法
  17. 004_浅析Python的GIL和线程安全
  18. Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization(第一周)深度学习的实践层面 (Practical aspects of Deep Learning)
  19. HTML 实例学习(基础)
  20. ASP 错误捕捉,处理

热门文章

  1. 替换ubuntu 14.04的源
  2. 一步步优化JVM四:决定Java堆的大小以及内存占用
  3. CentOS6.5部署L2TP over IPSec
  4. Linux的一些简单命令(四)-用户和组账户管理
  5. 使用pabot并发执行robotframework的testSuite
  6. 【3】Chrome 的一些常用操作
  7. CoreAnimation的使用
  8. 简单的jquery实现tab切换
  9. git 客户端提交
  10. VS2015转VS2008