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

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #define ll long long
 5 using namespace std;
 6 int main()
 7 {
 8     long long n,m;
 9     while(cin>>n>>m&&n&&m){
10         int len=(int)sqrt(m*2.0);
11         int l=0;//a1和len的乘积
12         for(;len>0;len--){
13             l=m-(len-1)*len/2;//a1*n=m-(n+1)*n/2
14             if(l%len==0) 
15                 cout<<"["<<l/len<<","<<l/len+len-1<<"]"<<endl;
16         }
17         cout<<endl;
18     }
19     return 0;
20 }

 

posted on 2016-01-28 16:39  Sunny糖果  阅读(145)  评论(0编辑  收藏  举报