Project Euler 299
Project Euler 299
题目
Three similar triangles
Four points with integer coordinates are selected:\(A(a,0), B(b,0), C(0,c)\) and \(D(0,d),\) with \(0<a<b\) and \(0<c<d\).
Point \(P\), also with integer coordinates, is chosen on the line \(AC\) so that the three triangles \(ABP, CDP\) and \(BDP\) are all similar.

It is easy to prove that the three triangles can be similar, only if \(a=c\).
So, given that \(a=c\), we are looking for triplets \((a,b,d)\) such that at least one point \(P\) (with integer coordinates) exists on \(AC\), making the three triangles \(ABP, CDP\) and \(BDP\) all similar.
For example, if \((a,b,d)=(2,3,4)\), it can be easily verified that point \(P(1,1)\) satisfies the above condition. Note that the triplets \((2,3,4)\) and \((2,4,3)\) are considered as distinct, although point \(P(1,1)\) is common for both.
If \(b+d<100\), there are \(92\) distinct triplets \((a,b,d)\) such that point \(P\) exists.
If \(b+d<100 000\), there are \(320471\) distinct triplets \((a,b,d)\) such that point \(P\) exists.
If \(b+d<100 000 000\), how many distinct triplets \((a,b,d)\) are there such that point \(P\) exists?
解决方案
可以发现,\(\angle DCP\)和\(\angle BAP\)必定是钝角。而在\(\triangle BDP\)中,最有潜力成为钝角的是\(\angle BPD\)。因此如果\(\triangle ABP,\triangle CDP,\triangle BDP\)相似,那么\(\angle DCP,\angle BAP,\angle BPD\)必定对应。且它们的大小都为\(\dfrac{3\pi}{4}\)。假设点\(P\)的坐标为\((x,y)\),那么满足\(x+y=a\)。
- 如果\(\triangle DCP\sim \triangle PAB\),那么有\(\dfrac{|DC|}{|PA|}=\dfrac{|CP|}{|AB|}=\dfrac{|DP|}{|PB|}\),也就是\(\dfrac{d-a}{\sqrt2 y}=\dfrac{\sqrt2x}{b-a}\),也就是\((d-a)(b-a)=2xy\)。
- 如果\(\triangle DCP\sim \triangle BAP\),那么有\(\dfrac{|DC|}{|BA|}=\dfrac{|CP|}{|AP|}=\dfrac{|DP|}{|BP|}\),也就是\(\dfrac{d-a}{b-a}=\dfrac{\sqrt2 x}{\sqrt2y}\),也就是\((d-a)y=(b-a)x\)。
考虑如下两种不同的情况:\(\triangle DCP\sim \triangle DPB,\triangle DCP\sim \triangle BPD\)。
\(\triangle DCP\sim \triangle DPB\)
此时有\(\dfrac{|DC|}{|DP|}=\dfrac{|CP|}{|PB|}=\dfrac{|DP|}{|DB|}\)。
联合情况 1,可以知道有 \(\dfrac{|DC|}{|CP|}=\dfrac{|DP|}{|PB|}=\dfrac{|DC|}{|PA|}\),也就是\(|CP|=|PA|\),即\(x=y=\dfrac{a}{2}\)。这时就有\(2(d-a)(b-a)=a^2\).即\(a\)必须是偶数。
联合情况 2,可以知道有 \(\dfrac{|DC|}{|CP|}=\dfrac{|DP|}{|PB|}=\dfrac{|DC|}{|BA|}\),也就是\(|CP|=|AB|\),即\(\sqrt{2} x=b-a\)。这种情况是不可能出现的,因为回代到原式中,关于\((a,b,d)\)的式子一定会出现无理数系数\(\sqrt{2}\)。
\(\triangle DCP\sim \triangle BPD\)
因此可以得到\(\angle CDP=\angle PBD\),以及比例关系:
\[\dfrac{|DC|}{|BP|}=\dfrac{|CP|}{|PD|}=\dfrac{|DP|}{|BD|}.\tag{B}\]
联合情况 1(\(\angle CDP=\angle APB\)),那么可以得到\(\angle APB=\angle PBD.\) 所以最终有\(PA\parallel BD.\)那么由于\(a=c\),因此必然有\(b=d\)。
联立情况 2,那么基于情况 2 的相似条件,令\(k=\dfrac{|DP|}{|BP|}=\dfrac{x}{y}=\dfrac{d-a}{b-a}\)。此外由\(\dfrac{|CP|}{|PD|}=\dfrac{|DP|}{|BD|}\),可以得知\(|DP|^2=|CP|\cdot|BD|\)。
由\((B)\)的第一、第三项,可以得到\(\dfrac{|DC|}{|BP|}=\dfrac{|DP|}{|BD|}\Longrightarrow|DC|\cdot|BD|=|DP|\cdot|BP|.\)代入\(k=\dfrac{|DP|}{|BP|}\),得到\(|DC|\cdot|BD|=k|BP|^2.\)
再代入 \(k=\dfrac{d-a}{b-a}=\dfrac{|DC|}{|BA|}\),即 \(k=\dfrac{|DC|}{b-a}\),可约去 \(|DC|\)(因为 \(d>a\) 所以 \(|DC|>0\)):\(|BD|=\dfrac{|BP|^2}{b-a}.\)
后得到\(|DP|^2 = |CP|\cdot|BD|= |CP|\cdot\dfrac{|BP|^2}{b-a}.\)
又因为\(|DP|^2 = k^2 |BP|^2\),所以\(k^2=\dfrac{|CP|}{b-a}.\)代入\(k=\dfrac{x}{y}\) 与 \(|CP|=\sqrt2x\)后,得到\(\left(\dfrac{x}{y}\right)^2=\dfrac{\sqrt2x}{b-a}.\)最终化简得到:
\[ \sqrt{2}=\frac{x(b-a)}{y^2}. \]
右边是有理数(整数比),左边是无理数,矛盾。因此这个组合不可行。
接下来考虑对这两种情况进行计数。
\(2(d-a)(b-a)=a^2\)
通过化简,可以得到:
\[ 2(b-a)(d-a)=a^2 \Longleftrightarrow a^2-2a(b+d)+2bd=0 \Longleftrightarrow (b+d-a)^2=b^2+d^2. \]
令\(c=b+d-a\),那么原式则等价于\(c^2=b^2+d^2.\)也就是 \((b,d,c)\) 是勾股三元组,且\(a=b+d-c, P=\left(\dfrac{a}{2},\dfrac{a}{2}\right).\)
因此这一类型的本质是:\(b,d\) 是一组勾股直角边,\(b+d\) 控制放大倍数;并且有序对 \((b,d)\) 与 \((d,b)\) 都算不同。
对每个原始勾股三元组,设两条直角边为 \(p,q\),则 \(b+d=p+q\),并且有序 \((b,d)=(p,q)\) 与 \((q,p)\) 都要计入,因此贡献
\[ 2\left\lfloor\frac{M-1}{p+q}\right\rfloor. \]
\(b=d\)
那么此时就得到\((b-a)^2=2xy\),其中\(x+y=a\)。
尝试用一组标准参数化(等价于从勾股三元组出发):取互素且一奇一偶的正整数 \(m>n\),令\(x=2n^2,y=(m-n)^2.\)则\(a=x+y=2n^2+(m-n)^2\)
并且有\(2xy=2\cdot 2n^2\cdot (m-n)^2=(2n(m-n))^2,\)因此\(b-a=2n(m-n),\)从而\(b=a+2n(m-n)=m^2+n^2.\)
于是这类解就满足:
\[b=d=m^2+n^2\]
对每个原始勾股三元组斜边为 \(c\),则 \(b+d=2c\),贡献
\[ \left\lfloor\frac{M-1}{2c}\right\rfloor. \]
把所有原始勾股三元组的贡献累加,即得答案。
代码
1 | from tools import * |