-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEP44.cpp
More file actions
37 lines (34 loc) · 802 Bytes
/
EP44.cpp
File metadata and controls
37 lines (34 loc) · 802 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
#include <iostream>
using namespace std;
typedef long long ll;
ll Pentagonal(ll n) {
return n * (n * 3 - 1) >> 1;
}
bool binary_search (ll n, ll x) {
int head = 1, tail = n, mid;
while (head <= tail) {
mid = (head + tail) >> 1;
if (Pentagonal(mid) == x) return true;
else if(Pentagonal(mid) < x) head = mid + 1;
else tail = mid - 1;
}
return false;
}
int main() {
ll min = INT_MAX;
ll n = 2, m, p1, p2;
while (Pentagonal(n) - Pentagonal(n - 1) < min) {
p1 = Pentagonal(n);
for (int m = n - 1; m >= 1; m--) {
p2 = Pentagonal(m);
if (p1 - p2 >= min) break;
int flag = 1;
flag = binary_search(2 * n, p1 + p2);
flag = flag && binary_search(n, p1 - p2);
if (flag) min = p1 - p2;
}
n++;
}
cout << min << endl;
return 0;
}