[莫比乌斯函数] BZOJ 2986 Non-Squarefree Numbers & BZOJ 2440 [中山市选2011]完全平方数

不知不觉就发现了2440的双倍经验


考虑二分 然后用容斥求出数量 

x以内的无平方因子数=无需是任何质数的倍数的数的数量(即x)-是至少一个质数平方倍数的数的数量+是至少两个质数平方倍数的数的数量-是至少三个质数平方倍数的数的数量...

发现每个数前的符号与莫比乌斯函数一致


然后么 复杂度分析 Orz




2986的代码


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;

inline char nc(){
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(ll &x){
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int maxn=500000;
int mobius[maxn+5],sum[maxn+5];
int vst[maxn+5],prime[maxn+5],num=0;

inline void Pre()
{
	mobius[1]=1;
	for (int i=2;i<=maxn;i++)
	{
		if (!vst[i])
			mobius[i]=-1,prime[++num]=i;
		for (int j=1;j<=num && prime[j]*i<=maxn;j++)
		{
			vst[i*prime[j]]=1;
			if (i%prime[j]==0)
			{
				mobius[i*prime[j]]=0; break;
			}
			else
				mobius[i*prime[j]]=-mobius[i];
		}
	}
}

ll K;

inline ll Cal(ll x)
{
	ll ret=0;
	ll t=sqrt(x);
	for (ll i=1;i<=t;i++)
		ret+=x/(i*i)*mobius[i];
	return x-ret;
}

int main()
{
	ll Q;
	ll L,R,MID;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	Pre();
	Q=1;
	while (Q--)
	{
		read(K);
		L=0; R=210000000000LL;
		while (L+1<R)
			if (Cal(MID=(L+R)>>1)>=K)
				R=MID;
			else
				L=MID;
		printf("%lld\n",R);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值