描述
莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出。梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号。具体定义如下:
如果一个数包含平方因子,那么miu(n) = 0。例如:miu(4), miu(12), miu(18) = 0。
如果一个数不包含平方因子,并且有k个不同的质因子,那么miu(n) = (-1)^k。例如:miu(2), miu(3), miu(30) = -1,miu(1), miu(6), miu(10) = 1。
给出一个区间[a,b],S(a,b) = miu(a) + miu(a + 1) + …… miu(b)。
例如:S(3, 10) = miu(3) + miu(4) + miu(5) + miu(6) + miu(7) + miu(8) + miu(9) + miu(10) = -1 + 0 + -1 + 1 + -1 + 0 + 0 + 1 = -1。
Input
输入包括两个数a, b,中间用空格分隔(2 <= a <= b <= 10^10)
Output
输出S(a, b)。
Input示例
3 10
Output示例
-1
思路
对于区间和我们可以将其分解为两个前缀和的差
求 $\sum_{i=l}^rμ(i)$
我们设 $M(n)=\sum_{i=1}^nμ(i)$
已知 $\sum_{d|n}μ(d)=[n=1]$ (当 $n=1$ 时结果为 $1$ ,其余为 $0$ )
所以有 $\sum_{i=1}^{n}\sum_{d|i}μ(d)=\sum_{i=1}^{n}\sum_{d=1}^{\lfloor\frac{n}{i}\rfloor}μ(d)=\sum_{i=1}^{n}M(\lfloor\frac{n}{i}\rfloor)=1$
因为 $\sum_{i=1}^{n}M(\lfloor\frac{n}{i}\rfloor)=M(n)+\sum_{i=2}^{n}M(\lfloor\frac{n}{i}\rfloor)=1$
所以 $M(n)=1-\sum_{i=2}^{n}M(\lfloor\frac{n}{i}\rfloor)$
然后就可以根据这个式子递归得出结果咯~
需要注意时间上的优化:
- 因为 $\lfloor\frac{n}{i}\rfloor$ 可能得到相同值,故可以采用分块加速
- 记录已得出的结果,在之后遇到的时候直接使用
- 预先处理出一段前缀和
AC 代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,bool> P;
const int maxn = 5000000;
const int mod = 1e5+7;
map<LL,P> mp[mod];
bool check[maxn];
int prime[maxn];
int mu[maxn];
void Moblus()
{
memset(check,false,sizeof(check));
mu[1]=1;
int tot=0;
for(int i=2; i<maxn; i++)
{
if(!check[i])
{
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0; j<tot && i*prime[j]<maxn; j++)
{
int num=i*prime[j];
check[num]=true;
if(i%prime[j]==0)
{
mu[num]=0;
break;
}
else
mu[num]=-mu[i];
}
}
for(int i=2; i<maxn; i++)
mu[i]+=mu[i-1];
}
int call(LL x)
{
if(x<maxn)return mu[x];
P p=mp[x%mod][x];
if(p.second)
return p.first;
int ans=1;
for(LL i=2,la=0; i<=x; i=la+1)
{
la=x/(x/i);
ans-=(la-i+1)*call(x/i);
}
mp[x%mod][x]=P(ans,true);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
Moblus();
LL a,b;
while(cin>>a>>b)
cout<<call(b)-call(a-1)<<endl;
return 0;
}