1. 程式人生 > >CPOJ 2018.10.29提高測試 Sequence

CPOJ 2018.10.29提高測試 Sequence

在這裡插入圖片描述 考慮兩個數p1k1p_1^{k_1},p2k2p_2^{k_2} 設由他們各自的因子構成的數列為a1ia_{1i}a2ia_{2i},將bb質因數分解後pip_i的冪次為did_i 顯然 gcd(a11,a12,,a1n)=min(g1,p1d1)gcd(a21,a22,,a2n)=min(g2,p2d2)gcd(a11,a12,,a1n,a21,a22,,a2n)=1 gcd(a_{11},a_{12},\ldots,a_{1n})=min(g_1,p_1^{d_1})\\ gcd(a_{21},a_{22},\ldots,a_{2n})=min(g_2,p_2^{d_2})\\ gcd(a_{11},a_{12},\ldots,a_{1n},a_{21},a_{22},\ldots,a_{2n})=1

這兩個數的乘積為p1k1p2k2p_1^{k_1}p_2^{k_2} 由它的因子構成的數列為bib_i 顯然對於bib_i可以唯一分解為a1ia2ia_{1i}a_{2i} 那麼 gcd(bi)=gcd(a1ia2i)=g1g2 gcd(b_i)=gcd(a_{1i}a_{2i})=g_1g_2 因此fif_i是積性函式,把所有質數的冪次算一下,剩下的線性篩搞一搞就行 fpik={j=0kpij((kj+1)n(kj)n)kdi(j=0dipij((dj+1)n(dj)n)+pidi(dj)nk>di f_{p_i^k}= \begin{cases} \sum_{j=0}^k p_i^j((k-j+1)^n-(k-j)^n) &\text{$k\leq d_i$}\\ (\sum_{j=0}^{d_i} p_i^j((d-j+1)^n-(d-j)^n)+p_i^{d_i}(d-j)^n &\text{$k>d_i$} \\ \end{cases}
Code