# BZOJ3994: [SDOI2015]约数个数和

## Description

$\sum_{i=1}^{n}\sum_{j=1}^md(i*j)$

## Output

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

2
7 4
5 6

110
121

1<=N, M<=50000

1<=T<=50000

## Solution

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

https://blog.csdn.net/ab_ever/article/details/76737617

\large{ \begin{aligned} &\sum_{i=1}^{n}\sum_{j=1}^md(i*j)\\ &=\sum_{i=1}^{n}\sum_{j=1}^m\sum_{x|i}\sum_{y|j}[(x,y)=1]\\ &=\sum_{x=1}^{n}\sum_{y=1}^{m}\sum_{i=1}^{\lfloor\frac{n}{x}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{y}\rfloor}[(x,y)=1]\\ &=\sum_{x=1}^{n}\sum_{y=1}^{m}\lfloor\frac{n}{x}\rfloor\lfloor\frac{m}{y}\rfloor[(x,y)=1]\\ &=\sum_{x=1}^{n}\sum_{y=1}^{m}\lfloor\frac{n}{x}\rfloor\lfloor\frac{m}{y}\rfloor\sum_{d|(x,y)}\mu(d)\\ &=\sum_{d=1}^{n}\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{n}{dx}\rfloor\lfloor\frac{m}{dy}\rfloor*\mu(d)\\ &=\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}{dx}\rfloor\lfloor\frac{m}{dy}\rfloor\\ &=\sum_{d=1}^{n}\mu(d)\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\lfloor\frac{n}{dx}\rfloor\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{m}{dy}\rfloor\\ \end{aligned}\\ 设g(x)=\sum_{i=1}^{n}{\lfloor\frac{n}{i}\rfloor}\\ 则代入原式可得\\ \begin{aligned} &\sum_{d=1}^{n}\mu(d)\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\lfloor\frac{n}{dx}\rfloor\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{m}{dy}\rfloor\\ &=\sum_{d=1}^{n}\mu(d)*g(\lfloor\frac{n}{d}\rfloor)*g(\lfloor\frac{m}{d}\rfloor) \end{aligned} }

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 50010
int p[N], vis[N], mu[N], sum[N];
int n, m, cnt;
ll ans = 0, g[N];

void init() {
mu[1] = 1;
for(int i = 2; i < N; ++i) {
if(!vis[i]) p[++cnt] = i, mu[i] = -1;
for(int j = 1; j <= cnt && i * p[j] < N; ++j) {
vis[i * p[j]] = 1;
if(i % p[j] == 0) break;
mu[i * p[j]] -= mu[i];
}
}
for(int i = 1; i < N; ++i) {
sum[i] = sum[i - 1] + mu[i];
for(int l = 1, r; l <= i; l = r + 1) {
r = i / (i / l);
g[i] += 1ll * (r - l + 1) * (i / l);
}
}
}

int main() {
init();
int T;
scanf("%d", &T);
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 * (sum[r] - sum[l - 1]) * g[n / l] * g[m / l];
//			printf("%d %d %d\n", (sum[r] - sum[l - 1]), g[n / l], g[m / l]);
}
printf("%lld\n", ans);
}
return 0;
}

posted @ 2019-01-11 13:41  henry_y  阅读(107)  评论(0编辑  收藏  举报