Codeforces Problem 707C Pythagorean Triples(数学)
来源:互联网 发布:java md5 32加密算法 编辑:程序博客网 时间:2024/05/16 14:21
此文章可以使用目录功能哟↑(点击上方[+])
比赛链接→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); else printf("%I64d %I64d\n",(n*n/2-2)/2,(n*n/2+2)/2); return 0;}菜鸟成长记
- Codeforces Problem 707C Pythagorean Triples(数学)
- codeforces 707 C Pythagorean Triples【数学】
- Codeforces 707C Pythagorean Triples(数学)
- codeforces 707C Pythagorean Triples(数学)
- CodeForces 707C Pythagorean Triples(数学 构造)
- codeforces 707-C. Pythagorean Triples(数学)
- [codeforces] C - Pythagorean Triples 数学
- codeforces 707C - Pythagorean Triples
- codeforces 707C Pythagorean Triples
- 【codeforces 707C Pythagorean Triples】
- codeforces--707C. Pythagorean Triples
- CodeForces 707C-Pythagorean Triples
- 【数学相关、规律】Codeforces 707C Pythagorean Triples
- Codeforces Round #368-C - Pythagorean Triples-数学
- CodeForces 707C Pythagorean Triples(数学题)
- codeforces 707 c Pythagorean Triples 数论+勾股数
- codeforces C. Pythagorean Triples (【数学】毕达哥拉斯三元数组 勾股定理)
- Codeforces Round #368 (Div. 2) problem: (C) Pythagorean Triples
- 实现音乐播放器歌词显示效果
- hihocoder 1362 修补木桶
- 程序设计竞赛训练手册
- 利用SpringMvc和MyBatis实现员工列表显示页面
- 准备在CSDN安家啦。
- Codeforces Problem 707C Pythagorean Triples(数学)
- Codeforces Round #364 (Div. 2) A. Cards
- C基础
- 命令熟悉
- POJ 2186 Popular Cows Korasaju .
- Expedition(优先队列)
- debian sudo自动补全
- 不能打开到主机的连接, 在端口 1521: 连接失败
- 内部排序