Project Euler 496
June 16, 2024
I have so much fun with this problem maybe, because it's about euclidean geometry. I think the easiest part is to reach to some conclusions finding similarities and applying some geometry theorems, then the boring part is just the computation. Anyway, I think this will be a good excuse to draw some triangles.
Statement
Given an integer sided triangle :
Let be the incenter of .
Let be the intersection between the line and the circumcircle of .
We define as the sum of for the triangles that satisfy and .
For example, because the triangles with satisfy the conditions.
Find .
Solution
As always, when you have a triangle and circumcircle, you can start thinking about cyclyc quadrilaterals, actually the point is a good point to start drawing a cyclic quadrilateral.

then, drawing the bisector for :

finally, just completing angles:

Applying Ptolemy's theorem:
Writing :
this problem is similar to generate pythagorean triplets, the only remaining step is to find the upper bound for .
Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define N 1000000000
int main() {
int n;
cin >> n;
if (n == -1)
n = N;
n = min(n, N);
ll ans = 0;
for (int u = 1; 1LL * u * u < 3.42 * n; u++) {
for (int v = 1; v < u && 1LL * u * v <= n; v++) {
if (1LL * u * u + 1LL * v * v > 3.42 * n)
break;
if (__gcd(u, v) == 1 and (u % 2 == 0 or v % 2 == 0)) {
ll m = 1LL * u * u + 1LL * v * v;
ll double_a = 2LL * u * v;
ll c = 1LL * u * u - 1LL * v * v;
if (c > double_a)
swap(c, double_a);
if (double_a % 2 != 0) {
m *= 2;
c *= 2;
double_a *= 2;
}
if (5 * c >= 3 * m or double_a > 2 * n)
continue;
int a = double_a / 2;
int b = (m - c) / 2;
int cnt = n / a;
ans += (1LL * (cnt + 1) * cnt / 2) * a;
}
}
}
cout << ans << '\n';
return 0;
}