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 several exact algorithms: the classical $O(n^2)$ sweep, decompositions of complexity $O(n^{3/2}\log n)$ and $O(n^{4/3}\log n)$, a ten-moment weighted-floor-sum reduction of complexity $O(n\log^3 n)$, and a divisor-layer algorithm with the complexity $O(n\log^2 n)$. We also give an all-values algorithm that computes $F(1),\ldots,F(N)$ in $O(N^{3/2})$ arithmetic operations. The main idea behind the near-linear one-value algorithms is to reduce the geometric summation to constant-size families of weighted floor sums closed under Euclidean-style affine and reciprocal transformations. Besides the exact algorithmic results, we 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(n\log^2 n)$ 的除数分层算法。我们还给出一种全值算法,可在 $O(N^{3/2})$ 次算术运算内计算 $F(1),\ldots,F(N)$。近线性单值算法的主要思想是将几何求和归约为常数大小的加权下取整求和族,该族在欧几里得式仿射和互反变换下保持封闭性。除精确算法结果外,我们推导出两项渐近展开式 $F(n)=\frac{4\log 2-1}{\pi^2}n^4\log n+B\,n^4+o(n^4)$,其中 $B$ 具有显式公式,这为算法产生的大 $n$ 数值数据提供了独立的自洽性验证。