bzoj 2986 Non-Squarefree Numbers 容斥原理+數學
阿新 • • 發佈:2018-08-14
代碼 bre define com amp check href break www.
題面
題目傳送門
解法
顯然可以二分答案
計算的時候用容斥原理即可
用莫比烏斯函數實現這個過程即可
代碼
#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; }
bzoj 2986 Non-Squarefree Numbers 容斥原理+數學