-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39Integer_right_triangles.cpp
More file actions
88 lines (82 loc) · 1.8 KB
/
Copy path39Integer_right_triangles.cpp
File metadata and controls
88 lines (82 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* How I solved this problem
* The main idea of this problem is to count how many right triangles
*
* Euclid's formula:
* a = m^2 - n^2
* b = 2mn
* c = m^2 + n^2
* The perimeter becomes:
* p = a + b + c
* p = 2*m*(m+n)
* Using this formula we generate primitive Pythagorean triples.
* Conditions required:
* 1) gcd(m,n) = 1
* 2) (m-n) must be odd
*
* These conditions ensure we generate only primitive triples.
*
* Every primitive triangle also has multiples:
*
* (3,4,5) -> p = 12
* (6,8,10) -> p = 24
* (9,12,15) -> p = 36
*
* So once we find a base perimeter p, we increase the count for:
*
* p, 2p, 3p, 4p ...
*
* We store this in an array:
*
* cnt[p] = number of right triangles with perimeter p
*
* After computing all counts, we build another array:
*
* best[i]
*
* best[i] stores the perimeter <= i that has the maximum number
* of right triangle solutions so far.
*
* Finally for every query N, we simply output:
*
* best[N]
*
* because it already stores the correct answer.
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000000;
int main()
{
vector<int> cnt(MAXN + 1, 0);
for (int m = 2; 2 * m * m <= MAXN; m++){
for (int n = 1; n < m; n++){
if (((m - n) & 1) && __gcd(m, n) == 1){
int p = 2 * m * (m + n);
if (p > MAXN)
break;
for (int k = p; k <= MAXN; k += p)
cnt[k]++;
}
}
}
vector<int> best(MAXN + 1);
int mx = 0, ans = 0;
for (int i = 0; i <= MAXN; i++)
{
if (cnt[i] > mx)
{
mx = cnt[i];
ans = i;
}
best[i] = ans;
}
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
cout << best[n] << endl;
}
}