# BZOJ 2440: [中山市选2011]完全平方数( 二分答案 + 容斥原理 + 莫比乌斯函数 )

------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

typedef long long ll;

const int maxn = 1000000;

int N;
int p[maxn], mu[maxn], pn = 0;
bool F[maxn];

void Init() {
memset(F, 0, sizeof F);
for(int i = 2; i < maxn; i++) {
if(!F[i])
p[pn++] = i, mu[i] = -1;
for(int j = 0; j < pn && i * p[j] < maxn; j++) {
F[i * p[j]] = true;
if(i % p[j])
mu[i * p[j]] = -mu[i];
else {
mu[i * p[j]] = 0;
break;
}
}
}
}

bool chk(int n) {
ll cnt = n;
for(int i = 2, t = sqrt(n); i <= t; i++)
if(mu[i]) cnt += n / (ll) (i * i) * mu[i];
return cnt >= N;
}

int main() {
Init();
int T; scanf("%d", &T);
while(T--) {
scanf("%d", &N);
ll L = 1LL, R = 2000000000LL, ans;
while(L <= R) {
ll m = (L + R) >> 1;
if(chk(m))
ans = m, R = m - 1;
else
L = m + 1;
}
printf("%lld\n", ans);
}
return 0;
}

------------------------------------------------------------------------

## 2440: [中山市选2011]完全平方数

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1803  Solved: 869
[Submit][Status][Discuss]

4
1
13
100
1234567

1
19
163
2030745

,    T ≤ 50

## Source

posted @ 2015-12-08 19:13  JSZX11556  阅读(223)  评论(0编辑  收藏  举报