[BZOJ2440][中山市选2011]完全平方数(莫比乌斯函数,二分)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2440
题意:RT。
是https://wenku.baidu.com/view/fbec9c63ba1aa8114431d9ac.html中的一道例题,拿来切了练手。
mark一个博客:http://blog.csdn.net/acdreamers/article/details/8542292
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const LL maxn = 1001000; 6 LL vis[maxn], mu[maxn], prime[maxn], cnt; 7 LL n; 8 9 void init() { 10 memset(vis,0,sizeof(vis)); 11 mu[1] = 1; 12 cnt = 0; 13 for(LL i=2; i<maxn; i++) { 14 if(!vis[i]) { 15 prime[cnt++] = i; 16 mu[i] = -1; 17 } 18 for(LL j=0; j<cnt&&i*prime[j]<maxn; j++) { 19 vis[i*prime[j]] = 1; 20 if(i%prime[j]) mu[i*prime[j]] = -mu[i]; 21 else { 22 mu[i*prime[j]] = 0; 23 break; 24 } 25 } 26 } 27 } 28 29 bool check(LL x) { 30 LL xx = (LL)sqrt(x); 31 LL ret = 0; 32 for(LL i = 1; i <= xx; i++) { 33 ret += mu[i]*(LL)(x/(i*i)); 34 } 35 return ret < n; 36 } 37 38 signed main() { 39 // freopen("in", "r", stdin); 40 init(); 41 LL T; 42 scanf("%lld", &T); 43 while(T--) { 44 scanf("%lld", &n); 45 LL lo = 1, hi = (LL)1e10; 46 LL ret = 0; 47 while(lo <= hi) { 48 LL mid = (lo + hi) >> 1; 49 if(check(mid)) lo = mid + 1; 50 else hi = mid - 1; 51 } 52 printf("%lld\n", lo); 53 } 54 return 0; 55 }