The sum problem
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 20505 Accepted Submission (s): 6023
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] parsing: The direct enumeration will time out, so the enumeration should be scaled down. According to the linear summation formula SN = n*a1+n* (n-1)/2*d, where sn = m,d = 1, then M = n*a1+n* (n-1)/2. Finishing 2*m = n*n+ (2*a1-1) *n. Available: N<sqrt (2.0*m).
1#include <cstdio>2#include <cmath>3 4 intn,m;5 6 intMain ()7 {8 while(SCANF ("%d%d",&n,&m), N) {9 for(intnum = (int) sqrt (m*2.0); Num>0; --num) {Ten intNUMA1 = m-num* (num-1)/2; One if(Numa1%num = =0) Aprintf"[%d,%d]\n", numa1/num,numa1/num+num-1); - } -printf"\ n"); the } - return 0; -}
HDU 2058 the sum problem