如果不会线性筛素数的话,建议先看这篇博客 了解一下线性筛素数。
记f ( n ) = ∑ i = 1 n gcd ( i , n ) f(n)=\sum_{i=1}^{n}\gcd(i,n) f ( n ) = ∑ i = 1 n g cd( i , n )
容易证明f ( n ) f(n) f ( n ) 一定是积性函数
即f ( p 1 t 1 p 2 t 2 ⋯ p k t k ) = f ( p 1 t 1 ) f ( p 2 t 2 ) ⋯ f ( p k t k ) f(p_1^{t_1} p_2^{t_2}\cdots p_k^{t_k}) =f(p_1^{t_1} )f(p_2^{t_2} ) \cdots f(p_k^{t_k} ) f ( p 1 t 1 p 2 t 2 ⋯ p k t k ) = f ( p 1 t 1 ) f ( p 2 t 2 ) ⋯ f ( p k t k )
又因为
f ( n ) = ∑ i = 1 n gcd ( i , n ) = ∑ d ∣ n d ∗ ∑ i n [ gcd ( i , n ) = d ] = ∑ d ∣ n d ∗ ϕ ( n d )
\begin{aligned}
f(n)&=\sum_{i=1}^{n} \gcd(i,n)\\
&=\sum_{d|n} d* \sum_i^n [\gcd(i,n)=d]\\
&= \sum_{d|n} d *\phi( \frac{n}{d} )\\
\end{aligned}
f ( n ) = i = 1 ∑ n g cd( i , n ) = d ∣ n ∑ d ∗ i ∑ n [ g cd( i , n ) = d ] = d ∣ n ∑ d ∗ ϕ ( d n )
于是f ( p t ) = ∑ i = 1 p t gcd ( i , p t ) = ( ∑ i = 0 t − 1 p i ∗ ϕ ( p t − i ) ) + p t = ( ∑ i = 0 t − 1 p i ∗ ( p t − i − 1 ) ∗ ( p − 1 ) ) + p t = ( ∑ i = 0 t − 1 p t − p t − 1 ) ) + p t = ( t + 1 ) ∗ p t − t ∗ p t − 1
\begin{aligned}
f(p^t) &=\sum_{i=1}^{p^t} \gcd(i,p^t)\\
&= \left( \sum_{i=0}^{t-1} p^i*\phi(p^{t-i}) \right) + p^t\\
&=\left( \sum_{i=0}^{t-1} p^i*(p^{t-i-1})*(p-1) \right) + p^t\\
&=\left( \sum_{i=0}^{t-1} p^{t}-p^{t-1}) \right) + p^t\\
&=(t+1)*p^t-t*p^{t-1}
\end{aligned}
f ( p t ) = i = 1 ∑ p t g cd( i , p t ) = ( i = 0 ∑ t − 1 p i ∗ ϕ ( p t − i ) ) + p t = ( i = 0 ∑ t − 1 p i ∗ ( p t − i − 1 ) ∗ ( p − 1 ) ) + p t = ( i = 0 ∑ t − 1 p t − p t − 1 ) ) + p t = ( t + 1 ) ∗ p t − t ∗ p t − 1
所以:
记g ( n ) g(n) g ( n ) 为n的所有质因子中所有最小质因子的乘积
记t ( n ) t(n) t ( n ) 为n的所有质因子中所有最小质因子的数目
1、当 n n n 是质数时,g ( n ) = n , f ( n ) = 2 ∗ n − 1 , t ( n ) = 1 g(n)=n\quad , \quad f(n)=2*n-1\quad , \quad t(n)=1 g ( n ) = n , f ( n ) = 2 ∗ n − 1 , t ( n ) = 1
对于 2和3 设 d = n p d=\frac{n}{p} d = p n 其中 p p p 为 n n n 的最小质因子
2、当 p p p 是 d d d 的某个质因子时, 则 g ( n ) = g ( d ) ∗ p , f ( n ) = f ( d ) ∗ ( ( t ( n ) + 1 ) ∗ g ( n ) − ( t ( d ) + 1 ) ∗ g ( d ) ) f ( g ( d ) ) , t ( n ) = t ( d ) + 1 g(n)=g(d)*p\quad, \quad f(n)=\frac{f(d)*((t(n)+1)*g(n)-(t(d)+1)*g(d))}{f(g(d))}\quad, \quad t(n)=t(d)+1 g ( n ) = g ( d ) ∗ p , f ( n ) = f ( g ( d ) ) f ( d ) ∗ ( ( t ( n ) + 1 ) ∗ g ( n ) − ( t ( d ) + 1 ) ∗ g ( d ) ) , t ( n ) = t ( d ) + 1
3、当 p p p 与 d d d 互质时,g ( n ) = n , f ( n ) = f ( d ) ∗ f ( p ) , t ( n ) = 1 g(n)=n\quad,\quad f(n)=f(d)*f(p)\quad,\quad t(n)=1 g ( n ) = n , f ( n ) = f ( d ) ∗ f ( p ) , t ( n ) = 1
good luck and have fun!!! 附上代码:
int f[ MAXN] , prime[ MAXN] , d[ MAXN] , dd[ MAXN] ;
\\dd表示g函数
\\d表示t函数
void get_it ( int n)
{
memset ( prime, 0 , sizeof prime) ;
f[ 1 ] = mu[ 1 ] = 1 ;
for ( int i= 2 ; i<= n; i++ ) {
if ( ! prime[ i] ) {
prime[ ++ prime[ 0 ] ] = i;
dd[ i] = i;
d[ i] = 1 ;
f[ i] = 2 * i- 1 ;
}
for ( int j= 1 ; j<= prime[ 0 ] && prime[ j] <= n/ i; j++ ) {
prime[ i* prime[ j] ] = 1 ;
if ( i% prime[ j] == 0 ) {
d[ i* prime[ j] ] = d[ i] + 1 ;
dd[ i* prime[ j] ] = dd[ i] * prime[ j] ;
f[ i* prime[ j] ] = f[ i] / f[ dd[ i] ] * ( 1ll * ( d[ i] + 2 ) * dd[ i* prime[ j] ] - 1ll * d[ i* prime[ j] ] * dd[ i] ) ;
break ;
}
d[ i* prime[ j] ] = 1 ;
dd[ i* prime[ j] ] = prime[ j] ;
f[ i* prime[ j] ] = f[ i] * ( 2 * prime[ j] - 1 ) ;
}
}
}