设 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=1n∑j=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)=x∣i∑y∣j∑[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] ∑x∣i∑y∣j[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=1n∑j=1m∑x∣i∑y∣j[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=1n∑y=1m⌊xm⌋⌊yn⌋[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=1n∑y=1m⌊xm⌋⌊yn⌋[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)=u∣d∑f(d)=x=1∑ny=1∑m⌊xm⌋⌊yn⌋[u∣gcd(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)=u∣d∑f(d)=x=1∑⌊un⌋y=1∑⌊um⌋⌊xum⌋⌊yun⌋
令 s u m ( n ) = ∑ i = 1 n ⌊ n i ⌋ sum(n)=\sum_{i=1}^n \lfloor\frac{n}{i}\rfloor sum(n)=∑i=1n⌊in⌋,则上式:
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)=u∣d∑f(d)
所以 f ( u ) = ∑ u ∣ d μ ( d u ) g ( d ) f(u)=\sum_{u|d}\mu(\frac{d}{u})g(d) f(u)=u∣d∑μ(ud)g(d)
f ( u ) = ∑ u ∣ d μ ( d u ) g ( d ) f(u)=\sum_{u|d}\mu(\frac{d}{u})g(d) f(u)=u∣d∑μ(ud)g(d)
所以 f ( 1 ) = ∑ d = 1 n μ ( d ) g ( d ) f(1)=\sum_{d=1}^n\mu(d)g(d) f(1)=d=1∑nμ(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=1∑nμ(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
#include
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;
}