On Mobius function & Inclusion exclusion theorem (example: number coloring, complete square number)

Keywords: Algorithm number theory

Today, I spent a day grinding Mobius function and did two questions. I have a lost understanding of Mobius. If I want to take notes, I'll write this blog

First, let's introduce what is an integral function. When gcd(a,b)=1 and f(a,b)=f(a)*f(b), f(x) is an integral function. For any a and B, f(a,b)=f(a)*f(b), it is called a complete integral function. Then the integral function has a very important property, which can be screened out by the linear screening method. Euler function and prime number, including Mobius function, are all integral functions

Then we should understand why Mobius:

Mobius function is a piecewise function, which means that when n=1, the value of Mobius function is 1. When n can be decomposed into many prime numbers and the number of these prime factors is 1, the Mobius value is the power of the number of prime factors of - 1. Then except for these cases, the value of Mobius function is 0;

Mobius value of the first fifty numbers

 

Give me a board first (in fact, it is the extension of linear sieve prime):

void init()
{
    int tot=0,k;
    mo[1]=1;
    for(int i=2;i<=40559;i++)
    {
        if(vis[i]==0)
        {
            p[++tot]=i;
            mo[i]=-1;
        }
        for(int j=1;j<=tot&&(k=i*p[j])<=40559;j++)
        {
            vis[k]=1;
            if(i%p[j]!=0)mo[k]=-mo[i];
            else 
            {
                mo[k]=0;
                break;
            }
        }
    }
}

Then talk about its two properties:

(1)The figure means that the sum of Mobius values of all factors of n is 1 only when n=1, and the rest are all 0;

Proof of this property:

First, we can decompose n into many prime numbers and multiply them (at this time, the power of prime numbers is not necessarily 1):

order n=p1^ a1* p 2^ a2 ... p k^ ak

Because this is the sum of their Mobius values, which is the sum of the Mobius values of all his divisors, it is equivalent to selecting one, two, three... Prime factors from so many prime numbers to multiply to form all divisors. However, according to the properties of Mobius function, the divisors containing the square of prime numbers need not be calculated, and his contribution to the answer is 0, All the times of prime numbers decomposed by N can be eliminated to 1. Only when the Mobius value is - 1 or 1 can it contribute to the result. Then the problem simply becomes to select 0 to k values from K prime factors of n to form divisors, and then add the values of these divisors:

Note that the symbols here are not all addition, but addition and subtraction. This is because Mobius function is an integral function. When the number selected is an odd number, it is negative, otherwise it is positive. Due to the binomial theorem, if - 1 and 1 are brought in, the result is 0

The formula here is very similar to the inclusion exclusion theorem. By the way, I will record a small part of the inclusion exclusion theorem I understand:

For example, it's easy to understand. When we want to calculate the area of this figure, we can first calculate the sum of the areas of figures a, B and C. after the calculation, we will find that the overlapping part is calculated more. Then, try to subtract the area of every two intersecting figures, so that after the area of every two intersecting figures is subtracted, we will find that, When calculating three large areas for the first time, the area in the middle of the three times is calculated. When subtracting the area where two intersect for the second time, it is subtracted for three times, which is equivalent to no calculation. Here, we need to add the area in the middle again to get the total area of the graph. It probably means that when you calculate a set containing multiple properties (the elements in each set may have many properties), which can be calculated in this way. (in fact, I don't understand it very well. It will be used in the following example. It is very similar to the distribution of Mobius function values)

(2) For any positive integer n(ps: this one involves Mobius inversion. I haven't learned it yet. I'll read it after learning it.)

  Well, this is the general knowledge. After learning this, I still don't know what the use of this thing is. I still have to rely on practical combat:

  Example: complete square number

List of topics - Luoguhttps://www.luogu.com.cn/problem/list?keyword=%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0&page=1 The meaning of the question is to sift out the complete square number and its multiples, and then output the value of k

When we ask for this, we want to sift all the complete square numbers from 1 to Ki and their multiples, but when we look at the data, 1e9, the linear sieve must be t, then we find a way to calculate. First, we calculate the multiples of square 4 of 2. In 1 to ki, there are ki/4 multiples of 4. When we calculate the multiples of 16, we will find that the multiples of 16 have been calculated when calculating the multiples of 4 After that, it will be repeated here. If you have calculated the multiples of 4 and 9, and then calculate the multiples of 36, you will find that it has been calculated twice. Then you need to subtract the multiples of 36, and it has been thought that you can use exclusion. Here we find that you need to enumerate the times of the squares of prime numbers, and the odd and even symbols are different, you will think of Mobius function. It calculates the edges of enumeration The bound is I * I < = n; the number of complete squares from 2 to ki calculated by subtracting n is the result:

  After the solution is obtained, we can use dichotomy to find the value at this time:

