We study the exact counting problem for all lattice rectangles contained in the square $[0,n)\times[0,n)$, including non-axis-parallel ones. Starting from the standard parametrization by a primitive direction $(u,v)$ and two side lengths, we derive a sequence of exact algorithms of complexity $O(n^2)$, $O(n^{3/2}\log n)$, $O(n^{4/3}\log n)$, and finally $O(n\log^3 n)$. The main idea behind the near-linear algorithm is to reduce the geometric summation to a constant-size family of weighted floor sums closed under Euclidean-style affine and reciprocal transformations, and hence evaluable in $O(\log n)$ time per query. Besides the exact algorithmic result, we also derive a two-term asymptotic expansion, $F(n)=\frac{4\log 2-1}{π^2}n^4\log n+B\,n^4+o(n^4)$ with the explicit formula for $B$, which provides an independent consistency check for the large-$n$ numerical data produced by the algorithms.
翻译:我们研究包含在正方形 $[0,n)\times[0,n)$ 内所有格点矩形(包括非轴平行矩形)的精确计数问题。从原始方向 $(u,v)$ 和两个边长的标准参数化出发,我们推导出一系列精确算法,其复杂度分别为 $O(n^2)$、$O(n^{3/2}\log n)$、$O(n^{4/3}\log n)$ 以及最终的 $O(n\log^3 n)$。近线性算法背后的核心思想是将几何求和问题约化为一族在欧几里得型仿射变换和倒数变换下封闭的加权底函数求和常系数族,从而每次查询可在 $O(\log n)$ 时间内求值。除精确算法结果外,我们还推导出两项渐近展开式 $F(n)=\frac{4\log 2-1}{π^2}n^4\log n+B\,n^4+o(n^4)$,其中 $B$ 的显式公式可作为算法生成的大 $n$ 数值数据的独立一致性检验依据。