forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10025.cpp
87 lines (83 loc) · 981 Bytes
/
10025.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
typedef long long Big;
Big K;
int GETI()
{
Big L, U, sum, M;
L = 1;
U = K / 2 + 1;
M = (L + U) / 2;
sum = (M * (M + 1)) / 2;
while (L != U && sum != K && L != U - 1)
{
if (sum > K)
{
U = M - 1;
}
else
{
L = M + 1;
}
M = (U + L) / 2;
sum = (M * (M + 1)) / 2;
}
return M;
}
void CAL()
{
Big I, i, sum;
I = GETI();
sum = (I * (I + 1)) / 2;
if (sum == K)
{
printf("%lld\n", I);
return;
}
for (i = I;; i++)
{
sum = ((i * (i + 1)) / 2) - K;
if (sum % 2 == 0)
{
printf("%lld\n", i);
return;
}
}
}
int main()
{
int kase;
scanf("%d", &kase);
while (kase--)
{
scanf("%lld", &K);
if (K < 0)
{
K = -K;
}
if (K == 0 || K == 2)
{
printf("3\n");
if (kase)
{
printf("\n");
}
continue;
}
else if (K == 1)
{
printf("1\n");
if (kase)
{
printf("\n");
}
continue;
}
CAL();
if (kase)
{
putchar('\n');
}
}
return 0;
}