-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP10025.cpp
More file actions
41 lines (37 loc) · 793 Bytes
/
P10025.cpp
File metadata and controls
41 lines (37 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>
using namespace std;
int sumDigitsToN(int n) {
return n*(n+1)/2;
}
int main() {
int cases, k;
cin >> cases;
for(int cas = 0; cas < cases; ++cas) {
if(cas != 0)
cout << endl;
cin >> k;
if(k < 0)
k = -k;
// find first n so: 1 + 2 + 3 + ... n = n(n+1)/2 = m >= k.
int n = (int)sqrt(k);
int m;
while((m = sumDigitsToN(n)) < k || n == 0)
++n;
if(k == m) {
cout << n << endl; // done :)
}
else {
if((m-k) % 2 == 0) {
cout << n << endl; // even diff => ok.
}
else { // make up an uneven difference
if(n % 2 == 0)
cout << n+1 << endl; // Need the next uneven number.
else
cout << n+2 << endl; // Need the next uneven number.
}
}
}
return 0;
}