约数个数
const int N = 1e6 + 5;
int sum[N], d[N], prime[N], tot;
bool mark[N];
void get_d()
{
mark[0] = mark[1] = true;
tot = 0;
sum[1] = d[1] = 1;
for(int i = 2; i < N; i ++)
{
if(!mark[i])
{
prime[tot ++] = i;
d[i] = 2;
sum[i] = 1;
}
for(int j = 0; j < tot && i * prime[j] < N; j ++)
{
mark[i * prime[j]] = true;
if(i % prime[j] == 0)
{
d[i * prime[j]] = d[i] / (sum[i] + 1) * (sum[i] + 2);
sum[i * prime[j]] = sum[i] + 1;
break;
}
sum[i * prime[j]] = 1;
d[i * prime[j]] = 2 * d[i];
}
}
}
前缀和: ∑ i = 1 n d ( i ) = ∑ i = 1 n ⌊ n i ⌋ \sum_{i=1}^{n}d(i)=\sum_{i=1}^{n}\lfloor\frac{n}{i}\rfloor i=1∑nd(i)=i=1∑n⌊in⌋
例题:bzoj1968
约数和
const int N = 1e6 + 5;
ll sd[N], sp[N];
int prime[N], tot;
bool mark[N];
void get_sd()
{
mark[0] = mark[1] = true;
tot = 0;
sd[1] = sp[1] = 1;
for(int i = 2; i < N; i ++)
{
if(!mark[i])
{
prime[tot ++] = i;
sd[i] = sp[i] = i + 1;
}
for(int j = 0; j < tot && i * prime[j] < N; j ++)
{
mark[i * prime[j]] = true;
if(i % prime[j] == 0)
{
sd[i * prime[j]] = sd[i] / sp[i] * (sp[i] * prime[j] + 1);
sp[i * prime[j]] = sp[i] * prime[j] + 1;
break;
}
sd[i * prime[j]] = sd[i] * (prime[j] + 1);
sp[i * prime[j]] = prime[j] + 1;
}
}
}
前缀和: ∑ i = 1 n s d ( i ) = ∑ i = 1 n i ∗ ⌊ n i ⌋ \sum_{i=1}^{n}sd(i)=\sum_{i=1}^{n}i*\lfloor\frac{n}{i}\rfloor i=1∑nsd(i)=i=1∑ni∗⌊in⌋
例题:洛谷P2424