Files
contests/2022/republic/d0/problem06/problem06.cpp
2022-06-01 20:14:50 +03:00

72 lines
1.2 KiB
C++

#include <iostream>
#include <cmath>
#include <map>
#include <algorithm>
#include <set>
#include <utility>
using namespace std;
bool areCoprimes(int a, int b, int c) {
return __gcd(a, b) == 1 && __gcd(a, c) == 1 && __gcd(b, c) == 1;
}
int main() {
int64_t c;
cin >> c;
map<int64_t, int64_t> roots;
int64_t i = 1;
while (true) {
int64_t tmp = pow(i, 2);
if (tmp > c) {
break;
}
roots[tmp] = i;
i++;
}
int64_t rightLimit = i - 1;
set<pair<int64_t, int64_t>> results;
for (int64_t x = 1; x <= rightLimit; x++) {
int64_t xSqr = pow(x, 2);
int64_t y = roots[c - xSqr];
if (y == 0) {
continue;
}
// x^4 + 2*x^2*y^2 + y^4 = r^2
// x^4 - 2*x^2*y^2 + y^4 + 4*x^2*y^2 = r^2
// (x^2-y^2)^2 + (2*x*y)^2 = r^2
// a = x^2-y^2 b = 2*x*y
int64_t a = pow(y, 2) - pow(x, 2);
int64_t b = 2 * x * y;
if (a > b) {
swap(a, b);
}
if (a < 0) {
continue;
}
if (areCoprimes(a, b, c)) {
results.insert(make_pair(a, b));
}
}
if (results.size() == 0) {
cout << "NOPE" << endl;
} else {
for (auto it = results.begin(); it != results.end(); it++) {
cout << (it -> first) << " " << (it -> second) << " " << c << endl;
}
}
return 0;
}