您的位置:首页 > 其它

HDU 2058 The sum problem

2016-01-28 16:39 302 查看
The sum problem

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 20506 Accepted Submission(s): 6023

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]

Author

8600

Source

校庆杯Warm Up

等差求和公式:

Sn=(a1+an)*n/2

=(a1+a1+d(n-1))*n/2

=a1*n+d(n-1)*n/2;

因为此处公差d=1,所以Sn=a1*n+(n-1)*n/2,从第一项开始算起时(本题a1=1时),可化简为Sn=n+(n-1)*n/2=(n+1)n/2;

m=Sn,n为项的个数,则len<=n(max)=sqrt(Sn*2)=sqrt(m*2);

m=Sn =a1*n+(n-1)n/2可得a1*n=m-(n-1)n/2;

如果得到的a1*n这个数能否被len整除,则Sn=m

#include <iostream>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
int main()
{
long long n,m;
while(cin>>n>>m&&n&&m){
int len=(int)sqrt(m*2.0);
int l=0;//a1和len的乘积
for(;len>0;len--){
l=m-(len-1)*len/2;//a1*n=m-(n+1)*n/2
if(l%len==0)
cout<<"["<<l/len<<","<<l/len+len-1<<"]"<<endl;
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: