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 ;
}

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. 国内首家MR头显公司于CES惊艳亮相

    在刚刚过去的CES2017大会上,我们看到了许多较为优秀的VR产品,而在这里面,有一家名不见经传的中国公司易瞳发布了一款兼具VR和AR功能的头显VMG-MARK.它的外观与联想VR和骁龙VR820等产 ...

  2. 移动web开发学习笔记一

    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0&quo ...

  3. JavaScript忍者秘籍——驯服线程和定时器

    1.定时器和线程 - 设置和清除定时器 JavaScript提供了两种方式,用于创建定时器以及两个相应的清除方法.这些方法都是window对象上的方法. 方法 格式 描述 setTimeout   i ...

  4. [河南省ACM省赛-第四届] 序号互换 (nyoj 303)

    相似与27进制的转换 #include<iostream> #include<cstdio> #include<cstring> #include<strin ...

  5. imx6平台qt锯齿原因分析

    gbl_alpha:全局alpha 就是设置整个界面的alpha值 loc_alpha 本地alpha 就是图片里面自带的alpha值 color_key color Key 要把界面透出来,就要设置 ...

  6. Zabbix 监控rabbitmq

    一 应用场景描述 测试环境业务 OpenStack中搭建了rabbitmq作为RPC消息队列,所以mq起到了很重要的角色,本案例是来如何使用zabbix监控 rabbitmq业务 二 Rabbitmq ...

  7. drupal中使用jquery&ajax

    不说了,直接上代码,看注释吧: <!--drupal中使用jquery的方法,有三个不同之处 --> <script> (function($){//1.$符号不放在最外边了 ...

  8. 基于jQuery弹性展开收缩菜单插件gooey.js

    首先 引入css <link rel="stylesheet" href="css/bootstrap.min.css"> <link rel ...

  9. 【.NET】转载:使用FileStream批量上传文件。

    先看效果图: 代码(没什么难度的一个案例,进度条为第三方控件ProgressODoom.dll,自己去网上下.) using System; using System.Collections.Gene ...

  10. LoadRunner参数值定义-摘自一米阳光

    深入解析LoadRunner下的参数化取值   熟悉LoadRunner的人,相信都会经常使用参数化功能,但是对于参数化的使用到底了解多少,就值得深思了.包括本人在内也是,每次在做压力测试的时候,基本 ...