The sum problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 31453    Accepted Submission(s): 9414

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]

思路

求公差为1的等差数列,在哪些区间内的和为m

对等差数列的前n项和进行化简:

可化为:

,可得:

然后对于每一项i判断a和b是不是整数

AC代码

/*
* @Author: WZY
* @School: HPU
* @Date: 2018-10-19 20:21:57
* @Last Modified by: WZY
* @Last Modified time: 2018-10-19 20:39:18
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"---------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int main(int argc, char const *argv[])
{
// ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
double _begin_time = clock();
#endif
int n,m;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
for(int i=sqrt(2*m);i>0;i--)
{
if(2*m%i==0&&(2*m/i-1+i)%2==0)
{
int b=(i-1+2*m/i)/2;
int a=2*m/i-b;
printf("[%d,%d]\n",min(a,b),max(a,b));
}
}
printf("\n");
}
#ifndef ONLINE_JUDGE
double _end_time = clock();
printf("time = %lf ms.", _end_time - _begin_time);
#endif
return 0;
}

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 (数学+暴力)

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

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

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

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

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

  5. HDU 2058 The sum problem

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

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

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

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

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

  8. HDU 2058 The sum problem 数学题

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

  9. HDOJ 2058 The sum problem

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

  10. HDU 5373 The shortest problem (数学)

    题意:给定两个数的n和m,有一种操作,把 n 的各位数字加起来放到 n后面形成一个新数n,问重复 m 次所得的数能否整除 11. 析:这个题首先要知道一个规律奇数位的和减去偶数位的和能被11整除的数字 ...

随机推荐

  1. rtp rtcp rtsp sip协议了解方法

    vlc有个串流功能 可以流各种推流 方便拉取. 能够方便抓包查看协议. 1.菜单媒体->流 打开对话框 分别可以选择本地文件 关盘文件 网络流 捕获设备做流化处理. 2. 选择 串流->新 ...

  2. coursera国际法笔记 持续更新

    LECTURE ONE International crime court(ICC) came into being after the Second World War. The Nuremberg ...

  3. linux下iostat命令详解

    iostat用于输出CPU和磁盘I/O相关的统计信息 iostat语法 用法:iostat [ 选项 ] [ <时间间隔> [ <次数> ]] 常用选项说明: -c:只显示系统 ...

  4. jquery ready&&load用法

    ready和load那一个先执行 DOM文档加载的步骤 (1) 解析HTML结构 (2) 加载外部脚本和样式表文件 (3) 解析并执行脚本代码 (4) 构造HTML DOM模型 //ready (5) ...

  5. ECMAscript5中的map

    今天看到到这样一个问题: ["1", "2", "3"].map(parseInt) 执行结果是什么? 结果是[1,NAN,NAN],很出乎 ...

  6. Win10系列:VC++媒体播放控制4

    (7)音量控制 MediaElement控件具有一个Volume属性,通过设置此属性的值可以改变视频音量的大小.接下来介绍如何实现视频的音量控制,首先打开MainPage.xaml文件,并在Grid元 ...

  7. learning scala write to file

    scala 写文件功能: scala> import java.io.PrintWriterimport java.io.PrintWriter scala> val outputFile ...

  8. Linux防火墙iptables的策略

    iptables策略 iptables -L #查看现有防火墙所有策略 iptables -F #清除现有防火墙策略 只允许特定流量通过,禁用其他流量 1.允许SSH流量(重要) iptables - ...

  9. c/c++动态内存分配的区别

    c中动态内存分配使用malloc和free. malloc指定需要分配的内存大小,分配成功则返回指向该内存的指针,不成功则返回空指针.返回的指针类型为void *,表示不确定指针所指内存存放的数据类型 ...

  10. Java基础-类和对象

    类和对象 对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴.叫.吃等. 类:类是一个模板,它描述一类对象的行为和状态. ...