Square on the Inside
Let ABCD be a quadrilateral whose vertices are lattice points lying on the coordinate axes: A(a, 0), B(0, b), C(-c, 0), D(0, -d) where 1 <= a, b, c, d <= m and a, b, c, d, m are integers. For m = 4...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let $ABCD$ be a quadrilateral whose vertices are lattice points lying on the coordinate axes as follows:
$A(a, 0)$, $B(0, b)$, $C(-c, 0)$, $D(0, -d)$, where $1 \le a, b, c, d \le m$ and $a, b, c, d, m$ are integers.
It can be shown that for $m = 4$ there are exactly $256$ valid ways to construct $ABCD$. Of these $256$ quadrilaterals, $42$ of them strictly contain a square number of lattice points.
How many quadrilaterals $ABCD$ strictly contain a square number of lattice points for $m = 100$?
Problem 504: Square on the Inside
Mathematical Analysis
Pick’s Theorem
For a simple polygon with vertices at lattice points:
where is the area, is the number of interior lattice points, and is the number of boundary lattice points.
Area of the Quadrilateral
The quadrilateral has vertices , , , . Using the Shoelace formula:
Wait — more carefully, the area of this kite-like shape is composed of 4 right triangles:
Boundary Points
The number of lattice points on a line segment from to (excluding one endpoint) is .
The four edges contribute:
- : points (excluding endpoints)
- : points
- : points
- : points
Total boundary points (including 4 vertices):
Wait — each edge from vertex to vertex has lattice points (including both endpoints). So total boundary points = sum of edge points minus double-counted vertices:
(Each edge contributes interior points + 2 endpoints. Sum = since each vertex counted twice = .)
Interior Points
From Pick’s Theorem:
We check whether is a perfect square.
Editorial
We precompute gcd(x, y) for all 1 <= x, y <= 100. We then precompute perfect squares up to max possible i. Finally, iterate over all (a, b, c, d) with 1 <= a, b, c, d <= 100.
Pseudocode
Precompute gcd(x, y) for all 1 <= x, y <= 100
Precompute perfect squares up to max possible i
For all (a, b, c, d) with 1 <= a, b, c, d <= 100:
Return counter
# Optimization
Complexity Analysis
- Time: with precomputed GCD table, which is — feasible.
- Space: for GCD table.
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() {
const int M = 100;
// Precompute GCD table
int g[M + 1][M + 1];
for (int i = 0; i <= M; i++)
for (int j = 0; j <= M; j++)
g[i][j] = __gcd(i, j);
// Precompute perfect squares
// Max interior points: ((M+M)*(M+M))/2 + 1 = 2*M*M + 1 = 20001
int max_i = 2 * M * M + 1;
vector<bool> is_sq(max_i + 1, false);
for (int k = 0; (long long)k * k <= max_i; k++)
is_sq[k * k] = true;
int count = 0;
for (int a = 1; a <= M; a++) {
for (int b = 1; b <= M; b++) {
for (int c = 1; c <= M; c++) {
for (int d = 1; d <= M; d++) {
int boundary = g[a][b] + g[b][c] + g[c][d] + g[d][a];
int twice_area = (a + c) * (b + d);
int twice_i = twice_area - boundary + 2;
// i must be integer, so twice_i must be even
if (twice_i % 2 != 0) continue;
int interior = twice_i / 2;
if (interior >= 0 && interior <= max_i && is_sq[interior])
count++;
}
}
}
}
cout << count << endl;
return 0;
}
from math import gcd, isqrt
def solve():
M = 100
# Precompute GCD table
g = [[0] * (M + 1) for _ in range(M + 1)]
for i in range(M + 1):
for j in range(M + 1):
g[i][j] = gcd(i, j)
# Precompute perfect squares
max_i = 2 * M * M + 1
is_sq = set()
k = 0
while k * k <= max_i:
is_sq.add(k * k)
k += 1
count = 0
for a in range(1, M + 1):
for b in range(1, M + 1):
for c in range(1, M + 1):
gab = g[a][b]
gbc = g[b][c]
sum_ac = a + c
sum_bd_base = b
for d in range(1, M + 1):
boundary = gab + gbc + g[c][d] + g[d][a]
twice_area = sum_ac * (b + d)
twice_i = twice_area - boundary + 2
if twice_i % 2 != 0:
continue
interior = twice_i // 2
if interior in is_sq:
count += 1
print(count)
solve()