题目描述
We shall define a pythagorean polygon to be a convex polygon with the following properties:
- there are at least three vertices,
- no three vertices are aligned,
- each vertex has integer coordinates,
- each edge has integer length.
For a given integer $n$, define $P(n)$ as the number of distinct pythagorean polygons for which the perimeter is $≤ n$.
Pythagorean polygons should be considered distinct as long as none is a translation of another.
You are given that $P(4) = 1$, $P(30) = 3655$ and $P(60) = 891045$.
Find $P(120)$.
题意
我们定义满足以下条件的凸包是一个毕达哥拉斯多边形:
- 至少包含三个点
- 不存在三点共线
- 所有点的横纵坐标都是整数
- 所有边长都是整数
对于给定的 $n$,我们定义 $P(n)$ 是周长 $\leq n$ 的不同的毕达哥拉斯多边形的数量。
分析
显然可以记忆化搜索。
首先我们可以暴搜出所有可行的方向。可以发现可行的方向并不多。因为要求点和长度都是整数,意味着要么是平行x轴,要么是平行y轴,要么是一个两边长都是整数的直角三角形的斜边,而且斜边长是整数,例如 $(3,4,5)$ 。
然后为了保证是凸包,我们按照方向逆时针进行枚举,每次枚举出一个方向后,判断一下可以往这个方向延伸多少,然后check一下剩下的距离足够到原点嘛。稍微剪剪枝,就可以了。
代码
给大佬递上我奇丑无比的代码 (/ω\)
1 |
|