标签:getch line get gcd i++ math else lin 整数
设\(d(x)\)为\(x\)的约数个数,给定\(N、M\),求?\(\sum^N_{i=1}\sum^M_{j=1}d(ij)\)
输入文件包含多组测试数据。第一行,一个整数T,表示测试数据的组数。接下来的T行,每行两个整数N、M。
T行,每行一个整数,表示你所求的答案。
2
7 4
5 6
110
121
1<=N, M<=50000
1<=T<=50000
\[ \sum_{i=1}^n\sum_{j=1}^md(ij) \]
\[ \sum_{i=1}^n\sum_{j=1}^m\sum_{x|i}\sum_{y|j} [gcd(x, y)==1] \]
\[ \sum_{i=1}^n\sum_{j=1}^m\sum_{x|i}\sum_{y|j} \sum_{d|gcd(x, y)} \mu(d) \]
\[ \sum_{d = 1}^{min(n,m)}\sum_{i=1}^n\sum_{j=1}^m\sum_{x=1}^{\lfloor\frac i d \rfloor}\sum_{y=1}^{\lfloor\frac j d \rfloor}[xd|i\&\&yd|j] \mu(d) \]
\[ \sum_{d = 1}^{min(n,m)}\mu(d)\sum_{i=1}^n\sum_{j=1}^m\sum_{x=1}^{\lfloor\frac i d \rfloor}\sum_{y=1}^{\lfloor\frac j d \rfloor}[xd|i\&\&yd|j] \]
\[ \sum_{d = 1}^{min(n,m)}\mu(d)\sum_{i=1}^n\sum_{x=1}^{\lfloor\frac i d \rfloor}[xd|i]\sum_{j=1}^m\sum_{y=1}^{\lfloor\frac j d \rfloor}[yd|j] \]
\[ \sum_{d = 1}^{min(n,m)}\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 \]
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 5e4 + 100;
LL pri[maxn], mu[maxn], cnt[maxn], ans[maxn], tot;
bool vis[maxn];
void predoit() {
mu[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!vis[i]) pri[++tot] = i, mu[i] = -1;
for(int j = 1; j <= tot && (LL)i * pri[j] < maxn; j++) {
vis[i * pri[j]] = true;
if(i % pri[j] == 0) break;
else mu[i * pri[j]] = -mu[i];
}
}
for(int i = 1; i < maxn; i++) {
mu[i] += mu[i - 1];
for(LL l = 1, r; l <= i; l = r + 1) {
r = i / (i / l);
ans[i] += (r - l + 1) * (i / l);
}
}
}
LL work(LL n, LL m) {
LL res = 0;
for(LL l = 1, r; l <= std::min(n, m); l = r + 1) {
r = std::min(n / (n / l), m / (m / l));
res += (mu[r] - mu[l - 1]) * ans[n / l] * ans[m / l];
}
return res;
}
int main() {
predoit();
for(int T = in(); T --> 0;) printf("%lld\n", work(in(), in()));
return 0;
}
标签:getch line get gcd i++ math else lin 整数
原文地址:https://www.cnblogs.com/olinr/p/10327728.html