∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} lcm(i, j) i=1∑nj=1∑mlcm(i,j)
= ∑ i = 1 n ∑ j = 1 m i j g c d ( i , j ) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \frac{ij}{gcd(i, j)} =i=1∑nj=1∑mgcd(i,j)ij
= ∑ d = 1 n 1 d ∑ i = 1 n ∑ j m i j ( g c d ( i , j ) = = d ) = \sum_{d = 1} ^{n} \frac{1}{d}\sum_{i = 1} ^{n} \sum_{j} ^{m} ij(gcd(i, j) == d) =d=1∑nd1i=1∑nj∑mij(gcd(i,j)==d)
= ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 m d i j ( g c d ( i , j ) = = 1 ) =\sum_{d = 1} ^{n} d \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}}ij(gcd(i, j) == 1) =d=1∑ndi=1∑dnj=1∑dmij(gcd(i,j)==1)
= ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 m d i j ∑ k ∣ g c d ( i , j ) μ ( k ) =\sum_{d = 1} ^{n} d \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}}ij\sum_{k \mid gcd(i, j)} \mu(k) =d=1∑ndi=1∑dnj=1∑dmijk∣gcd(i,j)∑μ(k)
= ∑ d = 1 n d ∑ k = 1 n d k 2 μ ( k ) ∑ i = 1 n k d i ∑ j = 1 m k d j = \sum_{d = 1} ^{n} d \sum_{k = 1} ^{\frac{n}{d}}k ^ 2 \mu(k) \sum_{i = 1} ^{\frac{n}{kd}}i \sum_{j = 1} ^{\frac{m}{kd}}j =d=1∑ndk=1∑dnk2μ(k)i=1∑kdnij=1∑kdmj
= ∑ d = 1 n d ∑ k = 1 n d k 2 μ ( k ) ⌊ n k d ⌋ ( ⌊ n k d ⌋ + 1 ) 2 ⌊ m k d ⌋ ( ⌊ m k d ⌋ + 1 ) 2 = \sum_{d = 1} ^{n} d\sum_{k = 1} ^{\frac{n}{d}}k ^ 2 \mu(k)\frac{\lfloor\frac{n}{kd}\rfloor(\lfloor\frac{n}{kd}\rfloor + 1)}{2}\frac{\lfloor\frac{m}{kd}\rfloor(\lfloor\frac{m}{kd}\rfloor + 1)}{2} =d=1∑ndk=1∑dnk2μ(k)2⌊kdn⌋(⌊kdn⌋+1)2⌊kdm⌋(⌊kdm⌋+1)
所以我们只要预处理出 s u m k = 1 n d k 2 μ ( k ) sum_{k = 1} ^{\frac{n}{d}}k ^ 2 \mu(k) sumk=1dnk2μ(k),然后再进行两次分块即可。
整体复杂度 n n = n \sqrt n \sqrt n = n nn=n还是线性 O ( n ) O(n) O(n)的。
/*
Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include
#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N = 1e7 + 10, mod = 20101009;
bool st[N];
vector<int> prime;
int n, m;
ll mu[N];
void mobius() {
st[0] = st[1] = mu[1] = 1;
for(int i = 2; i < N; i++) {
if(!st[i]) {
prime.pb(i);
mu[i] = -1;
}
for(int j = 0; j < prime.size() && i * prime[j] < N; j++) {
st[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
mu[i * prime[j]] = -mu[i];
}
}
for(int i = 1; i < N; i++) {
mu[i] = (mu[i - 1] + mu[i] * i % mod * i % mod + mod) % mod;
}
}
ll calc1(ll l, ll r) {
return (l + r) * (r - l + 1) / 2 % mod;
}
ll calc2(ll n, ll m) {
ll ans = 0;
if(n > m) swap(n, m);
for(ll l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans = (ans + (mu[r] - mu[l - 1] + mod) % mod * calc1(1, n / l) % mod * calc1(1, m / l) % mod + mod) % mod;
}
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
mobius();
ll n = read(), m = read(), ans = 0;
if(n > m) swap(n, m);
for(ll l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans = (ans + calc1(l, r) * calc2(n / l, m / l) % mod) % mod;
}
printf("%lld\n", ans);
return 0;
}