给一个贝尔级数视角的解释。 刻画一下 (−1)f(n)(-1)^{f(n)}(−1)f(n) 的贝尔级数,是 1 -1 1 -1 1 -1...,也就是 11+x\frac 1{1+x}1+x1。 注意到 11+x×11−x=11−x2\frac 1{1+x} \times \frac 1{1-x}=\frac 1{1-x^2}1+x1×1−x1=1−x21,对应到原函数上,就是 (−1)f(n)∗I=[n is a square number](-1)^{f(n)} * I=[\text{n is a square number}](−1)f(n)∗I=[n is a square number],于是不难杜教筛了。 代码我还没写,复杂度 O(n23)O(n^{\frac 23})O(n32)。