#include<iostream>
#define ll long long 
using namespace std;
int vis[40560],mo[40560],p[4253],n;
void init()
{
    int tot=0,k;
    mo[1]=1;
    for(int i=2;i<=40559;i++)
    {
        if(vis[i]==0)
        {
            p[++tot]=i;
            mo[i]=-1;
        }
        for(int j=1;j<=tot&&(k=i*p[j])<=40559;j++)
        {
            vis[k]=1;
            if(i%p[j]!=0)mo[k]=-mo[i];
            else 
            {
                mo[k]=0;
                break;
            }
        }
    }
}
bool judge(int x)
{
	ll ans=0;int i;
    for(int i=1;i*i<=x;i++)
    {
        ans+=mo[i]*(x/(i*i));
    }
    if(ans>=n)return true;
    else return false;
}
int main()
{
    int t;
    ll l,r,mid;
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        l=n,r=1644934082;
        while(l<r) 
        {
            // cout<<l<<" oo "<<r<<endl;
			mid=(l+r)>>1;
			if(judge(mid)) 
                r=mid;
			else l=mid+1;
		}
        printf("%lld\n",r);
    }
    return 0;
}

Example: Digital coloring

Login - professional IT written examination interview preparation platform niuke.comhttps://ac.nowcoder.com/acm/problem/221827 Title Meaning:

Give an array and select the number in it to see how many sets with GCD > 1 can be selected at most

If GCD > 1 is required, we will find all their divisors and record them in an si array (subscript is the value of this divisor, which stores how many times it has been used as a divisor by the numbers in the array) Then we traverse the si array and calculate the number of divisors that each divisor may form. For example, if 2 is used as a divisor by the numbers in the array for a total of 2 times, then all the divisors it can form are 1,2,4, that is, 2 ^ 2-1 combinations. When we choose to calculate all the common divisors that can form 2 and all the common divisors that can form 3, then when we calculate 6 It will be found that 6 has been calculated by 2 and 3. Well, the inclusion exclusion theorem uses the Mobius function as the coefficient to find the sum of (2^n-1). However, there is another problem. When the odd prime number is selected to calculate the number, it should be added, while the Mobius function value is - 1, and the even number should be subtracted. Therefore, we can take an inverse when calculating the Mobius value. Finally, the formula we require is:

Upper Code:

#include<iostream>
#define ll long long
using namespace std;
ll a[200005],vis[200005],p[200005],mo[200005];
ll si[200005];
const ll mod=1e9+7;
void init()
{
    ll tot=0,k;
    mo[1]=1;
    for(int i=2;i<=200005;i++)
    {
        if(vis[i]==0)
        {
            p[++tot]=i;
            mo[i]=1;
        }
        for(int j=1;j<=tot&&(k=i*p[j])<=200005;j++)
        {
            vis[k]=1;
            if(i%p[j]==0)
            {
                mo[k]=0;
                break;
            }
            else mo[k]=-mo[i];
        }
    }
}
ll qpow(ll pp,ll x)
{
    ll ans=1;
    while(x)
    {
        if(x&1)ans=ans%mod*pp%mod;
        pp=pp*pp%mod;
        x>>=1;
    }
    return ans;
}
int main()
{
    ll n,ans=0;
    init();
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        for(int j=1;j*j<=a[i];j++)
        {
            if(j*j==a[i])
                si[j]++;
            else if(a[i]%j==0)
            {
                si[j]++;
                si[a[i]/j]++;
            }
        }
    }
    for(int i=2;i<=200005;i++)
    {   
        ans+=(mo[i]*(qpow(2,si[i])-1)%mod)%mod;       
    }
    printf("%lld",(ans%mod+mod)%mod);
    return 0;
}

  ps: the final result is to prevent the negative number from taking the module. Add mod and take the module again

I will continue to study Mobius inversion later. If there are any mistakes in Ben konjac, I hope the boss can correct them

Posted by ardyandkari on Tue, 21 Sep 2021 01:33:37 -0700