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. ALV 行列 颜色

    1)颜色含义 1:海蓝:2:浅清:3:黄色:4:浅蓝:5:青色:6:红色:7:橙色.(1)首位为主颜色:(2)次位为辅助颜色:(3)末位为0时,表示首位数字表为表格的底色:末位为1时,则表示以1为底色 ...

  2. 【HDU 5381】 The sum of gcd (子区间的xx和,离线)

    [题目] The sum of gcd Problem Description You have an array A,the length of A is nLet f(l,r)=∑ri=l∑rj= ...

  3. 如何取消Linux下,vi中显示的^M符号

    http://www.cnblogs.com/dkblog/archive/2012/02/03/2337187.html dos2unix file_name bash: ./configure: ...

  4. 单片机 C 语言模块化编程

    好的开始是成功的一半 通过上一章的学习,我想你已经掌握了如何在程序中释放CPU了.希望能够继续坚持下去.一个良好的开始是成功的一半.我们今天所做的一切都是为了在单片机编程上做的更好. 在谈论今天的主题 ...

  5. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  6. linux字符图形界面

    /etc/inittab 1)  字符界面标识: id:3:initdefault: 2)  图形界面标识: id:5:initdefault:   [root@ora9i ~]# vi /etc/i ...

  7. VM Depot 助您使用本地开源软件架设开发 Web 站点

     发布于 2014-04-25 作者 云 浪生 使用 VM Depot 中的镜像在 Azure 上创建.开发.部署网站与应用不仅方便快捷而且省时省力!感谢开源社区的大力支持,我们的VM Depot ...

  8. (转载)查看三种MySQL字符集的方法

    (转载)http://database.51cto.com/art/201010/229171.htm MySQL字符集多种多样,下面为您列举了其中三种最常见的MySQL字符集查看方法,该方法供您参考 ...

  9. Bzoj 1982: [Spoj 2021]Moving Pebbles 博弈论

    1982: [Spoj 2021]Moving Pebbles Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 130  Solved: 88[Submi ...

  10. (转载)利用burp suite截断上传拿shell

    burpsuite上传必须要有filepath这个参数 第一步:选择一个jpg后缀的马. 第二步:设置本地代理,burp的本地端口是8080 第三步:打开burp suite 按图操作就ok了. 第四 ...