51nod 1244 Mobius function sum (Du teach sieve)

51nod 1244 Mobius function sum (Du teach sieve)

Portal

Meaning of the questions:

begging i = a b m ( i ) \sum_{i=a}^{b}\mu{(i)}

answer:

This is the question of quadrature function and the prefix, a Du teach screen template title.
Equation is derived as follows:
Suppose F ( n ) = i = 1 n m ( i ) \ Phi {(n)} = \ sum_ {i = 1} ^ {n} \ mu {(i)}
We know that there
d i m ( d ) = [ n = = 1 ] \sum_{d|i}{\mu{(d)}}=[n==1]
we can become like the above formula
i = 1 n j i μ ( j ) = 1 \sum_{i=1}^{n}{\sum_{j|i}{\mu{(j)}}}=1
then the change to the formula ( i i becomes i d \frac{i}{d} )
i = 1 n j = 1 n i μ ( j ) = 1 \sum_{i=1}^{n}{\sum_{j=1}^{\lfloor{\frac{n}{i}}\rfloor}{\mu{(j)}}}=1
i = 1 n ϕ ( n i ) = 1 \sum_{i=1}^{n}{\phi{(\lfloor{\frac{n}{i}}\rfloor)}}=1
and then construct it
ϕ ( n ) = 1 i = 2 n ϕ ( n i ) \phi{(n)}=1-\sum_{i=2}^{n}{\phi{(\lfloor{\frac{n}{i}}\rfloor)}}
so we O ( n 3 4 ) O(n^{\frac{3}{4}}) 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)); 
}

Published 51 original articles · won praise 6 · views 6326

Guess you like

Origin blog.csdn.net/weixin_40859716/article/details/82749891