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. hdu5296(2015多校1)--Annoying problem(lca+一个公式)

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. MyEclipse的html/JSP编辑器添加代码自动提示

    http://lusterfly.iteye.com/blog/1872627 在myeclipse 9以前的版本中,我们如果要为html编辑器添加自动的代码提示可以这样操作: windows--&g ...

  3. poj3181 Dollar Dayz

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...

  4. tree related problems (update continuously)

    leetcode Binary Tree Level Order Traversal 这道题是要进行二叉树的层次遍历.对于层次遍历,最简单直观的办法就是进行BFS.于是我们仅仅须要维护一个队列就能够了 ...

  5. HDU5363:Key Set

    Problem Description soda has a set S with n integers {1,2,-,n}. A set is called key set if the sum o ...

  6. 【BZOJ4956】lydsy七月月赛 I 乱搞

    [BZOJ4956]lydsy七月月赛 I 题面 题解:傻题,Floyd传递闭包即可~ #include <cstdio> #include <cstring> #includ ...

  7. Java单元测试(Junit+Mock+代码覆盖率)---------转

    Java单元测试(Junit+Mock+代码覆盖率) 原文见此处 单元测试是编写测试代码,用来检测特定的.明确的.细颗粒的功能.单元测试并不一定保证程序功能是正确的,更不保证整体业务是准备的. 单元测 ...

  8. Linux就该这么学--命令集合4(文件目录管理命令)

    1.touch命令用于创建空白文件与修改文件时间:(touch [选项] [文件]) 对于在Linux中的文件有三种时间: 更改时间(mtime):内容修改时间(不包括权限的) 更改权限(ctime) ...

  9. 微信小程序开发:学习笔记[9]——本地数据缓存

    微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...

  10. linux 网络设备,网卡配置 ,相关

    网络设备,网卡配置: Eth0是物理网卡:唯一mac地址,Bcast:广播地址,MAsk:子网掩码, Lo:系统自带的回环的ip地址,可以做一些基本的测试应用,比如没有网卡就用127.0.0.1, r ...