On the algorithm--Möbius inversion

Source: Internet
Author: User
Tags gcd

Objective

Möbius inversion (also known as the "crazy tungsten filament multiplication"), the kind of people to see the thing (in fact, I am too food)
Möbius inversion is useful for solving problems after knowing that the Persimmon (O (n) \) is divided into minutes ( O (\sqrt n) \) .
So, what is Möbius inversion?

Möbius inversion

1. The Uffizi Inversion
If said, there are \ (f (n) \) and \ (g (n) \ ) are two functions defined on the set of positive integers and satisfy
\[f (n) =\sum\limits_{d|n} g (d) \]
Then there is
\[g (n) =\sum\limits_{d|n} f (\dfrac{n}{d}) \times \mu (d) \]
This deformation is the inverse of the MO-

2. Möbius function (Nature/proof)
for any integer n, meet
\[\sum\limits_{d|n} \mu (d) =\ Begin{cases}1&,n=1\\0&,n>1\end{cases}\]
Certificate:

    • N=1, apparently.
    • n>1, set \ (N=p_1^{x_1}\times p_2^{x_2}\times ... \times p_k^{x_k}\), where \ (P_i (1\leqslant i\leqslant k) \) for the coprime factor of N, we set \ (D=p_1^{y_1}\times p_2^{y_2}\times ... \times p_k^{y_k}\), where \ (0\leqslant y_i\leqslant X_i (1\leqslant i\leqslant k) \), when there is a \ (y_i\geqslant2\) , then \ (\mu (d) =0\), so we just consider \ (y_i= 0,1\) of the situation. Suppose d contains the coprime factor of R N, and the exponent of these mass factors is 1, then \ (\mu (d) = ( -1) ^r\), such d has a total of \ (\binom{k}{r}\) .
      Then \ (\sum\limits_{d|n} \mu (d) =\sum\limits_{r=0}^k \binom{k}{r}\times ( -1) ^r= (1-1) ^k=0\)

The Möbius function \ (\MU (n) \) is a integrable function that proves slightly.
This indicates \ (\MU (n) =\prod\limits_{i=1}^k \mu (p_i^{x_i}) \)

Because of these properties of the Möbius function, it can be obtained by using Euler sieve in linear time.

3. Uffizi Inversion (Nature/proof)
First, we prove the correctness of the first persimmon.
\[g (n) =\sum\limits_{d|n}f (\dfrac{n}{d}) \times \mu (d) =\sum\limits_{d|n}\mu (d) \sum\limits_{i|\frac{n}{d}}g (i) \ Longrightarrow\sum\limits_{i|n}g (i) \sum\limits_{i\times d|n}\mu (d) =\sum\limits_{i|n}g (i) \sum\limits_{d|\frac{n} {i}} \mu (d) \]
When the arrows are converted, the \ (g (i) \) is enumerated in advance
Let's now consider the value of \ (\sum\limits_{d|\frac{n}{i}}\mu (d) \)

    • When I=n,\ (\sum\limits_{d|\frac{n}{i}}\mu (d) =1,g (i) \sum\limits_{d|\frac{n}{i}}\mu (d) =g (n) \)
    • When I is an approximate less than n,\ (\sum\limits_{d|\frac{n}{i}}\mu (d) =0,g (i) \sum\limits_{d|\frac{n}{i}}\mu (d) =0\)

So \ (g (n) =\sum\limits_{d|n}f (\dfrac{n}{d}) \times \mu (d) \) Proof

