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

题面戳我

solution

考虑二分答案,二分了一个答案n
在n以内,我们需要快速求出题目要我们求的数的个数

所以可以用莫比乌斯反演
设f[i]表示只含有i^2不含其它完全平方数的数的个数
\(g[i] = \sum_{i}^{i|d} f[d]\)
则g[i]表示所有i^2的倍数\(g[i]=\lfloor \frac{n}{i^2}\rfloor\)
\(f[i] = \sum_{i}^{i|d} mu[d]f[d]\)
f[1]即为二分所求

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e4 + 10), __(1e6 + 10);

IL ll Read(){
	char c = '%'; ll x = 0, z = 1;
	for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
	for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
	return x * z;
}

int k, prime[__], isprime[__], num, mu[__];
ll ans;

IL void Prepare(){
	mu[1] = 1;
	for(RG int i = 2; i <= __; ++i){
		if(!isprime[i]) prime[++num] = i, mu[i] = -1;
		for(RG int j = 1; j <= num && i * prime[j] <= __; ++j){
			isprime[i * prime[j]] = 1;
			if(!(i % prime[j])){  mu[i * prime[j]] = 0; break;  }
			else mu[i * prime[j]] = -mu[i];
		}
	}
}

IL ll Check(RG ll n){
	RG ll f = 0, g = 0;
	for(RG ll i = 1; i * i <= n; ++i) g = n / (i * i), f += mu[i] * g;
	return f;
}

int main(RG int argc, RG char *argv[]){
	Prepare();
	for(RG int T = Read(), i = 1; i <= T; ++i){
		k = Read(); ans = 0;
		RG ll l = 1, r = 1e10;
		while(l <= r){
			RG ll mid = (l + r) >> 1;
			if(Check(mid) >= k) ans = mid, r = mid - 1;
			else l = mid + 1;
		}
		printf("%lld\n", ans);
	}
	return 0;
}

posted @ 2018-01-08 22:02  Cyhlnj  阅读(150)  评论(0编辑  收藏  举报