HDOJ 2058 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]
题目的意思:输入两个整数N,M。 N, M( 1 <= N, M <= 1000000000),如果在范围[1,M]内连续整数的和为N,按从小到大次序输出所有这样的连续段,当输入的M,N都为0时结束。
计算的思路:
不考虑子列的终点,而是考虑子列的起点和子列元素的个数,分别记为i,j。由等差数列求和公式,得(i+(i+j-1))*j/2==M ,即(2*i+j-1)*j/2==M(2式),故得i=(2*M/j-j+1)/2,将i,j代回2式,成立则[i,i+j-1]满足条件。注意j最小为1,而由2式,得(j+2*i)*j=2*M,而i>=1,故j*j<=(int)sqrt(2*M).
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int m = sc.nextInt();
if(m==0&&n==0){
return ;
}
int j =(int)Math.pow(2.0*m, 0.5);
for(j=j;j>0;j--){
int i;
i = (2*m/j-j+1)/2;
if(j*(j+2*i-1)/2==m){
System.out.println("["+i+","+(i+j-1)+"]");
}
}
System.out.println();
}
}
}
HDOJ 2058 The sum problem的更多相关文章
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- hdu 2058 The sum problem(简单因式分解,,)
Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...
- HDU 2058 The sum problem
传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...
- 题解报告:hdu 2058 The sum problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...
- HDU - 2058 The sum problem(思路题)
题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...
- HDU 2058 The sum problem (数学+暴力)
题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...
- hdu 2058 The sum problem(数学题)
一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...
- HDU 2058 The sum problem 数学题
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Web通信中的Get、Post方法
首先我们要了解Tomcat,Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选. ...
- android EventBus 的使用
今天简单的介绍 一下啊 android EventBus 的使用 EventBus 在官方介绍中是订阅......什么的 一大堆 , 在我android 菜鸟眼里 就是用来代替android 广 ...
- LaTeX 中插入数学公式
一.常用的数学符号 1.小写希腊字母 \alpha \nu \beta \xi \gamma o \delta \pi \epsilon \rho \zeta \sigma \eta \tau \th ...
- PE File.
Figure 1 - PE File The CLR header stores information to indicate that the PE file is a .NET executab ...
- oracle数据库读取操作系统的物理文件-转载,待完善
--源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...
- OC - 25.CAKeyframeAnimation
概述 简介 CAKeyframeAnimation又称关键帧动画 CAKeyframeAnimation是抽象类CAPropertyAnimation的子类,可以直接使用 通过values与path两 ...
- 解决Undefined symbols for architecture x86_64: 报错 和 ld: warning: ld: warning: ignoring file警告
出现这种错误的情况: 用iphone5模拟器编译程序正常, 用iphone5s以上的模拟器编译出现Undefined symbols for architecture x86_64: 报错 和 ld: ...
- 【POJ2185】【KMP + HASH】Milking Grid
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- cos-26上传个人案例
package cn.gdpe.upload; import java.io.File;import java.io.IOException;import java.util.Enumeration; ...
- 装了SVN,你的关联图标变了没有?
装了SVN,你的关联图标变了没有? 开始合作之后,装上了SVN,非常高效,我在VS写了一部分的代码,上传之后,别人通过下载或是更新,就更新到了合作同伴的VS里,相当于大家在一个VS里写代码.和保强他们 ...