51nod 1244 Mobius function sum (Du teach sieve)
Meaning of the questions:
begging
answer:
This is the question of quadrature function and the prefix, a Du teach screen template title.
Equation is derived as follows:
Suppose
We know that there
we can become like the above formula
then the change to the formula (
becomes
)
and then construct it
so we
Calculate the answer within the time it
Code:
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e6+5;
int mu[maxn],a[maxn];
map<long long,long long>b;
bool isprime[maxn];
int prime[maxn],X;
void Mu()
{
X=5e6;
memset(isprime,1,sizeof(isprime));
mu[1]=1;a[1]=1;
int temp=0;
for(int i=2;i<maxn;i++)
{
if(isprime[i]) prime[++temp]=i,mu[i]=-1;
for(int j=1;j<=temp&&prime[j]*i<maxn;j++)
{
isprime[i*prime[j]]=0;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else mu[i*prime[j]]=-mu[i];
}
a[i]=a[i-1]+mu[i];
}
}
int n;
long long sumMu(long long m)
{
if(m<=X) return a[m];
if(b[m]) return b[m];
long long s=0;
for(long long i=2,j;i<=m;i=j+1)//分块优化
{
j=m/(m/i);
s+=(j-i+1)*sumMu(m/i);
}
b[m]=1-s;
return b[m];
}
int main()
{
int T,i,j,k;
long long n,m;
Mu();
scanf("%lld%lld",&n,&m);
printf("%lld\n",sumMu(m)-sumMu(n-1));
}