# BZOJ4407: 于神之怒加强版(莫比乌斯反演 线性筛)

## Description

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

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

// luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
const int MAXN = 5 * 1e6 + 10, mod = 1e9 + 7;
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--) {
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
*/

posted @ 2018-07-19 11:37  自为风月马前卒  阅读(341)  评论(0编辑  收藏  举报