Pythagorean Triples 707C
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?
The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.
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.
3
4 5
6
8 10
1
-1
17
144 145
67
2244 2245
Illustration for the first sample.
题意:给你一条直角边,让你求出另一条直角边与斜边,如果没有,则输出-1
分析:a²+b²=c²,a²=(c-b)(c+b),如果a是奇数,c-b=1,c+b=a*a,c=a*a+1/2, b=c-1.如果a是偶数,先判断,1:如果不断的除2,是否是奇数,是奇数就执行c=a*a+1/2, b=c-1;2:当其不断除2等于4时,执行c=5,b=3;
1 #include<cstdio> 2 int main() 3 { 4 long long n; 5 while(~scanf("%lld",&n)) 6 { 7 long long b,c; 8 if(n%2!=0)//当直角边时奇数的时候直接执行 9 { 10 b=(n*n-1)/2; 11 c=b+1; 12 if(b==0) 13 printf("-1\n"); 14 else 15 printf("%lld %lld\n",b,c); 16 } 17 else//当直角边是偶数的时候 18 { 19 long long t=1; 20 while(1) 21 { 22 if(n==4) 23 break; 24 if(n%2!=0) 25 break; 26 n/=2; 27 t*=2; 28 29 }//不断除2,看其是否为奇数,或者不断除2等于4 30 if(n==4) 31 { 32 b=3; 33 c=5; 34 b*=t; 35 c*=t; 36 }//如果等于4就满足,a=4,b=3,a=5的情况 37 else 38 { 39 b=(n*n-1)/2; 40 c=b+1; 41 b*=t; 42 c*=t; 43 }//否则就满足a是奇数的情况 44 if(b==0) 45 printf("-1\n"); 46 else 47 printf("%lld %lld\n",b,c); 48 } 49 } 50 return 0; 51 }