BZOJ2820 - YY的GCD(莫比乌斯反演)

BZOJ2820 - YY的GCD(莫比乌斯反演)

题目链接 BZOJ2820 - YY的GCD

题意

T T T 组查询,每次给定 N , M N, M N,M,求 1 < = x < = N , 1 < = y < = M 1<=x<=N, 1<=y<=M 1<=x<=N,1<=y<=M g c d ( x , y ) gcd(x, y) gcd(x,y) 为质数的 ( x , y ) (x, y) (x,y) 有多少对

T = 10000 , N , M < = 10000000 ​ T = 10000,N, M <= 10000000​ T=10000,N,M<=10000000

题解

A n s = ∑ i = 1 N ∑ j = 1 M [ g c d ( i , j ) = p r i m e ] Ans=\sum^{N}_{i=1}\sum_{j=1}^{M}[gcd(i,j)=prime] Ans=i=1Nj=1M[gcd(i,j)=prime]

设函数 f ( n ) f(n) f(n) 表示 i ∈ [ 1 , N ] , j ∈ [ 1 , M ] i \in [1,N],j \in [1,M] i[1,N],j[1,M] g c d ( i , j ) = n gcd(i,j)=n gcd(i,j)=n ( i , j ) (i,j) (i,j) 对数

设函数 F ( n ) ​ F(n)​ F(n) 表示 i ∈ [ 1 , N ] , j ∈ [ 1 , M ] ​ i\in[1,N],j\in[1,M]​ i[1,N],j[1,M] n   ∣   g c d ( i , j ) ​ n \ | \ gcd(i,j)​ n  gcd(i,j) ( i , j ) ​ (i,j)​ (i,j) 对数

f ​ f​ f F ​ F​ F 满足 F ( n ) = ∑ n ∣ d f ( d ) ​ F(n)=\sum_{n|d}f(d)​ F(n)=ndf(d)

反演可得
f ( n ) = ∑ n ∣ d u ( d n ) F ( d ) = ∑ n ∣ d u ( d n ) ⌊ N d ⌋ ⌊ M d ⌋ f(n)=\sum_{n|d}u(\frac{d}{n})F(d)=\sum_{n|d}u(\frac{d}{n})\lfloor\frac{N}{d}\rfloor\lfloor\frac{M}{d}\rfloor f(n)=ndu(nd)F(d)=ndu(nd)dNdM
因此
A n s = ∑ p ∈ p r i m e f ( p ) = ∑ p ∈ p r i m e ∑ p ∣ d u ( d p ) ⌊ N d ⌋ ⌊ M d ⌋ Ans=\sum_{p\in{prime}}f(p) = \sum_{p\in{prime}}\sum_{p|d}u(\frac{d}{p})\lfloor\frac{N}{d}\rfloor\lfloor\frac{M}{d}\rfloor Ans=pprimef(p)=pprimepdu(pd)dNdM
更换枚举方式,先枚举 d d d,把它当成是某个素数 p p p 的倍数,即
A n s = ∑ d = 1 m i n ( N , M ) ∑ p ∣ d , p ∈ p r i m e u ( d p ) ⌊ N d ⌋ ⌊ M d ⌋ Ans=\sum_{d=1}^{min(N,M)}\sum_{p|d,p\in{prime}}u(\frac{d}{p})\lfloor\frac{N}{d}\rfloor\lfloor\frac{M}{d}\rfloor Ans=d=1min(N,M)pd,pprimeu(pd)dNdM

A n s = ∑ d = 1 m i n ( N , M ) ⌊ N d ⌋ ⌊ M d ⌋ ∑ p ∣ d , p ∈ p r i m e u ( d p ) Ans=\sum_{d=1}^{min(N,M)}\lfloor\frac{N}{d}\rfloor\lfloor\frac{M}{d}\rfloor\sum_{p|d,p\in{prime}}u(\frac{d}{p}) Ans=d=1min(N,M)dNdMpd,pprimeu(pd)

其中
∑ p ∣ d , p ∈ p r i m e u ( d p ) \sum_{p|d,p\in{prime}}u(\frac{d}{p}) pd,pprimeu(pd)
这一部分可以通过欧拉筛把结果筛出来,然后再求前缀和,最后和前面的
⌊ N d ⌋ ⌊ M d ⌋ \lfloor\frac{N}{d}\rfloor\lfloor\frac{M}{d}\rfloor dNdM
一起,用整除分块计算结果

#include
using namespace std;
typedef long long ll;
const int maxn=10000005;

int vis[maxn];
int prime[maxn],tot;
int mu[maxn];
int g[maxn];

void getmu(){
	mu[1]=1;
	for(int i=2;im) swap(n,m);
		solve(n,m);
	}
	return 0;
}

你可能感兴趣的:(莫比乌斯反演)