Codeforces #368(Div.2)C. Pythagorean Triples【勾股数】

来源:互联网 发布:阿里推荐算法 编辑:程序博客网 时间:2024/05/28 15:08

C. Pythagorean Triples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 ≤ 109) — the length of some side of a right triangle.


Output
Print two integers m and k (1 ≤ m, k ≤ 1018), 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.


Examples
Input
3
Output
4 5
Input
6
Output
8 10
Input
1
Output
-1
Input
17
Output
144 145
Input
67
Output
2244 2245


题目大意:

给你一条直角边,输出其另一条直角边和斜边,如果不存在,输出-1。解要保证是整数。


思路:


1、首先输入1和2一定无解。


2、输入的数当是一个奇数并且大于1的时候,那么我们枚举出几个小数据,发现有这样的规律:

a*a=b+c

并且在勾股数中有:

a=2mn

b=m^2-n^2

c=m^2+n^2

那么我们列出式子4m^2n^2=m^2-n^2+m^2+n^2

整理有:4m^2n^2=2m^2------------>n^2=1/2

因为有a=2mn------------>a^2=4m^2n^2 ------------->a^2=2m^2------------>m^2=a^2/2;

那么b=m^2-n^2=a^2/2-1/2;

那么c=M^2+n^2=a^2/2+1/2;


3、当输入为大于4的偶数2n时,b=n^2-1, c=n^2+1。当输入的是4的时候,特殊判定一下即可。


4、数据较大,注意使用LL

Ac代码:


#include<stdio.h>#include<string.h>using namespace std;#define ll __int64int main(){    ll n;    while(~scanf("%I64d",&n))    {        if(n==1||n==2)        {            printf("-1\n");continue;        }        if(n%2==1)        {            n=n*n;            ll b=(n-1)*1.0/2*1.0;            ll c=(n+1)*1.0/2*1.0;            printf("%I64d %I64d\n",b,c);        }        else        {            if(n==4)            {                printf("3 5\n");            }            else            {                n/=2;                ll b=n*n-1;                ll c=n*n+1;                printf("%I64d %I64d\n",b,c);            }        }    }}



0 0
原创粉丝点击