【UVA 11426】gcd之和 (改编)
题面
\(\sum_{i=1}^{n}\sum_{j=1}^m\gcd(i,j)\mod998244353\)
\(n,m<=10^7\)
Sol
简单的一道莫比乌斯反演题
\(原式=\sum_{d=1}^{n}d*\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i, j)==1]\)
\(设f(i) = \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i, j)==1]\)
\(g(i) = \sum_{i|d} f(d) = \lfloor\frac{\lfloor\frac{n}{d}\rfloor}{i}\rfloor\lfloor\frac{\lfloor\frac{m}{d}\rfloor}{j}\rfloor\)
莫比乌斯反演求出f,用两个数论分块就好了
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e7 + 1), MOD(998244353);
IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
}
int prime[_], mu[_], num, s[_];
bool isprime[_];
IL void Prepare(){
isprime[1] = 1; s[1] = mu[1] = 1;
for(RG int i = 2; i < _; ++i){
if(!isprime[i]) prime[++num] = i, mu[i] = -1;
for(RG int j = 1; j <= num && i * prime[j] < _; ++j){
isprime[i * prime[j]] = 1;
if(i % prime[j]) mu[i * prime[j]] = -mu[i];
else{ mu[i * prime[j]] = 0; break; }
}
(mu[i] += mu[i - 1]) %= MOD; s[i] = (s[i - 1] + i) % MOD;
}
}
IL int Calc(RG ll n, RG ll m){
RG ll f = 0, g;
for(RG ll i = 1, j; i <= n; i = j + 1){
j = min(n / (n / i), m / (m / i));
g = 1LL * (n / i) * (m / i) % MOD;
(f += 1LL * (mu[j] - mu[i - 1] + MOD) % MOD * g % MOD) %= MOD;
}
return f;
}
int main(RG int argc, RG char *argv[]){
Prepare();
RG int n = Read(), m = Read(); RG ll ans = 0;
if(n > m) swap(n, m);
for(RG ll d = 1, j; d <= n; d = j + 1){
j = min(n / (n / d), m / (m / d));
(ans += 1LL * (s[j] - s[d - 1] + MOD) % MOD * Calc(n / d, m / d) % MOD) %= MOD;
}
printf("%lld\n", ans);
return 0;
}