# DZY Loves Math

### DZY Loves Math

$\sum_{i=1}^n\sum_{j=1}^m f(\gcd(i,j))$

$\sum_{i=1}^n \sum_{j=1}^m \sum_{d=1}^n [gcd(i,j) = d] f(d)$

$\sum_{d=1}^n f(d) \sum_{i=1}^n \sum_{j=1}^m [\gcd(i,j) = d]$

$\sum_{d=1}^n f(d) \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor \frac m d \rfloor } [\gcd(i,j) = 1]$

$\sum_{d=1}^n f(d)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor \frac m d \rfloor } \sum_{k | \gcd(i,j)} \mu(k)$

$\sum_{d=1} ^ n f(d) \sum_{k=1}^{\lfloor \frac n d \rfloor} \lfloor \frac n {dk} \rfloor \lfloor \frac n {dk} \rfloor \mu(k)$

$\sum_{T=1}^n \lfloor \frac n T \rfloor \lfloor \frac m T \rfloor \sum_{d|T} f(d) \mu(\frac T k)$

$\sum_{T=1}^{n} \lfloor \frac n T \rfloor \lfloor \frac m T \rfloor (f \times \mu)(T)$

#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "vector"
#include "map"
using namespace std;
#define MAXN 10000006
typedef long long ll;
int n , m;
int pri[MAXN] , en , mm[MAXN]; // mx : 最大次数 mm : 最小质数的次数
long long mx[MAXN];
void sieve( ) {
for( int i = 2 ; i < MAXN ; ++ i ) {
if( !pri[i] ) pri[++ en] = i , mx[i] = mm[i] = 1;
for( int j = 1 ; j <= en && pri[j] * i < MAXN ; ++ j ) {
pri[i * pri[j]] = 1;
if( i % pri[j] == 0 ) {
mm[i * pri[j]] = mm[i] + 1 , mx[i * pri[j]] = max( mx[i] , mm[i] + 1ll );
break;
}
mm[i * pri[j]] = 1 , mx[i * pri[j]] = max( mx[i] , 1ll );
}
}
for( int i = en ; i ; -- i )
for( int j = ( MAXN - 1 ) / pri[i] ; j ; -- j ) {
mx[j * pri[i]] -= mx[j];
}
for( int i = 1 ; i < MAXN ; ++ i ) mx[i] += mx[i - 1];
}
long long ans;
int main() {
sieve();
int T;cin >> T;
while( T-- ) {
scanf("%d%d",&n,&m); ans = 0;
int x = min( n , m );
for( int l = 1 , r ; l <= x ; l = r + 1) {
r = min( n / ( n / l ) , m / ( m / l ) );
ans += 1ll * ( mx[r] - mx[l - 1] ) * ( n / l ) * ( m / l );
}
printf("%lld\n",ans);
}
}

posted @ 2020-02-25 20:27  yijan  阅读(175)  评论(0编辑  收藏  举报