The sum problem
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 17697 Accepted Submission (s): 5275
Problem Descriptiongiven a sequence,...... N, your job is to calculate all the possible sub-sequences the sum of the sub-sequence is M.
Inputinput contains multiple test cases. Each case contains the integers n, m (1 <= N, M <= 1000000000). Input ends with N = m = 0.
Outputfor each test case, print all the possible sub-sequence, it sum is m.the format was show in the sample Below.pri NT a blank line after each test case.
Sample INPUT20 1050 300 0
Sample output[1,4][10,10][4,8][6,9][9,11][30,30]
#include <stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;#defineMAXN 1000000__int64 n,m;intMain () { while(SCANF ("%i64d%i64d", &n,&m)!=eof&&n&&M) { for(__int64 k= (int) sqrt (2*M); k>=1; k--) {__int64 A1=m/k-(K-1)/2; if((2*a1+k-1) *k==2*M) {printf ("[%i64d,%i64d]\n", a1,a1+k-1); }} printf ("\ n"); } return 0;}//based on arithmetic progression summation, m= (2*a1+k-1) *k/2,k represents the number of items in the sequence, and A1 represents the first item. //Enumeration K (1<=K<=SQTRT (m))
hdu2058 The sum problem (enumeration ~ ~ Arithmetic progression summation formula)