hdu2058 The sum problem (enumeration ~ ~ Arithmetic progression summation formula)

Source: Internet
Author: User

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)

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.