# Problem

## Description

$d(x)$$x$ 的约数个数，给定 $N$$M$，求

$\sum_{i=1}^N \sum_{j=1}^M d(ij)$

## Output Format

$T$ 行，每行一个整数，表示你所求的答案。

## Sample

### Input

2
7 4
5 6

### Output

110
121

# Mentality

$d(i)=\prod_{p\in prime}(x_p+1)$

$d(j)=\prod_{p\in prime}(y_p+1)$

$d(ij)=\sum_{x|i}\sum_{y|j}[gcd(x,y)=1]$

$\sum_{i=1}^n \sum_{j=1}^m d(ij)$
$\sum_{i=1}^n \sum_{j=1}^m \sum_{x|i}\sum_{y|j}[gcd(x,y)=1]$

$\sum_{x=1}^n \sum_{y=1}^m [gcd(x,y)=1]\sum_{i=1}^{n}\sum_{j=1}^m [x|i\&\&y|j]$
$\sum_{x=1}^n \sum_{y=1}^m [gcd(x,y)=1]\lfloor\frac{n}{x}\rfloor\lfloor\frac{m}{y}\rfloor$

$\sum_{x=1}^n \sum_{y=1}^m\sum_{d|gcd(x,y)}\mu(d)\lfloor\frac{n}{x}\rfloor\lfloor\frac{m}{y}\rfloor$

$\sum_{d=1}^n\mu(d)\sum_{x=1}^n\sum_{y=1}^m[d|x\&\&d|y]\lfloor\frac{n}{x}\rfloor\lfloor\frac{m}{y}\rfloor$

$\sum_{d=1}^n\mu(d)\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{n}{xd}\rfloor\lfloor\frac{m}{yd}\rfloor$

$\sum_{d=1}^n\mu(d)(\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\lfloor\frac{n}{xd}\rfloor)(\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{m}{yd}\rfloor)$

$ans=\sum_{d=1}^n\mu(d)*f(\lfloor\frac{n}{d}\rfloor)*f(\lfloor\frac{m}{d}\rfloor)$

# Code

#include <cstdio>
#include <iostream>
using namespace std;
int T, n, m;
int cntp, pri[50001];
long long ans, mu[50001], f[50001];
bool vis[50001];
void init() {
mu[1] = 1;
for (int i = 2; i <= 50000; i++) {
if (!vis[i]) pri[++cntp] = i, mu[i] = -1;
for (int j = 1; j <= cntp && pri[j] * i <= 50000; j++) {
vis[pri[j] * i] = true;
if (!(i % pri[j])) break;
mu[pri[j] * i] = -mu[i];  //求莫比乌斯函数
}
}
for (int i = 1; i <= 50000; i++) mu[i] += mu[i - 1];  //前缀和
for (int i = 1; i <= 50000; i++)
for (int l = 1, r; l <= i; l = r + 1) {
r = i / (i / l);
f[i] += 1ll * (r - l + 1) * (i / l);
}  //求 f 函数
}
int main() {
freopen("3327.in", "r", stdin);
freopen("3327.out", "w", stdout);
cin >> T;
init();
while (T--) {
scanf("%d%d", &n, &m);
if (n > m) swap(n, m);
ans = 0;
for (int l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans +=
1ll * (mu[r] - mu[l - 1]) * f[n / l] * f[m / l];  //整除分块计算答案
}
printf("%lld\n", ans);
}
}

posted @ 2019-05-08 19:57 洛水·锦依卫 阅读(...) 评论(...) 编辑 收藏