Binary Search - D

public
yeskendir.sultanov Mar 31, 2024 Never 64
Clone
C++ Dbinsearch.cpp 35 lines (28 loc) | 647 Bytes
1
#include <bits/stdc++.h>
2
#define ll long long
3
4
using namespace std;
5
6
ll n, x, y;
7
8
bool check(ll t) {
9
ll a = (t / x);
10
ll b = (t / y);
11
return (a + b >= n);
12
}
13
14
int main() {
15
std::ios_base::sync_with_stdio(false);
16
cin.tie(0);
17
cout.tie(0);
18
cin >> n >> x >> y;
19
20
ll low = 0, high = n * min(x, y), ans = high;
21
n--;
22
23
while (low <= high) {
24
ll mid = (low + high) / 2;
25
if (check(mid)) {
26
ans = mid;
27
high = mid - 1;
28
} else {
29
low = mid + 1;
30
}
31
}
32
33
cout << ans + min(x, y);
34
return 0;
35
}
36
37
38
39
40
41