HDU 2058 the sum problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.