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

[BZOJ2440][中山市选2011]完全平方数

4
1
13
100
1234567

1
19
163
2030745

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;

const int BufferSize = 1 << 16;
inline char Getchar() {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
}
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
}

#define maxn 1000001

int prime[maxn], cnt, mu[maxn];
bool vis[maxn];
void init() {
mu[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!vis[i]) prime[++cnt] = i, mu[i] = -1;
for(int j = 1; prime[j] * i < maxn && j <= cnt; j++) {
vis[prime[j]*i] = 1;
if(i % prime[j] == 0) { mu[prime[j]*i] = 0; break; }
mu[prime[j]*i] = -mu[i];
}
}
return ;
}

int calc(int x) {
int m = sqrt(x + .5), res = 0;
for(int i = 1; i <= m; i++) res += x / (i * i) * mu[i];
return res;
}

int main() {
init();

while(T--) {
int K = read(),  l = 1, r = 2000000000;
while(l < r) {
int mid = l + (r - l >> 1);
if(calc(mid) < K) l = mid + 1; else r = mid;
}
printf("%d\n", l);
}

return 0;
}


posted @ 2017-06-28 20:41  xjr01  阅读(183)  评论(0编辑  收藏  举报