The Möbius function has a property:
When either \ (f (n)/g (n) \) is an integrable function, the other is also an integrable function
This nature ensures that it is then able to perform line sieves.
Prove slightly (prove too long, I don't bother to write)

4. Application of Möbius function

    • \ (g (i) \) is difficult to ask directly, but \ (\sum\limits_{d|i}g (d) \) very good, we can use the Möbius inversion (g (i) \):\[f (i) =\sum\ Limits_{d|i}g (d) \rightarrow g (i) =\sum\limits_{d|i}f (\dfrac{i}{d}) \times \mu (d) \]
    • \ (g (i) \) It is difficult to ask directly, but \ (\sum\limits_{d=1}^{\frac{n}{i}}\) is very good, we can use the Möbius inversion to ask for \ (g (i) \): \[f (i) =\sum\limits_{d=1}^{\frac{n}{i}}g (d\times i) \rightarrow g (i) =\sum\limits_{d=1}^{\frac{n}{i}}f (d\times i) \mu (d) \]
Examples

[POI2007] Zap
Description
FGD is cracking a cipher, and he needs to answer a number of similar questions: for a given integer, a, B, and D, how many positive integers are to X, Y, X<=a,y<=b, and gcd (x, y) =d. As a classmate of FGD, FGD hopes to get your help.

Input
The first line contains a positive integer n, which indicates that there are altogether n groups of queries. (1<=n<= 50000) next n rows, each line represents a query, three positive integers per line, respectively, A,b,d. (1<=d<=a,b<=50000)

Output
For each set of queries, output to the output file zap.out a positive integer that represents the integer logarithm that satisfies the condition.

Sample Input
2
4 5 2
6 4 3

Sample Output
3
2

HINT
For the first set of queries, an integer pair that satisfies the condition has (2,2), (2,4), (4,2). For the second set of queries, an integer pair that satisfies the condition has (6,3), (3,3).

Attention: In order to facilitate the writing, we change the X, Y to I,j, the number of sets of data N to T, the B to the n,m, and we let the N<m

First we list the required persimmon
\[\SUM\LIMITS_{I=1}^N\SUM\LIMITS_{J=1}^M[GCD (i,j) ==d]\]
D seems too annoying, let's just get rid of it.
\[\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_ {J=1}^{\LFLOOR\FRAC{M}{D}\RFLOOR}[GCD (i,j) ==1]\]
and then we find that the last one looks like the Möbius function, slightly changed to get the
\[\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d }\RFLOOR}\SUM\LIMITS_{X|I,X|J}\MU (x) \]
Move the enumeration x to the front
\[\sum\limits_{x=1}^{ \LFLOOR\FRAC{N}{D}\RFLOOR}\MU (x) \lfloor\dfrac{n}{dx}\rfloor\lfloor\dfrac{m}{dx}\rfloor\]
This persimmon has finally become small and fresh, but if the direct enumeration of X will not get rid of the fate of Tle.
Note the following \ (\lfloor\dfrac{n}{dx}\rfloor\lfloor\dfrac{m}{dx}\rfloor?\) In many cases the values are the same, then we do a number-theoretic chunking to
Total time complexity to \ (O (t\sqrt n) \)

/*program from wolfycz*/#include <cmath> #include <cstdio> #include <cstring> #include <iostream > #include <algorithm> #define INF 0x7f7f7f7fusing namespace std;typedef long long ll;typedef unsigned int UI;    typedef unsigned long long ull;inline int read () {int X=0,f=1;char ch=getchar (); for (;ch< ' 0 ' | |    Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;    for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x= (x<<1) + (x<<3) +ch-' 0 '; return x*f;}    inline void print (int x) {if (x>=10) print (X/10); Putchar (x%10+ ' 0 ');}    const int N=5e4;int Prime[n+10],miu[n+10],sum[n+10];bool inprime[n+10];void prepare () {miu[1]=1;    int tot=0;        for (int i=2;i<=n;i++) {if (!inprime[i]) prime[++tot]=i,miu[i]=-1;            for (int j=1;j<=tot&&i*prime[j]<=n;j++) {inprime[i*prime[j]]=1;            if (i%prime[j]==0) {miu[i*prime[j]]=0;break;}        Miu[i*prime[j]]=-miu[i]; }} for (int i=1;i<=n;i++) sum[I]=sum[i-1]+miu[i];}    int main () {prepare ();        for (int data=read ();D ata;data--) {int a=read (), B=read (), D=read ();        ll Ans=0;        A/=d,b/=d;        int X=min (A, b), pos=0;            for (int d=1;d<=x;d=pos+1) {pos=min (A/D), b/(b/d));        ans+=1ll* (Sum[pos]-sum[d-1]) * (A/d) * (b/d);    } printf ("%lld\n", Ans); } return 0;}

Discussion on algorithm--Möbius inversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.