「SDOI 2015」约数个数和「莫比乌斯反演」

题意

d ( x ) d(x) d(x) x x x的约数个数,求 ∑ i = 1 n ∑ j = 1 m d ( i j ) \sum_{i=1}^{n}\sum_{j=1}^{m}d(ij) i=1nj=1md(ij)

题解

首先有个公式: d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[gcd(x,y)=1] d(ij)=xiyj[gcd(x,y)=1]

证明大致如下:

先考虑这种情况: i = p a i=p^a i=pa, j = p b j=p^b j=pb

乘积 i j ij ij的约数个数为 a + b + 1 a+b+1 a+b+1

∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] \sum_{x|i}\sum_{y|j}[gcd(x,y)=1] xiyj[gcd(x,y)=1]也为 a + b + 1 a+b+1 a+b+1,因为方案为让 i i i 0 0 0 j j j [ 0 , b ] [0,b] [0,b],或 i i i [ 0 , a ] [0,a] [0,a] j j j 0 0 0.

然后考虑把每一位合起来,也是一个道理, i i i的次数分别取 0    or    [ 0 , a i ] 0 \;\text{or}\;[0,a_i] 0or[0,ai], j j j的次数分别取 0    or    [ 0 , b i ] 0 \;\text{or}\;[0,b_i] 0or[0,bi]

然后开始推导本题解法:

∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] \sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}[gcd(x,y)=1] i=1nj=1mxiyj[gcd(x,y)=1]

先枚举因数:

∑ x = 1 n ∑ y = 1 m ⌊ m x ⌋ ⌊ n y ⌋ [ g c d ( x , y ) = 1 ] \sum_{x=1}^n\sum_{y=1}^m\lfloor\frac{m}{x}\rfloor\lfloor\frac{n}{y}\rfloor[gcd(x,y)=1] x=1ny=1mxmyn[gcd(x,y)=1]

f ( u ) = ∑ x = 1 n ∑ y = 1 m ⌊ m x ⌋ ⌊ n y ⌋ [ g c d ( x , y ) = u ] f(u)=\sum_{x=1}^n\sum_{y=1}^m\lfloor\frac{m}{x}\rfloor\lfloor\frac{n}{y}\rfloor[gcd(x,y)=u] f(u)=x=1ny=1mxmyn[gcd(x,y)=u]

因此令:

g ( u ) = ∑ u ∣ d f ( d ) = ∑ x = 1 n ∑ y = 1 m ⌊ m x ⌋ ⌊ n y ⌋ [ u ∣ g c d ( x , y ) ] g(u)=\sum_{u|d}f(d)=\sum_{x=1}^n\sum_{y=1}^m\lfloor\frac{m}{x}\rfloor\lfloor\frac{n}{y}\rfloor[u|gcd(x,y)] g(u)=udf(d)=x=1ny=1mxmyn[ugcd(x,y)]

u u u提出来:

g ( u ) = ∑ u ∣ d f ( d ) = ∑ x = 1 ⌊ n u ⌋ ∑ y = 1 ⌊ m u ⌋ ⌊ m x u ⌋ ⌊ n y u ⌋ g(u)=\sum_{u|d}f(d)=\sum_{x=1}^{\lfloor\frac{n}{u}\rfloor}\sum_{y=1}^{\lfloor\frac{m}{u}\rfloor}\lfloor\frac{m}{xu}\rfloor\lfloor\frac{n}{yu}\rfloor g(u)=udf(d)=x=1uny=1umxumyun

s u m ( n ) = ∑ i = 1 n ⌊ n i ⌋ sum(n)=\sum_{i=1}^n \lfloor\frac{n}{i}\rfloor sum(n)=i=1nin,则上式:

g ( u ) = s u m ( ⌊ n u ⌋ ) s u m ( ⌊ m u ⌋ ) g(u)=sum(\lfloor\frac{n}{u}\rfloor)sum(\lfloor\frac{m}{u}\rfloor) g(u)=sum(un)sum(um)

然后莫比乌斯反演:

因为 g ( u ) = ∑ u ∣ d f ( d ) g(u)=\sum_{u|d}f(d) g(u)=udf(d)

所以 f ( u ) = ∑ u ∣ d μ ( d u ) g ( d ) f(u)=\sum_{u|d}\mu(\frac{d}{u})g(d) f(u)=udμ(ud)g(d)

f ( u ) = ∑ u ∣ d μ ( d u ) g ( d ) f(u)=\sum_{u|d}\mu(\frac{d}{u})g(d) f(u)=udμ(ud)g(d)

所以 f ( 1 ) = ∑ d = 1 n μ ( d ) g ( d ) f(1)=\sum_{d=1}^n\mu(d)g(d) f(1)=d=1nμ(d)g(d)

f ( 1 ) = ∑ d = 1 n μ ( d ) s u m ( ⌊ n d ⌋ ) s u m ( ⌊ m d ⌋ ) f(1)=\sum_{d=1}^n\mu(d)sum(\lfloor\frac{n}{d}\rfloor)sum(\lfloor\frac{m}{d}\rfloor) f(1)=d=1nμ(d)sum(dn)sum(dm)

于是 O ( n n ) O(n\sqrt n) O(nn )预处理 s u m sum sum,查询 O ( n ) O(\sqrt n) O(n )

#include <cstdio>
#include <ios>

typedef long long LL;

const int N = 5e4 + 10;

LL sum[N], mu[N], mus[N];
int pr[N], tot;
bool tag[N];

LL calc1(int n) {
	LL ans = 0;
	for(int i = 1, j; i <= n; i = j + 1) {
		j = n / (n / i);
		ans += n / i * 1ll * (j - i + 1);
	}
	return ans;
}

void init(int n) {
	for(int i = 1; i <= n; i ++) sum[i] = calc1(i);
	mu[1] = mus[1] = tag[1] = 1;
	for(int i = 2; i <= n; i ++) {
		if(!tag[i]) {
			pr[++ tot] = i;
			mu[i] = -1;
		}
		for(int j = 1; j <= tot && i * pr[j] <= n; j ++) {
			tag[i * pr[j]] = 1;
			if(i % pr[j] == 0) {
				mu[i * pr[j]] = 0;
				break ;
			}
			mu[i * pr[j]] = - mu[i];
		} 
		mus[i] = mus[i - 1] + mu[i];
	}
}

LL calc2(int n, int m) {
	LL ans = 0;
	for(int i = 1, j; i <= n; i = j + 1) {
		j = std :: min(n / (n / i), m / (m / i));
		ans += (mus[j] - mus[i - 1]) * sum[n / i] * sum[m / i];
	}
	return ans;
}

int main() {
	init(50000);
	int t, n, m;
	for(scanf("%d", &t); t --; ) {
		scanf("%d%d", &n, &m);
		if(n > m) n ^= m ^= n ^= m;
		printf("%lld\n", calc2(n, m));
	} 
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值