P2257 YY的GCD 线性筛+莫比乌斯反演+数论分块

在这里插入图片描述
求的是 ∑ i = 1 n ∑ j = 1 m [ ( i , j ) ∈ p r i m e ] \sum_{i=1}^n\sum_{j=1}^m[(i,j)\in prime] i=1nj=1m[(i,j)prime]
= ∑ p ∈ p r i m e ∑ i = 1 n ∑ j = 1 m [ ( i , j ) = p ] =\sum_{p\in prime}\sum_{i=1}^n\sum_{j=1}^m[(i,j)=p] =pprimei=1nj=1m[(i,j)=p]
= ∑ p ∈ p r i m e ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ [ ( i , j ) = 1 ] =\sum_{p\in prime}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}[(i,j)=1] =pprimei=1pnj=1pm[(i,j)=1]
= ∑ p ∈ p r i m e ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ ∑ d ∣ ( i , j ) μ ( d ) =\sum_{p\in prime}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}\sum_{d|(i,j)}\mu(d) =pprimei=1pnj=1pmd(i,j)μ(d)
= ∑ p ∈ p r i m e ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ ∑ d = 1 m i n ( ⌊ n p ⌋ , ⌊ m p ⌋ ) [ d ∣ i ] [ d ∣ j ] μ ( d ) =\sum_{p\in prime}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}\sum_{d=1}^{min(\lfloor\frac{n}{p}\rfloor,\lfloor\frac{m}{p}\rfloor)}[d|i][d|j]\mu(d) =pprimei=1pnj=1pmd=1min(pn,pm)[di][dj]μ(d)
= ∑ p ∈ p r i m e ∑ d = 1 m i n ( ⌊ n p ⌋ , ⌊ m p ⌋ ) μ ( d ) ∑ i = 1 ⌊ n p ⌋ [ d ∣ i ] ∑ j = 1 ⌊ m p ⌋ [ d ∣ j ] =\sum_{p\in prime}\sum_{d=1}^{min(\lfloor\frac{n}{p}\rfloor,\lfloor\frac{m}{p}\rfloor)}\mu(d)\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}[d|i]\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}[d|j] =pprimed=1min(pn,pm)μ(d)i=1pn[di]j=1pm[dj] = ∑ p ∈ p r i m e ∑ d = 1 m i n ( ⌊ n p ⌋ , ⌊ m p ⌋ ) μ ( d ) ⌊ n d p ⌋ ⌊ m d p ⌋ =\sum_{p\in prime}\sum_{d=1}^{min(\lfloor\frac{n}{p}\rfloor,\lfloor\frac{m}{p}\rfloor)}\mu(d)\lfloor\frac{n}{dp}\rfloor\lfloor\frac{m}{dp}\rfloor =pprimed=1min(pn,pm)μ(d)dpndpm
一般到这里就可以,因为没有套外面的枚举素数,所以本题要继续推555
T = d p T=dp T=dp n < m n<m n<m
= ∑ p ∈ p r i m e ∑ T = 1 , p ∣ T n μ ( T p ) ⌊ n T ⌋ ⌊ m T ⌋ =\sum_{p\in prime}\sum_{T=1,p|T}^{n}\mu(\frac{T}{p})\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor =pprimeT=1,pTnμ(pT)TnTm
换一下枚举方式,
= ∑ T = 1 n ⌊ n T ⌋ ⌊ m T ⌋ ∑ p ∈ p r i m e , p ∣ T μ ( T p ) =\sum_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum_{p\in prime,p|T}\mu(\frac{T}{p}) =T=1nTnTmpprime,pTμ(pT)
f ( T ) = ∑ p ∈ p r i m e , p ∣ T μ ( T p ) f(T)=\sum_{p\in prime,p|T}\mu(\frac{T}{p}) f(T)=pprime,pTμ(pT)
= ∑ T = 1 n f ( T ) ⌊ n T ⌋ ⌊ m T ⌋ =\sum_{T=1}^nf(T)\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor =T=1nf(T)TnTm
f f f是可以用线性筛求的,
在这里插入图片描述
接下来求一下 f f f的前缀和,数论分块求即可。

#include <cstdio>
#define min(a,b) ((a<b)?(a):(b))
#define ll long long
using namespace std;

const int N = 1e7+10, lnN = 15;
int p[N], prime[N/lnN], f[N];
int mu[N], cnt;
void primes(ll n){
	mu[1] = p[1] = 1;
	for(register int i = 2; i <= n; i++){
		if(!p[i]) p[i] = prime[++cnt] = (ll)i, f[i] = 1, mu[i] = -1;
		for(register int j = 1; j <= cnt && prime[j] <= n/i; j++){
			ll x = 1ll*i*prime[j];
			p[x] = prime[j];
			if(i%prime[j] == 0){
				f[x] = mu[i];mu[x] = 0;
				break;
			}
			mu[x] = -mu[i];f[x] = mu[i]-f[i];
		}
	}
	for(register int i = 2; i <= n; i++) f[i] += f[i-1];
}
ll solve(int k, int a, int b){
	ll ans = 0;
	for(register ll l = 1, r; l <= k && a/l && b/l; l = r+1){
		ll r1 = min(k, a/(a/l));
		ll r2 = min(k, b/(b/l));
		r = min(r1, r2);
		ans += 1ll*(f[r]-f[l-1])*(a/l)*(b/l);
	}
	return ans;
}
int main(){
	int t;
	primes(1e7);
	scanf("%d", &t);
	while(t--){
		int n, m;
		scanf("%d%d", &n, &m);
		if(n>m) n^=m^=n^=m;
		printf("%lld\n", solve(n, n, m));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值