HDU_2058——等差数列,子集,集合长度判断

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]
 1 /*
 2 设j为子串的长度,i为子串的第一个数 
 3 m=[j*(i+i+(j-1)*1)]/2
 4 2*m/j-j=i+i-1----2*i=2*m/j-j+1
 5 因为i+i-1>0
 6 所以2*m>j*j----j<sqrt(2m)
 7 */
 8 #include <cstdio>
 9 #include <cmath>
10 int main()
11 {
12    int n,m,i,j;
13    while(~scanf("%d%d",&n,&m),(n||m))
14       {
15          for(j=(int)sqrt(2*(double)m);j>=1;j--)//枚举长度 
16             {
17                i=(2*m/j-j+1)/2;//由长度获得的子串第一个元素值
18                if((j*(i+i+j-1))/2 == m)//等差数列前n项的和,还有通项公式
19                   printf("[%d,%d]\n",i,i+j-1);
20             }
21          printf("\n");
22       }
23    return 0;   
24 }

 

posted @ 2013-07-11 22:32  瓶哥  Views(263)  Comments(0Edit  收藏  举报