The sum problem

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

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]

/*import java.util.*;
class Main{
public static void main(String args[])
{Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{int n=cin.nextInt();
int m=cin.nextInt();
if(n==0&&m==0)
break;
for(int i=1;i<=n;i++)
{int sum=i;
int k=0,s=0,flag=0;
for(int j=i+1;j<=n;j++)
{ sum+=j;
if(sum==m)
{flag=1;
k=i;
s=j;
break;}
else if(sum>m)
{break;
}
}
if(flag==1)
System.out.println("["+k+","+s+"]");
}
if(n>=m)
System.out.println("["+m+","+m+"]");
System.out.println();
}
}
}*/

上面的那个代码会超时,下面的代码不会超时,这是一个数学题,a+(i*(i+1)/2)=m,当a最小的时候是等于1,所以1+(i*(i+1)/2)<=m;

所以i<=sqrt(2*m);因为这是一个等差数列,d为1;
import java.util.*;
class Main{
public static void main(String args[])
{Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{long n=cin.nextInt();
long m=cin.nextInt();
if(n==0&&m==0)
break;
long d=0;
for(int i=(int)Math.sqrt(2*m);i>0;i--)
{
d=m-(i+i*i)/2;
if(d%i==0)
System.out.println("["+(d/i+1)+","+(d/i+i)+"]");
}

System.out.println();
}
}
}

hdu2058java的更多相关文章

随机推荐

  1. client denied by server configuration

    http://blog.csdn.net/fdipzone/article/details/40512229

  2. 【poj3734】矩阵乘法求解

    [题意] 给N个方块排成一列.现在要用红.蓝.绿.黄四种颜色的油漆给这些方块染色.求染成红色方块和染成绿色方块的个数同时为偶数的染色方案的个数,输出对10007取余后的答案.(1<=n<= ...

  3. Cxf + Spring3.0 入门开发WebService

    转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...

  4. 循环冗余校验(CRC)算法入门引导

    目录 写给嵌入式程序员的循环冗余校验CRC算法入门引导 前言 从奇偶校验说起 累加和校验 初识 CRC 算法 CRC算法的编程实现 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌 ...

  5. 【Xamarin开发 Android 系列 2】VS2015跨平台开发的几种方式

    原文:[Xamarin开发 Android 系列 2]VS2015跨平台开发的几种方式 在微软Build大会上,微软宣布在VS2015中支持三种方式进行跨平台的开发. 1. Xamarin 2. Co ...

  6. JDBC事务控制管理

    1.事务 (1)事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句 update account set mone ...

  7. linux权限掩码

    我的博客:www.while0.com 主要是在新建文件或目录的时候,控制新文件或目录的默认权限. 文件:新建文件默认没有x权限,故新建文件在umask为000时最大权限是666. 目录:新建目录默认 ...

  8. bzoj2705

    一个常用的结论(方法) 只要知道gcd(i,n)=L 的i的个数s,我们就能很轻易得出答案 gcd(i,n)=L gcd(i/L,n/L)=1 不难得到这样的s=与n/L互质的个数=phi(n/L) ...

  9. 【转】Android自定义控件

    原文网址:http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理 ...

  10. 计数方法,博弈论(扫描线,树形SG):HDU 5299 Circles Game

    There are n circles on a infinitely large table.With every two circle, either one contains another o ...