hdu2058


The sum problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20449    Accepted Submission(s): 6006


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+........+2*m在将和开根号大于m,所以至少要有根号下2*m个元素(其实不搞不懂,为什么不是m个就可以了,因为其中每个不都是比1大吗,假设全都是等于1,那所有相加比就好了)不过个人感觉应该是利用了放大的思想吧。



给出AC代码:


#include<cstdio>
#include<cmath>
#include<algorithm>
 using namespace std; long long n,m,sum1; int main(){ while(scanf("%I64d%I64d",&n,&m)!=EOF){ if(n==0&&m==0)break; int a = (int)sqrt(2*m); for(int k = a;k > 0;k--){//一共有k个数字
            sum1 = m/k + (1 - k)/2; if((2*sum1+k-1) * k == 2 * m)
                 printf("[%I64d,%I64d]\n",sum1,sum1+k-1); }
        printf("\n"); } return 0; }



你可能感兴趣的:(hdu2058)