link。
由 CRT,我们只考虑模奇素数 (p) 的情况。
利用勒让德符号帮助计数(此处记 (left(frac{0}{p} ight) = 0)):
[egin{aligned}
ans &= sum_{i=0}^{p-1}(left(frac{i}{p}
ight) + 1)(left(frac{x-i}{p}
ight) + 1) \
&= sum_{i=0}^{p-1}left(frac{i}{p}
ight)left(frac{x-i}{p}
ight) + 2sum_{i=0}^{p-1}left(frac{i}{p}
ight) + p
end{aligned}
]
由于勒让德符号的一些性质,可得 (left(frac{i}{p} ight)left(frac{x-i}{p} ight) = left(frac{i(x-i)}{p} ight)),且 (sum_{i=0}^{p-1}left(frac{i}{p} ight) = 0):
[egin{aligned}
ans &= sum_{i=color{red}{1}}^{p-1}left(frac{i(x-i)}{p}
ight) + p \
&= sum_{i=1}^{p-1}left(frac{frac{x}{i} - 1}{p}
ight) + p
end{aligned}
]
当 (x = 0) 时,(ans = (p-1) imesleft(frac{-1}{p} ight)+p=(p-1) imes(-1)^{frac{p-1}{2}}+p)。
当 (x eq 0) 时,(ans = sum_{i=0}^{p-1}left(frac{i}{p} ight) - left(frac{-1}{p} ight) + p = (-1)^{frac{p+1}{2}} + p)。
更一般的情况可见 https://blog.csdn.net/qq_39972971/article/details/106379811 。