zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    【笔记】C++自学笔记系列02:类的组合与类的友元
    【windows程序设计】系列02:显示屏幕分辨率
    【笔记】C++自学笔记系列01:基础知识
    【windows程序设计】系列03:窗口与消息
    【windows程序设计】系列04:文本输出
    【Boost】系列05:数值与字符串转换
    【windows程序设计】系列01
    【Boost】系列04:实用技巧之禁止类拷贝构造和赋值
    【gdb】基本操作
    性能测试基本流程介绍(《软件性能测试过程详解与安全剖析》)
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/9700019.html
Copyright © 2011-2022 走看看