51nod 1244 莫比乌斯函数之和
原题链接
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244
题目是让你计算梅藤斯函数。经典杜教筛
u∗I=e
M(n)=1−∑i=2nM(⌊ni⌋)
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <cmath>
#define MAXN 1000005
using namespace std;
typedef long long LL;
LL M[MAXN]={0,-1};
LL tmp[MAXN];
void clat(LL n,int d)
{
LL ans=0;
int m=(int)sqrt(n)+1;
for(int L=1;L<m;L++)
ans+=M[L]*(n/L-n/(L+1));
for(int i=(int)(n/m);i>1;i--)
{
LL u=n/i;
if(u<MAXN)
ans+=M[u];
else
ans+=tmp[i*d];
}
tmp[d]=1-ans;
}
LL slove(LL n)
{
if(n<MAXN)return M[n];
for(int i=(int)(n/(MAXN-1));i;i--) clat(n/(LL)i,i);
return tmp[1];
}
int main ()
{
for(int i=1;i<MAXN;i++)
{
M[i]=-M[i];
for(int j=i<<1;j<MAXN;j+=i)M[j]+=M[i];
M[i]+=M[i-1];
}
LL a,b;
scanf("%lld %lld",&a,&b);
a--;
printf("%lld\n",slove(b)-slove(a));
return 0;
}