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 }
  • 相关阅读:
    博客园添加侧边栏小插件并更改css样式
    ubuntu14.04环境下利用docker搭建solrCloud集群
    使用Grunt 插件打包Electron Windows应用
    使用Squirrel创建基于Electron开发的Windows 应用安装包
    Electron实战:创建ELectron开发的window应用安装包
    6、创建-查看-复制-删除-文件和文件夹相关命令
    5、Linux 系统基本文件管理
    3、Linux 获取帮助的方法-关机命令-7个系统启动级别
    2 、Linux基本命令-ls-pwd-cd-date-hwclock
    1 、Linux-Rhel6终端介绍-Shell提示符
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/9700019.html
Copyright © 2011-2022 走看看