Codeforces Round #368 (Div. 2)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.

這個題目用到了數論的知識,本原勾股數組。下面的推理都是參考《數論概論》第九頁的本原勾股數組。部分書裏未給出的證明會詳細寫出。

首先給出

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 -1
elif n&1:
    print (n*n - 1)/2, (n*n + 1)/2
else:
    n >>= 1
    print n*n - 1, n*n + 1

最小的勾股數是3,4,5小於3自然就沒有答案了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章