# 刷题记录【BZOJ2440 完全平方数】数论、组合数学、莫比乌斯函数

https://cn.vjudge.net/contest/210428#problem/J

[1,n]内4的倍数有$[\frac n 4]$
9的倍数有$[\frac n 9]$

μ(n)表示莫比乌斯函数，f(n)表示[1,n]内符合条件的答案数
$f(n)=\sum_{i=1}^{\sqrt n}[\frac n {i^2}]μ(i)$

$f(n)=\sum_{i=1}^{\sqrt n}[\frac n {i^2}]μ(i)≥n-\sum_{i=2}^{\sqrt n}\frac n {i^2}> n- \frac {π^2} 6 n > \frac n 2$

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define N 100000
#define ll long long
int mu[N+5],prime[N+5],tot,t;
bool notprime[N];
void init()
{
notprime[1]=1;
mu[1]=1;
for(int i = 2 ; i <= N; i ++)
{
if(!notprime[i])
{
prime[++tot]=i;
mu[i]=-1;
}
for(int j = 1 ; j <= tot && i  *prime[j] <= N ;  j++)
{
notprime[i*prime[j]]=1;
if(i%prime[j]!=0)
{
mu[i*prime[j]]=-mu[i];
}
else break;
}
}
}
int main()
{
init();
scanf("%d",&t);
while(t--)
{
ll n;
scanf("%lld",&n);
ll l = 1,r = 2LL*n;
while(l<=r)
{
ll mid = (l+r)>>1;
ll ans = 0;
for(ll i =1 ; i * i <= mid ; i++)
{
ans += (mid/(i*i))*mu[i];
}
if(ans < n)
{
l = mid+1;
}
else r = mid-1;
}
printf("%lld\n",l);
}
}
posted @ 2018-12-04 10:59  akonoh  阅读(103)  评论(0编辑  收藏  举报