-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathF.cpp
97 lines (89 loc) · 1.44 KB
/
F.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
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
using namespace std;
// brute force approach
void solve()
{
long long r;
cin >> r;
long long ans = 0;
long long x = r;
for (long long i = 0; i <= r; i++)
{
long long dist = (i * i + x * x);
while (dist >= (r + 1) * (r + 1))
{
x--;
dist = i * i + x * x;
}
long long height = x;
while (i * i + height * height >= r * r && height >= 0)
{
height--;
ans++;
}
}
cout << ans * 4 - 4 << endl;
}
// binary search approach
void solve1()
{
long long r;
cin >> r;
int y = 0;
long long ans = 0;
for (long long i = 0; i <= r; i++)
{
long long d = (r + 1) * (r + 1) - i * i;
long long l = 0, h = r;
while (l <= h)
{
long long mid = l + (h - l) / 2;
if (mid * mid < d)
{
y = mid;
l = mid + 1;
}
else
{
h = mid - 1;
}
}
if (i)
ans += (2 * y + 1) * 2;
else
ans += (2 * y + 1);
}
for (long long i = 0; i < r; i++)
{
long long int d = (r) * (r)-i * i;
int l = 0, h = r - 1;
while (l <= h)
{
long long mid = l + (h - l) / 2;
if (mid * mid < d)
{
y = mid;
l = mid + 1;
}
else
{
h = mid - 1;
}
}
if (i)
ans -= (2 * y + 1) * 2;
else
ans -= (2 * y + 1);
}
cout << ans << endl;
}
int32_t main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}