Pythagorean Triples
题意:给定一个数,求出另外两个数,使这三个数构成勾股数
分析:
a
2
=
b
2
−
c
2
,
a
2
=
(
b
−
c
)
∗
(
b
+
c
)
a^2=b^2-c^2,a^2=(b-c)*(b+c)
a2=b2−c2,a2=(b−c)∗(b+c)
当a^2为奇数,令(b-c)=1,(b+c) = a^2;
当a^2为偶数,令(b-c)=2,(b+c) = a^2/2;
求解方程即可
a<=2时,不可能构成勾股数
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, mid, rt << 1
#define rrt mid + 1, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
int main(void) {
ll a;
read(a);
if (a <= 2)
printf("-1\n");
else {
ll aa = a * a;
if (aa & 1)
printf("%lld %lld\n", (aa - 1) / 2, (aa + 1) / 2);
else
printf("%lld %lld\n", aa / 4 - 1, aa / 4 + 1);
}
return 0;
}