Codeforces Round #368 (Div. 2)C.Pythagorean Triples 本原勾股数组

来源:互联网 发布:打谱软件overture下载 编辑:程序博客网 时间:2024/05/25 19:55
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.

这个题目用到了数论的知识,本原勾股数组。下面的推理都是参考《数论概论》第九页的本原勾股数组。部分书里未给出的证明会详细写出。

首先给出

a^2 + b^2 = c^2中,a,b奇偶性相异证明,原书就很明白了:



下图书中用(c-b)与(c+b)没有公因子就得出二者都是完全平方数的证明不是很直观。

做证明:

因为a^2 = (c - b)×(c + b),这样对a分解质因数,则每种因子至少有2个或者是大于2的偶数个,

因为有2个a。而c-b和c+b除了1没有公因子,那么就说明每一种因子要不全在c-b中要不全在c+b中,

比如:

a=30=2*3*5  a^2=2×3×5×2×3×5  2跟2如果分别在c-b,c+b中,则公因子至少有个2,肯定不是1.

这样可能有c-b = 2×2 c+b=3×3×5×5=15×15 ,这样每个数里面都有偶数种因子,就可以凑出完全平方数。

所以没有公因子就都是完全平方数了。



当a是奇数的时候,通过s, (s^2-1)/2, (s^2 + 1)/2 就可以直接求出另外两个数。

当a是偶数的时候呢?上面证明中有一段是证明(c-b)和(c+b)没有公因子的,只有这一点证明出来才可以保证(c-b)

(c+b)都是完全平方数,才有最终的答案,而因为c+b + c-b = 2c c+b-(c-b)=2b c,b开始假设就是没有公因子的。

这样c+b c-b的公因子只有1 2,因为a前提是奇数所以2没有了。


现在a是偶数了,也就是说2也是公因子了,只有两数除去公约数2才可以当作(c-b) (c+b)是完全平方数的情况处理。

a^2 = (c-b)×(c+b) 等式除去公因子就是:

a^2/4 =(c-b)/2×(c+b)/2

这样用a1=(a/2) 得到都是完全平方数的式子:

a1^2=(c1-b1)×(c1+b1)

b1=(a1^2 - 1)/2

c1=(a1^2 + 1)/2

但这不是最终答案,因为a除了2

因为 a^2+b^2=c^2

等式两边同时乘一个数等式仍然成立   

4×a1^2 + 4×b1^2 = 4×c1^2

(a1×2)^2 + (b1×2)^2 = (c1×2)^2

所以c=c1×2 b=b1×2这才是最终答案。

c=c1×2=(a1^2+1)/2×2=a1^2+1=(a/2)^2 + 1

if a是偶数

a=a/2

b=a^2-1

c=a^2+1

代码就很简单了:

n=input()if n < 3:    print -1elif n&1:    print (n*n - 1)/2, (n*n + 1)/2else:    n >>= 1    print n*n - 1, n*n + 1

最小的勾股数是3,4,5小于3自然就没有答案了。

0 0
原创粉丝点击