• codeforces 707C


    C. Pythagorean Triples
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 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.

    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


    题意:给你一个数,问能否找出另外任意两个数和这个数组成勾股数,
    分析:..首先想到的就是之前看的一个勾股数组定理的结论(证明当时没记 (*^▽^*)!!!);
    a 为奇数 b为偶数 c 奇数
    a = st;
    b = (s * s - t * t) / 2;
    c = (s * s + t * t) / 2;
    s > t >= 1
    然后可以缩小n, 缩小到为奇数为止,并记录缩小的倍数ans;
    如果此时n = 1,那可以再把n扩大到4,此时ans /= 4;(3,4,5是一组勾股数)
    让m = 3 * ans, k = 4 * ans;(缩小扩大相同的倍数不影响勾股数之间的关系)
    如果n = 2 * p + 1(奇数);
    那让n 为上面结论的a = s * t;
    也就是找a的两个因子,直接就让t = 1;s = n;
    然后m = (s * s - t * t) / 2 * ans;
           k = (s * s + t * t) / 2 * ans;
    如果m或者k为0,则无法构成勾股数,输出-1;
     1 #include <bits/stdc++.h>
     2 #define int long long
     3 using namespace std;
     4 const int maxn = 1e6+5;
     5 
     6 signed main()
     7 {
     8     int n, m, k;
     9     while(~scanf("%lld", &n))
    10     {
    11         if(n == 1)
    12         {
    13             printf("-1
    ");
    14             continue;
    15         }
    16         int s, t;
    17         int ans = 1;
    18         int tep = 2048;
    19         while(tep != 1)
    20         {
    21             while(n % tep == 0)
    22             {
    23                 n /= tep;
    24                 ans *= tep;
    25             }
    26             tep /= 2;
    27         }
    28         if(n == 1)
    29         {
    30             n = 4;
    31             ans /= 4;
    32             m = 3 * ans;
    33             k = 5 * ans;
    34         }
    35         else if(n % 2)
    36         {
    37             s = n;
    38             t = 1;
    39             m = (s * s - t * t) / 2 * ans;
    40             k = (s * s + t * t) / 2 * ans;
    41         }
    42         if(!m || !k)
    43         {
    44             printf("-1
    ");
    45         }
    46         else
    47             printf("%lld %lld
    ", m, k);
    48     }
    49     return 0;
    50 }
    51 
    52 /*
    53 
    54 
    55 */
  • 相关阅读:
    Coursera 高级数据结构与算法,北京大学
    heap 算法函数
    笛卡尔树 Cartesian tree
    POJ 1830 开关问题 异或高斯消元
    jmeterJSR223取样器和断言
    Flask JinJa2模板语法测试
    Python Flask Web 试题汇编<待续>
    jsDoc自动生成文档
    SQL 优化
    IO多路复用之select、poll、epoll的区别
  • 原文地址:https://www.cnblogs.com/mashen/p/11398290.html
Copyright © 2020-2023  润新知