The sum problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9020 Accepted Submission(s): 2759
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]
Author
8600
Source
Recommend
linle
1 // Project name : 2058 ( The sum problem ) 2 // File name : main.cpp 3 // Author : Izumu 4 // Date & Time : Sun Jul 15 14:45:01 2012 5 6 7 #include <iostream> 8 #include <stdio.h> 9 #include <string> 10 #include <cmath> 11 #include <algorithm> 12 using namespace std; 13 14 int main() 15 { 16 int n, m; 17 while (cin >> n >> m && n + m) 18 { 19 int length = sqrt((double)2 * m) + 2; 20 21 while (length) 22 { 23 int a = m / length - (length - 1) / 2; 24 if ((a + a + length - 1) * length / 2 == m && a > 0 && (a + length - 1) <= n) 25 { 26 printf("[%d,%d]\n", a, a + length - 1); 27 } 28 length--; 29 } 30 cout << endl; 31 } 32 return 0; 33 } 34 35 // end 36 // ism