Mobius反演的套路
T1
\(\sum_{i=1}^N \sum_{j=1}^M [(i,j)=1]\)
\(f(d)=\sum_{i=1}^N \sum_{j=1}^M [(i,j)=d]\)
\(g(d)=\sum_{i=1}^N \sum_{i=1}^M [d|(i,j)]=\lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor\)
\(g(n)=\sum_{n|d} f(d)\)
\(f(n)=\sum_{n|d} \mu(\frac{d}{n})g(d)\)
\(f(1)=\sum_{i=1}^{\min(N,M)} \mu(i)\lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{i} \rfloor\)
T2
\(\sum_{i=1}^N \sum_{j=1}^M (i,j)\)
\(f(d)=\sum_{i=1}^N \sum_{j=1}^M d[(i,j)=d]=d\sum_{i=1}^N \sum_{j=1}^M [(i,j)=d]=d\sum_{i=1}^{\lfloor \frac{\min(N,M)}{d} \rfloor} \mu(i) \lfloor \frac{N}{id} \rfloor \lfloor \frac{M}{id} \rfloor\)
\(Ans=\sum_{d=1}^{\min(N,M)} f(d)=\sum_{d=1}^{\min(N,M)} d\sum_{i=1}^{\lfloor \frac{\min(N,M)}{d} \rfloor} \mu(i) \lfloor \frac{N}{id} \rfloor \lfloor \frac{M}{id} \rfloor\)
設\(w=id\)
\(Ans=\sum_{w=1}^{\min(N,M)} \sum_{d|w} \mu(\frac{w}{d}) \lfloor \frac{N}{w} \rfloor \lfloor \frac{M}{w} \rfloor=\sum_{w=1}^{\min(N,M)} \lfloor \frac{N}{w} \rfloor \lfloor \frac{M}{w} \rfloor \sum_{d|w} \mu(d)\)
\(\sum_{d|w} \mu(d)\)顯然是積性函數,線性篩後做下前綴和,離線\(\Theta(\max(N,M))\)
\(\sum_{w=1}^{\min(N,M)} \lfloor \frac{N}{w} \rfloor \lfloor \frac{M}{w} \rfloor\)
多組詢問下總復雜度\(\Theta(\max(N,M)+T(\sqrt{N}+\sqrt{M}))\)
T3
\(\sum_{i=1}^N \sum_{j=1}^M \frac{ij}{(i,j)}\)
\(f(d)=\sum_{i=1}^{\lfloor \frac{N}{d} \rfloor} \sum_{j=1}^{\lfloor \frac{M}{d} \rfloor} ijd[(i,j)=1]=d \sum_{i=1}^{\lfloor \frac{N}{d} \rfloor} \sum_{j=1}^{\lfloor \frac{M}{d} \rfloor} ij[(i,j)=1]\)
\(Ans=\sum_{d=1}^{\min(N,M)} f(d)\)
\(Ans=\sum_{d=1}^{\min(N,M)} d \sum_{i=1}^{\lfloor \frac{N}{d} \rfloor} \sum_{j=1}^{\lfloor \frac{M}{d} \rfloor} ij\sum_{n|(i,j)} \mu(n)\)
\(Ans=\sum_{d=1}^{\min(N,M)} d \sum_{n=1}^{\lfloor \frac{\min(N,M)}{d} \rfloor} n (\sum_{i=1}^{\lfloor \frac{N}{dn} \rfloor} i)n(\sum_{j=1}^{\lfloor \frac{M}{dn} \rfloor} j)\)
設\(w=dn\)
\(Ans=\sum_{w=1}^{\min(N,M)} (\sum_{i=1}^{\lfloor \frac{N}{w} \rfloor} i)(\sum_{j=1}^{\lfloor \frac{M}{w} \rfloor} j) w\sum_{n|w} n \mu(n)\)
線篩前綴和+整除分塊
復雜度與上題相同
Mobius反演的套路