标签:color lse 素数 play exists int ora inline exist
保证每次只被自己最小的质因数筛到。。
void yych()
{
for(int i = 2; i <= maxn; i ++)
{
if(!vis[i]) prime[++tot] = i;
for(int j = 1; j <= tot&&i * prime[j] <= maxn; j ++)
{
vis[i * prime[j]] = 1;
if(i%prime[j] == 0) break;
}
}
欧拉函数 \(phi(i)\) 为小于i 的正整数中与\(i\)互质的数的个数, 然后有公式
? 然后考虑怎么筛, 还是分三种情况
1.\(i\)是质数 则显然 \(\color{green}{phi(i) =(i-1)}\)
2.\(i\) 能整除质数\(p\), 则说明\(i\) 中含有\(p\) 这个质数, 则后面的质数不会改变,只是在本身上乘上\(p\);
\(\phi(i) = i*(1-\dfrac{1}{p_1})*(1-\dfrac{1}{p_2})*...*(1-\dfrac{1}{p_k})\)
\(\phi(i*p) = i*p*(1-\dfrac{1}{p_1})*(1-\dfrac{1}{p_2})*...*(1-\dfrac{1}{p_k})\)
So, \(\color{green}{phi(i*p) = phi(i)*p}\)
3.\(i\) 不能整除质数\(p\), 说明\(i\)中不含\(p\)这个因数, 也就是原来多乘了\(p*(1-\dfrac{1}{p})\) , 化简得\((p-1)\);
\(\phi(i) = i*(1-\dfrac{1}{p_1})*(1-\dfrac{1}{p_2})*...*(1-\dfrac{1}{p_k})\)
\(\phi(i*p) = i*p*(1-\dfrac{1}{p})*(1-\dfrac{1}{p_1})*(1-\dfrac{1}{p_2})*...*(1-\dfrac{1}{p_k})\)
? \(= i*(p-1)*(1-\dfrac{1}{p_1})*(1-\dfrac{1}{p_2})*...*(1-\dfrac{1}{p_k})\)
So, \(\color{green}{phi(i*p) = phi(i)*(p-1)}\);
void yych()
{
phi[1] = 1;
for(int i = 2; i <= maxn; i ++)
{
if(!vis[i])
{
prime[++tot] = i;
phi[i] = (i-1);
}
for(int j = 1; j <= tot&&prime[j]*i <= maxn; j ++)
{
vis[i * prime[j]] = 1;
if(i%prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
\(\huge\color{red}{筛mu}\)
可以放心,和反演无关
根据莫比乌斯函数定义来筛:
根据唯一分解定理
\(i = p_1^{k_1}*p_2^{k_2}*p_3^{k_3}...p_q^{k_q}\)
对于一个数\(i\), \[\mu(i)=\begin{cases} 1, &(i=1)\\0, & (\exists k, k>1)\\(-1)^q ,&(\forall k, k<=1) \end{cases} \]
? 1.则对于一个质数\(i\), 显然
? \(\color{green}{\mu(i)=-1}\),
? 2.若\(i\)能整除\(p\), 则\(i\)中\(p\) 的原来指数至少为1, \(i*p\) 中\(p\)指数就一定大于1
? \(\color{green}{\mu(i*p)=0}\)
? 3.若\(i\)不能整除\(p\), 则\(i\)中原来没有\(p\) 这个质数, \(i*p\) 相当于多了一个质数, 则\(q+=1\), 奇变偶, 偶变奇。。。。
? \(\color{green}{\mu(i*p)=-\mu(i)}\)
void yych()
{
mu[1] = 1;
for(int i = 2; i <= maxn; i ++)
{
if(!vis[i])
{
prime[++tot] = i;
mu[i] = -1;
}
for(int j = 1; j <= tot&&prime[j]*i <= maxn; j ++)
{
vis[i * prime[j]] = 1;
if(i%prime[j] == 0)
{
mu[i * prime[j]] = 0;
break;
}
else
mu[i * prime[j]] = -mu[i];
}
}
}
标签:color lse 素数 play exists int ora inline exist
原文地址:https://www.cnblogs.com/spbv587/p/11610804.html