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 }
时间: 2024-10-25 05:46:23

Codeforces 707C. Pythagorean Triples的相关文章

【数学】Codeforces 707C Pythagorean Triples

题目链接: http://codeforces.com/problemset/problem/707/C 题目大意: 给你一个数,构造其余两个勾股数.任意一组答案即可,没法构造输出-1. 答案long long 范围. 题目思路: [数学] 这里贴一下勾股数的构造: 当a为大于1的奇数2n+1时,b=2n2+2n, c=2n2+2n+1. 实际上就是把a的平方数拆成两个连续自然数,例如: n=1时(a,b,c)=(3,4,5) n=2时(a,b,c)=(5,12,13) n=3时(a,b,c)=

CodeForces 707C Pythagorean Triples (数论)

题意:给定一个数n,问你其他两边,能够组成直角三角形. 析:这是一个数论题. 如果 n 是奇数,那么那两边就是 (n*n-1)/2 和 (n*n+1)/2. 如果 n 是偶数,那么那两边就是 (n/2*n/2-1) 和 (n/2*n/2+1).那么剩下的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #

Codeforces Round #368 (Div. 2) Pythagorean Triples

Pythagorean Triples Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths

Codeforces 707 C. Pythagorean Triples(找规律)——Codeforces Round #368 (Div. 2)

传送门 Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding t

codeforces707C:Pythagorean Triples

Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresp

【Codeforces 707C】Pythagorean Triples(找规律)

一边长为a的直角三角形,a^2=c^2-b^2.可以发现1.4.9.16.25依次差3.5.7.9...,所以任何一条长度为奇数的边a,a^2还是奇数,那么c=a^2/2,b=c+1.我们还可以发现,1.4.9.16.25.36各项差为8.12.16.20,偶数的平方是4的倍数,那么c=a^2/4-1,b=a^2/4+1. #include <iostream> using namespace std; int main() { long long n; cin>>n; n*=n;

Pythagorean Triples CodeForces - 707C 推理题,大水题

给定一个数n(1 <= n <= 1e9),判断这个数是否是一个直角三角形的边长,如果是,则输出另外两条边(1 <= x <= 1e18),否则输出-1. 参考题解:http://blog.csdn.net/harlow_cheng/article/details/69055614 首先,当n <= 2 的时候无解,其他时候都有解 假设n是直角边,a是斜边,则n^2 + b^2 = a^2; n^2 = (a + b)*(a - b); ①假设n是偶数,则另(a - b) =

Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

给定一个直角三角形的一边长度.问是否存在一个直角三角形,使得它满足有一边的长度是x 当x=1.2的时候是无解的,可以暴力打表看看. 注意到,相邻的两个数的平方的差值是奇数 x^2 - (x-1)^2 = 2*x-1 间隔为2的两个数的平方的差值是偶数 (x+1)^2 - (x-1)^2 = 4*x 这样就可以分类讨论了. 把n永远当成是指角边就OK #include <cstdio> #include <cstdlib> #include <cstring> #incl

CodeForces - 707C

让我们铭记勾股数,1,2无勾股数,大于等于3的奇数:2n+1勾股数是2*n*n+2*n,2*n*n+2*n+1,大于等于3的偶数:2n为n*n+1,n*n-1.啊,gtmd勾股数! #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; ll n; int main() {