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. bzoj 2806: [Ctsc2012]Cheat 后缀自动机DP

    2806: [Ctsc2012]Cheat Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 583  Solved: 330[Submit][Statu ...

  2. Linux Screen命令使用

    参考URL: http://jingyan.baidu.com/article/295430f128d8ea0c7e005089.html ~~~~~~~~~~~~~~~~~~~~~~~~ 其它的不提 ...

  3. HTML标签与表格

    1.打开DREAMWEAVER,新建HTML,如下图: 2.body的属性: bgcolor 页面背景色 background    背景壁纸.图片 text  文字颜色 topmargin   上边 ...

  4. ASP.NET MVC 下 引用阿里巴巴和IconFont字体路径404问题

    参考:http://stackoverflow.com/questions/28169365/font-wont-get-found-on-server-for-firefox# http://blo ...

  5. jni相关

    封装 jni 的 java 层 Integer.Long 对象使用时必须用 new 对象的形式,防止修改 128>x x>-128 之间缓存的对象,一定要谨记 配置 在 eclipse 环 ...

  6. hud1520Anniversary party(树形DP)

    链接 第一道树形DP 根据左儿子 右兄弟 将多叉树转化成二叉树 结构体里保存取这个节点和不取这个节点的最大值 #include <iostream> #include<cstdio& ...

  7. 求奇数偶数的和,,利用while循环

    static void Main(string[] args)        {             while (true)                {             try   ...

  8. Android Weekly Notes Issue #238

    Android Weekly Issue #238 January 1st, 2017 Android Weekly Issue #238 本期内容包括: Firebase发送Notification ...

  9. 设置SharePoint2010列表的项目级权限

    转:http://www.cfanz.cn/?c=article&a=read&id=24096 在SharePoint2010中我们经常会用到这样的权限设置,在一个列表中可以存储多个 ...

  10. java 枚举使用详解

    在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的. 例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素组成了四季的“数据集” ...