首页 > 代码库 > Codeforces 707C. Pythagorean Triples

Codeforces 707C. Pythagorean Triples

题目链接:http://codeforces.com/problemset/problem/707/C

题意:

给你直角三角形其中一条边的长度,让你输出另外两条边的长度.

思路:

直接构造勾股数即可,构造勾股数的方法:

  当 a 为大于 1 的奇数 2 * n+1 时, b = 2 * n * n + 2 * n, c = 2 * n * n + 2 * n + 1.

  当 a 为大于 4 的偶数 2 * n 时,b = n * n - 1, c = n * n + 1.

然后不满足上述构造方法的数 1, 2, 4直接特判就好.

代码:

 1 #include <bits/stdc++.h> 2  3 using namespace std; 4  5 const int MAXN = 100000; 6 typedef long long LL; 7  8 int main() { 9     ios_base::sync_with_stdio(0); cin.tie(0);10     LL a; cin >> a;11     LL b = 1, c = 10, n;12     if(a > 1 && a & 1) n = ( a - 1 ) / 2, b = 2 * n * n + 2 * n, c = b + 1;13     else if(a > 4 && !(a & 1)) n = a / 2, b = n * n - 1, c = b + 2;14     else if(a == 4) b = 3, c = 5;//特判15     if(a != 1 && a != 2) cout << b << " " << c << endl;16     else cout << "-1" << endl;17     return 0;18 }

 

Codeforces 707C. Pythagorean Triples