煎蛋提。
不妨令 \(g(i)=(-1)^{f(i)}\),由 \(f(i)\) 的和性不难推出 \(g(i)\) 为完全积性函数,因此可以考虑杜教筛。
考察 \(g(n)\) 和恒等函数 \(I(n)=1\) 的卷积 \(g*I\),不难发现 \((g*I)(p^k)=\sum\limits_{i=0}^kg(p^i)=\sum\limits_{i=0}^k(-1)^i=[2\mid i]\),又由于 \(g*I\) 为积性函数,有 \((g*I)(n)=(g*I)(p_1^{q_1}p_2^{q_2}\cdots p_m^{q_m})=[2\mid q_1][2\mid q_2]\cdots [2\mid q_m]=\prod\limits_{i=1}^m[2\mid q_i]\),也就是说 \((g*I)(n)=[\exists k\in \mathbb{N^*},n=k^2]\),也就是 \(n\) 为完全平方数。
于是 \(\sum\limits_{i=1}^n(g*I)(n)=\left\lfloor\sqrt{n}\right\rfloor\),简单筛一下即可。
const int N = 2.5e7 + 250;
int n, lim, tot, vs[N], f[N], pr[N];
map<int, int> s;
void init(int lim) {
f[1] = 1;
for (int i = 2; i <= lim; i++) {
if (!vs[i]) pr[++tot] = i, f[i] = -1;
for (int j = 1; j <= tot && i * pr[j] <= lim; j++) {
f[i * pr[j]] = -f[i], vs[i * pr[j]] = 1;
if (i % pr[j] == 0) break;
}
}
for (int i = 2; i <= lim; i++)
f[i] += f[i - 1];
}
int S(int x) {
if (x <= lim) return f[x];
if (s[x]) return s[x];
int res = sqrt(x);
for (int l = 2, r; l <= x; l = r + 1)
r = x / (x / l), res -= (r - l + 1) * S(x / l);
return s[x] = res;
}
signed main() {
n = rd(), init(lim = pow(n, 2. / 3));
wr(S(n));
return 0;
}
标签:limits,int,Sum,mid,积性,sum
From: https://www.cnblogs.com/Ender32k/p/17569357.html