Let the number of integers in , which are not divisible by and . It can be calculated using the idea of inclusion and exclusion principle.
Now, one can see, that holds, and thus, binary search on answer can be applied. I.e. it is required to find minimum value of , that .
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
long long f(long long x) {
return x - (x / 3 + x / 5 - x / 15);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
long long l = 1, r = 1e18;
while (l < r) {
long long m = (l + r) / 2;
if (f(m) >= n) {
r = m;
} else {
l = m + 1;
}
}
cout << l << "\n";
}