codeforces 707C C. Pythagorean Triples(数学)

题目链接:

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 calledPythagorean 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 nm 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和2的时候不可能,其他的时候都行;
偶数的时候有2*n,n*n-1,n*n+1;
奇数的时候有2*n+1,2*n*n+2*n,2*n*n+2*n+1;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+20;
const double eps=1e-12;



int main()
{
    LL n,m;
    read(n);
    if(n==1||n==2)cout<<"-1
";
    else 
    {
        m=n/2;
        if(n%2)cout<<2*m*m+2*m<<" "<<2*m*m+2*m+1<<endl;
        else cout<<m*m-1<<" "<<m*m+1<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5792706.html