Codeforces 707C Pythagorean Triples

链接

传送门

题意

给出$n(1 \leq n \leq 10^9)$,输出与$n$能够组成直角三角形的两条边的长度,无解输出-1。

思路

  • $n \leq 2$时,无解。
  • $n \geq 3$且为奇数时,满足${\left( \frac{n^2 - 1}{2} \right)}^2 + n^2 = {\left( \frac{n^2 + 1}{2} \right)}^2$
  • $n \geq 4$且为偶数时,满足${\left( \frac{n^2}{4} - 1 \right)}^2 + n^2 = {\left( \frac{n^2}{4} + 1 \right)}^2$

代码

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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int main() {
int n;
scanf("%d", &n);
if (n <= 2) {
puts("-1");
return 0;
}
LL m = 0, k = 0;
if (n & 1) {
m = (LL(n) * n - 1) / 2;
k = m + 1;
} else {
m = LL(n) * n / 4 - 1;
k = m + 2;
}
printf("%I64d %I64d\n", m, k);
return 0;
}

支付宝扫码领红包