Counting Rectangles
By counting carefully, it can be verified that a rectangular grid that is 3 by 2 contains 18 rectangles. Find the area of the grid with the nearest number of rectangles to two million (T = 2,000,000).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
By counting carefully it can be seen that a rectangular grid measuring \(3\) by \(2\) contains eighteen rectangles:

Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the nearest solution
Problem 85: Counting Rectangles
Mathematical Foundation
Rectangle Counting
Theorem 1 (Rectangle counting formula). An grid contains exactly
axis-aligned rectangles.
Proof. An grid is delineated by vertical lines and horizontal lines . Every axis-aligned rectangle is uniquely determined by a choice of two distinct vertical lines (for the left and right edges) and two distinct horizontal lines (for the top and bottom edges). The number of such vertical pairs is and the number of horizontal pairs is . Since these choices are independent, the total count is their product. Expanding:
Verification. For , : .
Search Space Reduction
Lemma 1 (Upper bound on ). We may restrict the search to satisfying , i.e., .
Proof. Since for all (as ), once we have for every . Moreover, is strictly increasing in both arguments, so all are likewise infeasible for producing a value closer to from below. The quadratic inequality yields .
Lemma 2 (Optimal for fixed ). For fixed and target , the unique real root of satisfies
The optimal integer is either or .
Proof. Setting yields , i.e., . By the quadratic formula, the positive root is as stated. Since is strictly increasing in (for ), the function crosses the target between and , so one of these two integers minimizes .
Editorial
The rectangle count factors into one triangular number from the horizontal direction and one from the vertical direction. That is what makes the search manageable: once one side length is fixed, the count becomes a strictly increasing quadratic in the other side.
So instead of testing every pair blindly, we iterate over one dimension and solve for the real value of the other dimension that would hit the target exactly. Because the count is monotone, the best integer width must lie right next to that real root. The candidates are therefore generated by the feasible heights and only a tiny neighborhood around the corresponding optimal width, which turns a two-dimensional search into an essentially one-dimensional one.
Pseudocode
Set the best difference to infinity and the best area to 0.
For each grid height m while its triangular factor does not already exceed the target:
Compute the real width that would make the rectangle count equal to the target
Examine the nearby integer widths around that real value
For each such width:
compute the exact rectangle count
compare its distance from the target with the current best
if it is better, record the new difference and the new area
Return the recorded best area.
Complexity Analysis
Time: . The outer loop runs while , giving . Each iteration performs arithmetic.
Space: . Only a constant number of variables are maintained.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main(){
// Problem 85: Counting Rectangles
// R(m,n) = C(m+1,2) * C(n+1,2) = m(m+1)/2 * n(n+1)/2
// Find area m*n such that R(m,n) is closest to 2,000,000.
// Answer: 2772
long long target = 2000000;
long long best_diff = LLONG_MAX;
int best_area = 0;
for(int m = 1; m * (m + 1) / 2 <= target; m++){
long long tm = (long long)m * (m + 1) / 2;
double val = 2.0 * target / tm;
int n = (int)((-1.0 + sqrt(1.0 + 4.0 * val)) / 2.0);
for(int nn = max(1, n - 1); nn <= n + 2; nn++){
long long rects = tm * (long long)nn * (nn + 1) / 2;
long long diff = abs(rects - target);
if(diff < best_diff){
best_diff = diff;
best_area = m * nn;
}
}
}
cout << best_area << endl;
return 0;
}
"""
Problem 85: Counting Rectangles
Find the area of the rectangular grid whose rectangle count is
nearest to 2,000,000.
R(m, n) = C(m+1,2) * C(n+1,2) = m(m+1)/2 * n(n+1)/2
Answer: 2772
"""
import math
def solve():
target = 2_000_000
best_diff = float('inf')
best_area = 0
m = 1
while m * (m + 1) // 2 <= target:
tm = m * (m + 1) // 2
val = 2 * target / tm
n = int((-1 + math.sqrt(1 + 4 * val)) / 2)
for nn in range(max(1, n - 1), n + 3):
rects = tm * nn * (nn + 1) // 2
diff = abs(rects - target)
if diff < best_diff:
best_diff = diff
best_area = m * nn
m += 1
return best_area
def main():
answer = solve()
assert answer == 2772
print(answer)
if __name__ == "__main__":
main()