zoukankan      html  css  js  c++  java
  • codeforces707C:Pythagorean Triples

    Description

    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



    正解:数学(数论)
    解题报告:
      n<=2显然无解。
      若n为奇数,则另两个为(a*a-1)/2和(a*a+1)/2;
      若n为偶数,则另两个为(a/2)*(a/2)-1和(a/2)*(a/2)+1

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<algorithm>
     7 using namespace std;
     8 typedef long long LL;
     9 LL n;
    10 LL a,b;
    11 
    12 inline int getint(){
    13     int w=0,q=1;char c=getchar();
    14     while(c!='-' && (c<'0' || c>'9')) c=getchar();
    15     if(c=='-') q=-1,c=getchar();
    16     while(c>='0' && c<='9') w=w*10+c-'0',c=getchar();
    17     return w*q;
    18 }
    19 
    20 int main()
    21 {
    22     n=getint();
    23     if(n<=2) printf("-1");
    24     else if(n&1) {
    25     a=n*n-1; a/=2; b=a+1;
    26     printf("%I64d %I64d",a,b);
    27     } 
    28     else {
    29     n/=2;
    30     a=n*n-1; b=n*n+1;
    31     printf("%I64d %I64d",a,b);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    多线程学习笔记第一篇
    当Visual Studio中win32控制台应用程序的注释也会生产代码时……
    博客行文及排版技法
    Debian Linux下的Python学习——安装python,vim
    onhashchange事件
    MyEclipse9 Maven开发Web工程 详细配置
    Java面向对象(上)
    lucene 的分析器(analyzer)与分词器(tokenizer)和过滤器(tokenfilter)
    java编程陷阱
    solr中文分词(mmseg4j)
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5793917.html
Copyright © 2011-2022 走看看