The Sum Problem HDU 2058
程序员文章站
2022-05-17 20:58:54
...
[My blog https://dyingdown.github.io/
The Sum Problem
Problem
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]
Analysis
Code
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m;
while(cin >> n >> m && m != 0 && n != 0){
for(int i = sqrt(2 * m); i > 0 ; i --){
if(2*m % i == 0){
if((2*m/i - (i-1)) % 2 == 0){
cout << "[" << (2*m/i - (i-1)) / 2 << ',' << (2*m/i - (i-1)) / 2 + i - 1 <<']' << endl;
}
}
}
cout << endl;
}
return 0;
}
上一篇: Java中对象和引用的理解
下一篇: Java中的日期处理工具DateTime
推荐阅读
-
(string高精度)A + B Problem II hdu1002
-
ACM1001:Sum Problem
-
[斐波那契幂和+二次剩余]2020hdu多校 6755 Fibonacci Sum
-
【线段树】【模板】讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值)
-
hdu 5371 Hotaru's problem (manacher)
-
HDU 6336 Problem E. Matrix from Arrays(找规律)
-
hdu 6336 Problem E. Matrix from Arrays
-
杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
-
HDU6336 Problem E. Matrix from Arrays
-
杭电多校04补题 HDU6336 Problem E. Matrix from Arrays【构造】