-
Notifications
You must be signed in to change notification settings - Fork 0
/
#009:Special_Pythagorean_triplet.cpp
72 lines (63 loc) · 1.59 KB
/
#009:Special_Pythagorean_triplet.cpp
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
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
long max_(long a, long b)
{
return (a * (a > b) + b * (a <= b));
}
void Pythagorean_triplets(vector<int> v, int t)
{
int a, b, c, b2, c2, bc, _b_c;
int x = v[distance(v.begin(), max_element(v.begin(), v.end()))], x_2;
vector<int> v_uniq = v;
sort(v_uniq.begin(), v_uniq.end());
v_uniq.erase( unique( v_uniq.begin(), v_uniq.end() ), v_uniq.end() );
int n = v_uniq.size();
vector<long> res(n, -1);
x_2 = floor(x / 2);
for (c = x_2; c > 0; c--)
{
c2 = c * c;
for (b = x_2 - 1; b > 0; b--)
{
b2 = b * b;
if (floor(sqrt(c2 - b2)) != ceil(sqrt(c2 - b2)))
continue ;
bc = b * c;
_b_c = -b -c;
for (int i = n - 1; i >= 0; i--)
{
a = v_uniq[i] + _b_c;
if (a <= 0)
break ;
if ((a * a + b2) == c2)
res[i] = max_(res[i], a * bc);
}
}
}
for (int i = 0; i < t; i++)
{
auto it = find(v_uniq.begin(), v_uniq.end(), v[i]);
x = distance(v_uniq.begin(), it);
cout << res[x] << endl;
while ((i < (t - 1)) && (v[i] == v[i + 1]))
{
i++;
cout << res[x] << endl;
}
}
}
int main(){
int t;
vector<int> v;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
int n;
cin >> n;
v.push_back(n);
}
Pythagorean_triplets(v, t);
return 0;
}