• 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?

    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
    Copy
    3
    output
    Copy
    4 5
    input
    Copy
    6
    output
    Copy
    8 10
    input
    Copy
    1
    output
    Copy
    -1
    input
    Copy
    17
    output
    Copy
    144 145
    input
    Copy
    67
    output
    Copy
    2244 2245
    Note

    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
    ");
    14             else
    15             printf("%lld %lld
    ",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
    ");
    46             else
    47             printf("%lld %lld
    ",b,c);
    48         }
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    省市区选择器
    查找算法 分享1:顺序查找
    查找算法 分享2:折半(二分)查找
    编程实现对键盘输入的英文名句子进行加密。用加密方法为,当内容为英文字母时其在26字母中的其后三个字母代替该字母,若为其它字符时不变。
    查找算法 分享3:分块查找
    NSMutableDicitionary 的setValue和setObject的区别
    查找算法 分享4:哈希查找
    玩转博客园的5个小技巧
    WCF读书笔记安全:基础知识(身份验证、授权、传输安全)
    北京的房价
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/9700019.html
Copyright © 2020-2023  润新知