求的是
∑
i
=
1
n
∑
j
=
1
m
[
(
i
,
j
)
∈
p
r
i
m
e
]
\sum_{i=1}^n\sum_{j=1}^m[(i,j)\in prime]
i=1∑nj=1∑m[(i,j)∈prime]
=
∑
p
∈
p
r
i
m
e
∑
i
=
1
n
∑
j
=
1
m
[
(
i
,
j
)
=
p
]
=\sum_{p\in prime}\sum_{i=1}^n\sum_{j=1}^m[(i,j)=p]
=p∈prime∑i=1∑nj=1∑m[(i,j)=p]
=
∑
p
∈
p
r
i
m
e
∑
i
=
1
⌊
n
p
⌋
∑
j
=
1
⌊
m
p
⌋
[
(
i
,
j
)
=
1
]
=\sum_{p\in prime}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}[(i,j)=1]
=p∈prime∑i=1∑⌊pn⌋j=1∑⌊pm⌋[(i,j)=1]
=
∑
p
∈
p
r
i
m
e
∑
i
=
1
⌊
n
p
⌋
∑
j
=
1
⌊
m
p
⌋
∑
d
∣
(
i
,
j
)
μ
(
d
)
=\sum_{p\in prime}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}\sum_{d|(i,j)}\mu(d)
=p∈prime∑i=1∑⌊pn⌋j=1∑⌊pm⌋d∣(i,j)∑μ(d)
=
∑
p
∈
p
r
i
m
e
∑
i
=
1
⌊
n
p
⌋
∑
j
=
1
⌊
m
p
⌋
∑
d
=
1
m
i
n
(
⌊
n
p
⌋
,
⌊
m
p
⌋
)
[
d
∣
i
]
[
d
∣
j
]
μ
(
d
)
=\sum_{p\in prime}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}\sum_{d=1}^{min(\lfloor\frac{n}{p}\rfloor,\lfloor\frac{m}{p}\rfloor)}[d|i][d|j]\mu(d)
=p∈prime∑i=1∑⌊pn⌋j=1∑⌊pm⌋d=1∑min(⌊pn⌋,⌊pm⌋)[d∣i][d∣j]μ(d)
=
∑
p
∈
p
r
i
m
e
∑
d
=
1
m
i
n
(
⌊
n
p
⌋
,
⌊
m
p
⌋
)
μ
(
d
)
∑
i
=
1
⌊
n
p
⌋
[
d
∣
i
]
∑
j
=
1
⌊
m
p
⌋
[
d
∣
j
]
=\sum_{p\in prime}\sum_{d=1}^{min(\lfloor\frac{n}{p}\rfloor,\lfloor\frac{m}{p}\rfloor)}\mu(d)\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}[d|i]\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}[d|j]
=p∈prime∑d=1∑min(⌊pn⌋,⌊pm⌋)μ(d)i=1∑⌊pn⌋[d∣i]j=1∑⌊pm⌋[d∣j]
=
∑
p
∈
p
r
i
m
e
∑
d
=
1
m
i
n
(
⌊
n
p
⌋
,
⌊
m
p
⌋
)
μ
(
d
)
⌊
n
d
p
⌋
⌊
m
d
p
⌋
=\sum_{p\in prime}\sum_{d=1}^{min(\lfloor\frac{n}{p}\rfloor,\lfloor\frac{m}{p}\rfloor)}\mu(d)\lfloor\frac{n}{dp}\rfloor\lfloor\frac{m}{dp}\rfloor
=p∈prime∑d=1∑min(⌊pn⌋,⌊pm⌋)μ(d)⌊dpn⌋⌊dpm⌋
一般到这里就可以,因为没有套外面的枚举素数,所以本题要继续推555
令
T
=
d
p
T=dp
T=dp,
n
<
m
n<m
n<m
=
∑
p
∈
p
r
i
m
e
∑
T
=
1
,
p
∣
T
n
μ
(
T
p
)
⌊
n
T
⌋
⌊
m
T
⌋
=\sum_{p\in prime}\sum_{T=1,p|T}^{n}\mu(\frac{T}{p})\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor
=p∈prime∑T=1,p∣T∑nμ(pT)⌊Tn⌋⌊Tm⌋
换一下枚举方式,
=
∑
T
=
1
n
⌊
n
T
⌋
⌊
m
T
⌋
∑
p
∈
p
r
i
m
e
,
p
∣
T
μ
(
T
p
)
=\sum_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum_{p\in prime,p|T}\mu(\frac{T}{p})
=T=1∑n⌊Tn⌋⌊Tm⌋p∈prime,p∣T∑μ(pT)
设
f
(
T
)
=
∑
p
∈
p
r
i
m
e
,
p
∣
T
μ
(
T
p
)
f(T)=\sum_{p\in prime,p|T}\mu(\frac{T}{p})
f(T)=∑p∈prime,p∣Tμ(pT),
=
∑
T
=
1
n
f
(
T
)
⌊
n
T
⌋
⌊
m
T
⌋
=\sum_{T=1}^nf(T)\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor
=T=1∑nf(T)⌊Tn⌋⌊Tm⌋
f
f
f是可以用线性筛求的,
接下来求一下
f
f
f的前缀和,数论分块求即可。
#include <cstdio>
#define min(a,b) ((a<b)?(a):(b))
#define ll long long
using namespace std;
const int N = 1e7+10, lnN = 15;
int p[N], prime[N/lnN], f[N];
int mu[N], cnt;
void primes(ll n){
mu[1] = p[1] = 1;
for(register int i = 2; i <= n; i++){
if(!p[i]) p[i] = prime[++cnt] = (ll)i, f[i] = 1, mu[i] = -1;
for(register int j = 1; j <= cnt && prime[j] <= n/i; j++){
ll x = 1ll*i*prime[j];
p[x] = prime[j];
if(i%prime[j] == 0){
f[x] = mu[i];mu[x] = 0;
break;
}
mu[x] = -mu[i];f[x] = mu[i]-f[i];
}
}
for(register int i = 2; i <= n; i++) f[i] += f[i-1];
}
ll solve(int k, int a, int b){
ll ans = 0;
for(register ll l = 1, r; l <= k && a/l && b/l; l = r+1){
ll r1 = min(k, a/(a/l));
ll r2 = min(k, b/(b/l));
r = min(r1, r2);
ans += 1ll*(f[r]-f[l-1])*(a/l)*(b/l);
}
return ans;
}
int main(){
int t;
primes(1e7);
scanf("%d", &t);
while(t--){
int n, m;
scanf("%d%d", &n, &m);
if(n>m) n^=m^=n^=m;
printf("%lld\n", solve(n, n, m));
}
return 0;
}