BZOJ4407: 于神之怒加强版(莫比乌斯反演 线性筛)
程序员文章站
2023-10-18 08:15:15
Description 给下N,M,K.求 给下N,M,K.求 感觉好迷茫啊,很多变换看的一脸懵逼却又不知道去哪里学。一道题做一上午也是没谁了,, 首先按照套路反演化到最后应该是这个式子 $$ans = \sum_{d = 1}^n d^k \sum_{i = 1}^{\frac{n}{d}} \f ......
Description
给下N,M,K.求
感觉好迷茫啊,很多变换看的一脸懵逼却又不知道去哪里学。一道题做一上午也是没谁了,,
首先按照套路反演化到最后应该是这个式子
$$ans = \sum_{d = 1}^n d^k \sum_{i = 1}^{\frac{n}{d}} \frac{n}{di} \frac{m}{di} \mu(i)$$
这样就可以$O(n)$计算
继续往下推,考虑$\frac{n}{di} \frac{m}{di}$对答案的贡献
设$T = id$
$ans = \sum_{T = 1}^n \frac{n}{T} \frac{m}{T} \sum_{d \mid T} ^ T d^k \mu(\frac{T}{d})$
后面那一坨是狄利克雷卷积的形式,显然是积性函数,可以直接筛
然后我在这里懵了一个小时,,
设$H(T) = \sum_{d \mid T} ^ T d^k \mu(\frac{T}{d})$
那么当$T = p^a$式,上面的式子中只有$\frac{T}{d} = 1$或$\frac{T}{d} = p$式,$\mu(\frac{T}{d})$才不为$0$
那么把式子展开$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() { T = read(); K = read(); 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 */
上一篇: 双节来临勿忘保肝 疲劳过度让身心休息休息
下一篇: 用PS制作温馨浪漫的情人节电子贺卡