给定 n , m n, m n,m,求 ∏ X ∈ [ 1 , m ] n l c m ( X 1 , ⋯ , X n ) gcd ( X 1 , ⋯ , X n ) \prod_{X\in [1,m]^n}\mathrm{lcm}(X_1,\cdots,X_n)^{\gcd(X_1,\cdots,X_n)} X∈[1,m]n∏lcm(X1,⋯,Xn)gcd(X1,⋯,Xn)
X ∈ [ 1 , m ] n X\in [1,m]^n X∈[1,m]n表示 X X X取遍所有长度为 n n n的序列,满足 X i X_i Xi为 1 1 1到 m m m之间的整数。 n ≤ 1 0 9 , m ≤ 1 0 8 n\le 10^9,m\le 10^8 n≤109,m≤108
先考虑这个问题的弱化版:求
S ( m ) = ∏ X ∈ [ 1 , m ] n l c m ( X 1 , ⋯ , X n ) S(m)=\prod_{X\in [1,m]^n}\mathrm{lcm}(X_1,\cdots,X_n) S(m)=X∈[1,m]n∏lcm(X1,⋯,Xn)
第一种算法是
S ( m ) = ∏ p ∈ P , p k ≤ m p m n − ( m − ⌊ m p k ⌋ ) n S(m)=\prod_{p\in \mathbb{P},p^k\le m}p^{m^n-(m-\lfloor\frac{m}{p^k}\rfloor)^n} S(m)=p∈P,pk≤m∏pmn−(m−⌊pkm⌋)n
可以预处理
f ( n ) = { p n = p k , p ∈ P 1 o t h e r w i s e f(n)=\begin{cases} p & n = p^k,p\in \mathbb{P}\\ 1 & otherwise \end{cases} f(n)={p1n=pk,p∈Potherwise
的前缀积,然后通过分块 O ( m log P ) O(\sqrt m\log P) O(m logP)求出答案。
第二种算法是通过min-max反演得到
l c m ( X 1 , ⋯ , X n ) = ∏ k = 1 n ∏ 1 ≤ y 1 < ⋯ < y k ≤ n gcd ( X y 1 , ⋯ , X y k ) ( − 1 ) k − 1 \mathrm{lcm}(X_1,\cdots,X_n)=\prod_{k=1}^n\prod_{1\le y_1<\cdots <y_k\le n}\gcd(X_{y_1},\cdots,X_{y_k})^{(-1)^{k-1}} lcm(X1,⋯,Xn)=k=1∏n1≤y1<⋯<yk≤n∏gcd(Xy1,⋯,Xyk)(−1)k−1
如果令
g ( p ) = ∑ k = 1 n ∑ 1 ≤ y 1 < ⋯ < y k ≤ n , 1 ≤ X y i ≤ p , 1 ≤ X i ≤ m [ gcd ( X y 1 , ⋯ , X y k ) = 1 ] ( − 1 ) k − 1 g(p)=\sum_{k=1}^n\sum_{1\le y_1<\cdots <y_k\le n,1\le X_{y_i}\le p,1\le X_i\le m}[\gcd(X_{y_1},\cdots,X_{y_k})=1](-1)^{k-1} g(p)=k=1∑n1≤y1<⋯<yk≤n,1≤Xyi≤p,1≤Xi≤m∑[gcd(Xy1,⋯,Xyk)=1](−1)k−1
那么
S ( m ) = ∏ k = 1 m d g ( ⌊ m d ⌋ ) S(m)=\prod_{k=1}^md^{g(\lfloor\frac{m}{d}\rfloor)} S(m)=k=1∏mdg(⌊dm⌋)
注意到 g g g只有 O ( m ) O(\sqrt m) O(m )种取值,只要求出这些 g g g,就能够通过分块 O ( m log P ) O(\sqrt m\log P) O(m logP)求出答案。问题在于如何求 g g g。令
g t ( p ) = ∑ k = 1 n ∑ 1 ≤ y 1 < ⋯ < y k ≤ n , 1 ≤ X y i ≤ p , 1 ≤ X i ≤ m [ gcd ( X y 1 , ⋯ , X y k ) = t ] ( − 1 ) k − 1 g_t(p)=\sum_{k=1}^n\sum_{1\le y_1<\cdots <y_k\le n,1\le X_{y_i}\le p,1\le X_i\le m}[\gcd(X_{y_1},\cdots,X_{y_k})=t](-1)^{k-1} gt(p)=k=1∑n1≤y1<⋯<yk≤n,1≤Xyi≤p,1≤Xi≤m∑[gcd(Xy1,⋯,Xyk)=t](−1)k−1
且
A ( p ) = ∑ t = 1 p g t ( p ) = ∑ k = 1 n ( n k ) m n − k p k ( − 1 ) k − 1 A(p)=\sum_{t=1}^pg_t(p)=\sum_{k=1}^n\binom{n}{k}m^{n-k}p^k(-1)^{k-1} A(p)=t=1∑pgt(p)=k=1∑n(kn)mn−kpk(−1)k−1
= m n − ∑ k = 0 n ( n k ) m n − k ( − p ) k = m n − ( m − p ) n =m^n-\sum_{k=0}^n\binom{n}{k}m^{n-k}(-p)^k=m^n-(m-p)^n =mn−k=0∑n(kn)mn−k(−p)k=mn−(m−p)n
又注意到 g t ( p ) = g ( ⌊ p t ⌋ ) g_t(p)=g(\lfloor\frac{p}{t}\rfloor) gt(p)=g(⌊tp⌋),因此
g ( p ) = g 1 ( p ) = m n − ( m − p ) n − ∑ t = 2 p g ( ⌊ p t ⌋ ) g(p)=g_1(p)=m^n-(m-p)^n-\sum_{t=2}^pg(\lfloor\frac{p}{t}\rfloor) g(p)=g1(p)=mn−(m−p)n−t=2∑pg(⌊tp⌋)
用上式计算 g g g,复杂度和杜教筛类似,都是 O ( m 3 4 ) O(m^{\frac{3}{4}}) O(m43)。
回到原问题,类似可设
G ( p ) = ∏ X ∈ [ 1 , p ] n l c m ( X 1 , ⋯ , X n ) [ gcd ( X 1 , ⋯ , X n ) = 1 ] G(p)=\prod_{X\in [1,p]^n}\mathrm{lcm}(X_1,\cdots,X_n)^{[\gcd(X_1,\cdots,X_n)=1]} G(p)=X∈[1,p]n∏lcm(X1,⋯,Xn)[gcd(X1,⋯,Xn)=1]
G 1 ( p ) = ∑ X ∈ [ 1 , p ] n [ gcd ( X 1 , ⋯ , X n ) = 1 ] G1(p)=\sum_{X\in [1,p]^n}[\gcd(X_1,\cdots,X_n)=1] G1(p)=X∈[1,p]n∑[gcd(X1,⋯,Xn)=1]
那么
a n s = ∏ d = 1 m G ( ⌊ m d ⌋ ) d ∗ ( d d ) G 1 ( ⌊ m d ⌋ ) ans=\prod_{d=1}^m G(\lfloor\frac{m}{d}\rfloor)^d*(d^d)^{G1(\lfloor\frac{m}{d}\rfloor)} ans=d=1∏mG(⌊dm⌋)d∗(dd)G1(⌊dm⌋)
如果求出了 G G G和 G 1 G1 G1以及 d d d^d dd的前缀积,就可以分块来求 a n s ans ans了。求 d d d^d dd的前缀积可以通过
∏ i = 1 n i i = ∏ i = 1 n ( i ! ( i − 1 ) ! ) i = ( n ! ) n ∏ i = 1 n − 1 i ! \prod_{i=1}^ni^i=\prod_{i=1}^n\Big(\frac{i!}{(i-1)!}\Big)^i=\frac{(n!)^n}{\prod_{i=1}^{n-1}i!} i=1∏nii=i=1∏n((i−1)!i!)i=∏i=1n−1i!(n!)n
因此只需要 O ( m ) O(m) O(m)预处理阶乘的前缀积。
用求 g g g相同的方式求求 G G G和 G 1 G1 G1。令
G 1 t ( p ) = ∑ X ∈ [ 1 , p ] n [ gcd ( X 1 , ⋯ , X n ) = t ] G1_t(p)=\sum_{X\in [1,p]^n}[\gcd(X_1,\cdots,X_n)=t] G1t(p)=X∈[1,p]n∑[gcd(X1,⋯,Xn)=t]
那么
∑ t = 1 p G 1 t ( p ) = p n ⇒ G 1 ( p ) = p n − ∑ t = 2 p G 1 ( ⌊ p t ⌋ ) \sum_{t=1}^pG1_t(p)=p^n\Rightarrow G1(p)=p^n-\sum_{t=2}^pG1(\lfloor\frac{p}{t}\rfloor) t=1∑pG1t(p)=pn⇒G1(p)=pn−t=2∑pG1(⌊tp⌋)
令
G t ( p ) = ∏ X ∈ [ 1 , p ] n l c m ( X 1 , ⋯ , X n ) gcd ( X 1 , ⋯ , X n ) = t G_t(p)=\prod_{X\in [1,p]^n}\mathrm{lcm}(X_1,\cdots,X_n)^{\gcd(X_1,\cdots,X_n)=t} Gt(p)=X∈[1,p]n∏lcm(X1,⋯,Xn)gcd(X1,⋯,Xn)=t
那么
∏ t = 1 p G t ( p ) = ∏ X ∈ [ 1 , p ] n l c m ( X 1 , ⋯ , X n ) = S ( p ) \prod_{t=1}^pG_t(p)=\prod_{X\in [1,p]^n}\mathrm{lcm}(X_1,\cdots,X_n)=S(p) t=1∏pGt(p)=X∈[1,p]n∏lcm(X1,⋯,Xn)=S(p)
得到
G ( p ) = S ( p ) ∗ ( ∏ t = 2 p G ( ⌊ p t ⌋ ) ∗ t G 1 ( ⌊ p t ⌋ ) ) − 1 G(p)=S(p)*\Big(\prod_{t=2}^pG(\lfloor\frac{p}{t}\rfloor)*t^{G1(\lfloor\frac{p}{t}\rfloor)}\Big)^{-1} G(p)=S(p)∗(t=2∏pG(⌊tp⌋)∗tG1(⌊tp⌋))−1
求 S ( p ) S(p) S(p)的时候,当 p p p较小时用第一种算法,较大时用第二种算法。