# BZOJ4407: Wrath of the gods enhanced version (Mobius inversion linear screen)

## Description

Give N,M,K

I feel so confused. I look at a lot of changes, but I don't know where to learn. There's no one to do one question all morning,,

First of all, according to the routine, it should evolve to this formula

$$ans = \sum_{d = 1}^n d^k \sum_{i = 1}^{\frac{n}{d}} \frac{n}{di} \frac{m}{di} \mu(i)$$

So we can calculate $O(n)$

Move on to consider the contribution of $\ frac{n}{di} \frac{m}{di}$to the answer

Set $T = id$

$ans = \sum_{T = 1}^n \frac{n}{T} \frac{m}{T} \sum_{d \mid T} ^ T d^k \mu(\frac{T}{d})$

The latter one is in the form of Dirichlet convolution, which is obviously a product function and can be screened directly

And then I was here for an hour,,

Set $H (T) = \ sum {D \ mid t} ^ t d ^ k \ mu (\ frac {t} {D})$

So when $T = p^a$, only $\ frac{T}{d} = 1$or $\ frac{T}{d} = p$, and $\ mu(\frac{T}{d})$is not $0$

Then expand the formula to $H(p^{a + 1}) = H(p^a) * (p^k)$

// luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
const int MAXN = 5 * 1e6 + 10, mod = 1e9 + 7;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int T, K;
int prime[MAXN], vis[MAXN], tot, mu[MAXN];
LL H[MAXN], low[MAXN];
LL fastpow(LL a, LL p) {
LL base = 1;
while(p) {
if(p & 1) base = (base * a) % mod;
a = (a * a) % mod; p >>= 1;
}
return base;
}
void GetH(int N) {
vis[1] = H[1] = mu[1] = low[1] = 1;
for(int i = 2; i <= N; i++) {
if(!vis[i]) prime[++tot] = i, mu[i] = -1, H[i] = (-1 + fastpow(i, K) + mod) % mod, low[i] = i;
for(int j = 1; j <= tot && i * prime[j] <= N; j++) {
vis[i * prime[j]] = 1;
if(!(i % prime[j])) {
mu[i * prime[j]] = 0; low[i * prime[j]] = (low[i] * prime[j]) % mod;
if(low[i] == i)
//H[i * prime[j]] = (H[i] + fastpow((i * prime[j]), K)) % mod;
H[i * prime[j]] = H[i] * (fastpow(prime[j], K)) % mod;
else H[i * prime[j]] = H[i / low[i]] * H[prime[j] * low[i]] % mod;
break;
}
mu[i * prime[j]] = mu[i] * mu[prime[j]] % mod;
H[i * prime[j]] = H[i] * H[prime[j]] % mod;
low[i * prime[j]] = prime[j] % mod;
}
}
for(int i = 2; i <= N; i++) H[i] = (H[i] + H[i - 1] + mod) % mod;
}
int main() {
GetH(5000001);
while(T--) {
int N = read(), M = read(), last;
LL ans = 0;
if(N > M) swap(N, M);
for(int T = 1; T <= N; T = last + 1) {
last = min(N / (N / T), M / (M / T));
ans = (ans + (1ll * (N / T) * (M / T) % mod) * (H[last] - H[T - 1] + mod)) % mod;
}
printf("%lld\n", ans % mod);
}
return 0;
}
/*
2 5000000
7 8
123 456
4999999 5000000
*/

Tags: C++

Posted on Sun, 09 Feb 2020 15:19:32 -0500 by ScubaKing22