# 线性筛d&&sd（约数个数&&约数和）

### d$d$$d$函数

$d\left(n\right)$$d(n)$表示n的约数个数和

$n={p}_{1}^{{a}_{1}}{p}_{2}^{{a}_{2}}...{p}_{k}^{{a}_{k}}$$n=p_1^{a_1}p_2^{a_2}...p_k^{a_k}$
$d\left(n\right)=\left({a}_{1}+1\right)\left({a}_{2}+1\right)...\left({a}_{k}+1\right)$$d(n)=(a_1+1)(a_2+1)...(a_k+1)$

$prime\left[i\right]$$prime[i]$表示第$i$$i$个质数
$num\left[i\right]$$num[i]$表示$i$$i$的最小质因子出现次数

void prepare() {
for (int i=2;i<N;i++) {
if (!no[i]) sshu[++tot]=i;
for (int j=1;j<=tot&&sshu[j]*i<N;j++) {
no[sshu[j]*i]=1;
if (i%sshu[j]==0) {
no[sshu[j]*i]=1;
break;
}
}
}
}

#### ②i%prime[j]!=0

$d\left[i\right]=\left({a}_{1}+1\right)\left({a}_{2}+1\right)...\left({a}_{k}+1\right)$$d[i]=(a_1+1)(a_2+1)...(a_k+1)$

$d\left[i\ast prime\left[j\right]\right]=\left({a}_{1}+1\right)\left({a}_{2}+1\right)...\left({a}_{k}+1\right)\left(1+1\right)$$d[i*prime[j]]=(a_1+1)(a_2+1)...(a_k+1)(1+1)$
$d\left[i\ast prime\left[j\right]\right]=d\left[i\right]\ast d\left[prime\left[j\right]\right]=d\left[i\right]\ast 2$$d[i*prime[j]]=d[i]*d[prime[j]]=d[i]*2$

$num\left[i\ast prime\left[j\right]\right]=1$$num[i*prime[j]] = 1$

#### ③i%prime[j]=0

$i\ast prime\left[j\right]$$i*prime[j]$比起$i$$i$则是多了一个最小质因子

$d\left[i\right]=\left({a}_{1}+1\right)\left({a}_{2}+1\right)...\left({a}_{k}+1\right)$$d[i]=(a_1+1)(a_2+1)...(a_k+1)$
$d\left[i\ast prime\left[j\right]\right]=\left({a}_{1}+1+1\right)\left({a}_{2}+1\right)...\left({a}_{k}+1\right)$$d[i*prime[j]]=(a_1+1+1)(a_2+1)...(a_k+1)$

$num\left[i\ast prime\left[j\right]\right]=num\left[i\right]+1$$num[i*prime[j]]=num[i]+1$

void prepare() {
d[1]=1; num[1]=1;
for (int i=2;i<N;i++) {
if (!no[i]) {
sshu[++tot]=i;
d[i]=2; num[i]=1;
}
for (int j=1;j<=tot&&sshu[j]*i<N;j++) {
int v=sshu[j]*i;
no[v]=1;
if (i%sshu[j]==0) {
num[v]=num[i]+1;
d[v]=d[i]/num[v]*(num[v]+1);
break;
}
d[v]=d[i]<<1; num[v]=1;
}
}
//for (int i=1;i<=10;i++) printf("%d\n",d[i]);
}

### sd$sd$$sd$函数

$sd\left(n\right)$$sd(n)$表示$n$$n$的所有约数之和

$n={p}_{1}^{{a}_{1}}{p}_{2}^{{a}_{2}}...{p}_{k}^{{a}_{k}}$$n=p_1^{a_1}p_2^{a_2}...p_k^{a_k}$
$sd\left(n\right)=\left(1+{p}_{1}+{p}_{1}^{2}+{p}_{1}^{3}+...+{p}_{1}^{{a}_{1}}\right)\left(1+{p}_{2}+{p}_{2}^{2}+...+{p}_{2}^{{a}_{2}}\right)...\left(1+{p}_{3}+{p}_{3}^{2}+...+{p}_{3}^{{a}_{3}}\right)$$sd(n)=(1+p_1+p_1^2+p_1^3+...+p_1^{a_1})(1+p_2+p_2^2+...+p_2^{a_2})...(1+p_3+p_3^2+...+p_3^{a_3})$

