本文主要是介绍Codeforces Problem 707C Pythagorean Triples(数学),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
此文章可以使用目录功能哟↑(点击上方[+])
比赛链接→Codeforces Round #368 (Div. 2)
Codeforces Problem 707C Pythagorean Triples
Accept: 0 Submit: 0
Time Limit: 1 second Memory Limit : 256 megabytes
Problem 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 corresponding to triple. Such triples are called Pythagorean triples.
For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.
Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.
Katya had no problems with completing this task. Will you do the same?
Input
The only line of the input contains single integer n (1 ≤ n ≤ 10^9) — the length of some side of a right triangle.
Output
Print two integers m and k (1 ≤ m, k ≤ 10^18), such that n, m and k form a Pythagorean triple, in the only line.
In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them.
Sample Input
6
1
17
67
Sample Output
8 10
-1
144 145
2244 2245
Hint
Illustration for the first sample.
Problem Idea
解题思路:
【题意】
给你一个数n,问是否存在另两个数a,b,使得这三个数恰好构成直角三角形的三条边
【类型】
数学题
【分析】
对于直角三角形△ABC,如图:
我们知道的是
因为题目说如果题目存在多种解,输出任何一个就可以了,所以我们不妨假设输入的n是一条直角边的长度,那么
根据平方差公式可得
那么,这个时候,我们要求解的就是a,b
于是乎,我们分类讨论即可
【时间复杂度&&优化】
O(1)
题目链接→Codeforces Problem 707C Pythagorean Triples
Source Code
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 40;
const int inf = 1000000007;
const int mod = 1000000007;
int main()
{__int64 n,a,b;scanf("%I64d",&n);if(n==1||n==2)puts("-1");else if(n*n%2)printf("%I64d %I64d\n",(n*n-1)/2,(n*n+1)/2);elseprintf("%I64d %I64d\n",(n*n/2-2)/2,(n*n/2+2)/2);return 0;
}
菜鸟成长记
这篇关于Codeforces Problem 707C Pythagorean Triples(数学)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!