bzoj 2986 Non-Squarefree Numbers 容斥原理+数学
题面
解法
显然可以二分答案
计算的时候用容斥原理即可
用莫比乌斯函数实现这个过程即可
代码
#include <bits/stdc++.h>
#define LL long long
#define N 1000010
using namespace std;
int f[N], p[N], mu[N];
void sieve(){
int len = 0, n = 1000000; mu[1] = 1;
for (int i = 2; i <= n; i++) {
if (!f[i]) p[++len] = i, mu[i] = -1;
for (int j = 1; j <= len && i * p[j] <= n; j++) {
f[i * p[j]] = true;
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
} else mu[i * p[j]] = -mu[i];
}
}
}
bool check(LL mid, LL k) {
LL ret = 0;
for (LL i = 1; i * i <= mid; i++)
ret += mu[i] * (mid / (i * i));
return mid - ret >= k;
}
int main() {
sieve();
LL k; cin >> k;
LL l = 0, r = 210000000000ll, ans;
while (l <= r) {
LL mid = (l + r) >> 1;
if (check(mid, k)) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << "\n";
return 0;
}