$prime\left[i\right]$$prime[i]$表示第$i$$i$个质数
$sp\left[i\right]$$sp[i]$表示$i$$i$$\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}\right)$$(1+p_{1}+p_{1}^2+...+p_{1}^{a_1})$

#### ②i%prime[j]!=0

$sd\left[i\right]=\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}\right)\left(1+{p}_{2}+{p}_{2}^{2}+...+{p}_{2}^{{a}_{2}}\right)...\left(1+{p}_{3}+{p}_{3}^{2}+...+{p}_{3}^{{a}_{3}}\right)$$sd[i]=(1+p_1+p_1^2+...+p_1^{a_1})(1+p_2+p_2^2+...+p_2^{a_2})...(1+p_3+p_3^2+...+p_3^{a_3})$
$sd\left[i\ast prime\left[j\right]\right]=\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}\right)...\left(1+{p}_{3}+{p}_{3}^{2}+...+{p}_{3}^{{a}_{3}}\right)\left(1+prime\left[j\right]\right)$$sd[i*prime[j]]=(1+p_1+p_1^2+...+p_1^{a_1})...(1+p_3+p_3^2+...+p_3^{a_3})(1+prime[j])$

$sd\left[i\ast prime\left[j\right]\right]=sd\left[i\right]\ast sd\left[prime\left[j\right]\right]$$sd[i*prime[j]]=sd[i]*sd[prime[j]]$
$sp\left[i\ast prime\left[j\right]\right]=sp\left[prime\left[j\right]\right]=prime\left[j\right]+1$$sp[i*prime[j]]=sp[prime[j]]=prime[j]+1$

#### ③i%prime[j]=0

$sd\left[i\right]=\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}\right)\left(1+{p}_{2}+{p}_{2}^{2}+...+{p}_{2}^{{a}_{2}}\right)...\left(1+{p}_{3}+{p}_{3}^{2}+...+{p}_{3}^{{a}_{3}}\right)$$sd[i]=(1+p_1+p_1^2+...+p_1^{a_1})(1+p_2+p_2^2+...+p_2^{a_2})...(1+p_3+p_3^2+...+p_3^{a_3})$
$sd\left[i\ast prime\left[j\right]\right]=\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}+{p}_{1}^{{a}_{1}+1}\right)...\left(1+{p}_{3}+{p}_{3}^{2}+...+{p}_{3}^{{a}_{3}}\right)$$sd[i*prime[j]]=(1+p_1+p_1^2+...+p_1^{a_1}+p_1^{a_1+1})...(1+p_3+p_3^2+...+p_3^{a_3})$

$\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}\right)$$(1+p_1+p_1^2+...+p_1^{a_1})$
$\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}+{p}_{1}^{{a}_{1}+1}\right)$$(1+p_1+p_1^2+...+p_1^{a_1}+p_1^{a_1+1})$

$sd\left[i\ast prime\left[j\right]\right]=sd\left[i\right]/\left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}\right)\ast \left(1+{p}_{1}+{p}_{1}^{2}+...+{p}_{1}^{{a}_{1}}+{p}_{1}^{{a}_{1}+1}\right)$$sd[i*prime[j]]=sd[i]/(1+p_1+p_1^2+...+p_1^{a_1})*(1+p_1+p_1^2+...+p_1^{a_1}+p_1^{a_1+1})$

$sd\left[i\ast prime\left[j\right]\right]=sd\left[i\right]/sp\left[i\right]\ast \left(sp\left[i\right]\ast prime\left[j\right]+1\right)$$sd[i*prime[j]]=sd[i]/sp[i]*(sp[i]*prime[j]+1)$
$sp\left[i\ast prime\left[j\right]\right]=sp\left[i\right]\ast prime\left[j\right]+1$$sp[i*prime[j]]=sp[i]*prime[j]+1$

void prepare() {
sd[1]=1; sp[1]=1;
for (int i=2;i<N;i+=) {
if (!no[i]) {
sshu[++tot]=i;
sd[i]=i+1;
sp[i]=i+1;
}
for (int j=1;j<=tot&&sshu[j]*i<N;j++) {
int v=sshu[j]*i;
no[v]=1;
if (i%sshu[j]==0) {
sp[v]=sp[i]*sshu[j]+1;
sd[v]=sd[i]/sp[i]*sp[v];
break;
}
sd[v]=sd[i]*sd[sshu[j]];
sp[v]=sshu[j]+1;
}
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120