Study Notes Section 5: Mobius Inversion

The subject

      This thing is rather disgusting, because at first you may look at the formulas very blind, but in the end they find that these formulas are not useful.

      The two most important formulas are

If there is: F(n)=d|nf(d)

then there is f(n)=d|nμ ( d)F(nd)

It's that simple, you just have to remember this.

Second, you need to understand the definition of the μ function, i.e.

When n=1, the function value is 1; when n=p1*p2*...*pk, p is a prime number and p is different from each other, then the value is (-1)^k; otherwise, it is 0.

So the proof doesn't need me to say more, these two formulas are very simple.

It's about his application! !

First, you can use this thing to find the logarithm of gcd for k.

We set f(x)= i=1~n  j=1~m   [gcd(i,j)=x]

   Let F (x)= i=1~n  j=1~m   [x | gcd(i,j)]=(n/x)*(m/x)

   Then we can know that F(n)=n | d nf(d)

   Pushing it backwards, you can find that f(d) is easy to calculate! !

   Then we use division to optimize it in blocks and output directly.

   The time complexity is reduced from the original n*m to sqrt(min(n,m));

   The division block means that for a pair of l and r, make them equal to the number divided by n, that is, n/l=n/r, then it is known that l,r=n/(n/l);

   Then use l=r+1 to jump to the next block.



F ( n ) = d|n f ( d )
F ( n ) = d|n f ( d )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902525&siteId=291194637