The sum problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21744    Accepted Submission(s): 6391

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]
 

本题题意:

输入两个数n,m,n代表数列为从1~n的等比为1的等比数列,求其中连续哪几个数的和为m,并输出。

运用等差数列的求和公式就能很好的解决这个问题。下面重申一下等差数列的求和公式:

附AC代码:

 #include<iostream>
#include<cstdio>
#include<cmath>//包含开根函数sqrt() using namespace std; int main(){
int n,m;
while(~scanf("%d %d",&n,&m)&&m||n){
for(int i=sqrt(*m);i>=;i--){//1连加到sqrt(2*m) > m
int a=(m-(i*(i-))/)/i;//等差求和公式推出
if(m==a*i+(i*(i-))/)
printf("[%d,%d]\n",a,a+i-);
}
printf("\n");//注意每组数据空一行
}
return ;
}

HDOJ-2058的更多相关文章

  1. HDOJ 2058 The sum problem

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

  2. hdoj:2058

      #include <iostream> #include <cmath> #include <vector> using namespace std; stru ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. bzoj 2058+2059+2060 Usaco2010 Nov

    三道金组比较容易的题目.. 2058 首先交换次数就是逆序对数,因为只能交换相邻的两数 先对原序列找逆序对数 用树状数组nlogn求出 然后O(n)依次求出其循环序列的逆序对数 比如 3 5 4 2 ...

  7. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  9. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  10. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

随机推荐

  1. C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密

    C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩   这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http:// ...

  2. vim调试

    首先,想调试一个程序的话,输入以下命令: guest-djjtew@ubuntu:~$ python3 -m pdb 1.py 这时候就停止了,等待着你的输入,然后输入"l"的话, ...

  3. OpenCV load 运行出错 cv::Exception 出错

    今天在使用OpenCV的load函数时,出现错误,代码如下: cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); ...

  4. $modal 参数 以及 使用方法

    $modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示ht ...

  5. Coder-Strike 2014 - Round 2

    t题目链接:Coder-Strike 2014 - Round 2 A题:简单水题,注意能加入反复的数字.因此仅仅要推断是否能把Min和Max加入好.就能够了 B题:开一个sum计算每一个聊天总和,和 ...

  6. linux 块设备驱动(二)——块设备数据结构

    本文来源于: 1. http://www.cnblogs.com/dyllove98/archive/2013/07/01/3165567.html 块设备相关的数据结构以及接口: 块设备接口则相对复 ...

  7. 阿里云nginx+thinkphp环境运行会直接下载php文件的问题。

    测试了好久,在网上查了各种nginx.conf的虚拟机配置都没用 其实最简单的方法就是在php.ini中找到 #cgi.fix_pathinfo=1; 去掉前面的#,如果值不是1则改为1

  8. nginx的源代码分析--间接回调机制的使用和类比

    nginx使用了间接回调机制.结合upstream机制的使用来说明一下,首先明白几个事实: 1)事实上ngxin和下游client的连接使用的是ngx_http_connection_t,每一个连接相 ...

  9. c# 怎么获取自己的IP地址

    1.aspx页面,asp.net项目的页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehin ...

  10. Ubuntu搜狗输入法候选词乱码

    打开Fcitx Configuration,进入Addon标签 选中Simplified Chinese To Traditional Chinese       Convert Simplified ...