• HUNNU11351:Pythagoras's Revenge


    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11351&courseid=0

    Problem description

      

    The famous Pythagorean theorem states that a right triangle, having side lengthsA and B and hypotenuse length C, satisfies the formula

    A 2 + B 2 = C 2

    It is also well known that there exist some right triangles in which all three side lengths are integral, such as the classic:

    Further examples, both having A=12, are the following:

    The question of the day is, given a fixed integer value for A, how many distinct integersB > A exist such that the hypotenuse length C is integral?



    Input
      

    Each line contains a single integer A, such that 2 ≤ A < 1048576 = 2 20 . The end of the input is designated by a line containing the value 0.

    Output
      

    For each value of A, output the number of integers B > A such that a right triangle having side lengthsA and B has a hypotenuse with integral length.

    Sample Input
    3
    12
    2
    1048574
    1048575
    0
    Sample Output
    1
    2
    0
    1
    175
    Judge Tips
      

    A Hint and a Warning: Our hint is that you need not consider any value forB that is greater than ( A 2-1)/2 , because for any such right triangle, hypotenuse C satisfies B < C < B + 1 , and thus cannot have integral length.

    Our warning is that for values of A ≈ 2 20 , there could be solutions with B ≈ 2 39 , and thus values of C 2 > B 2 ≈ 2 78.

    You can guarantee yourself 64-bit integer calculations by using the type long long in C++ or long in Java. But neither of those types will allow you to accurately calculate the value ofC2 for such an extreme case. (Which is, after all, what makes thisPythagoras's revenge!)


     

    题意:给出一条最短的直角边,要求另外两边都是整数的直角三角形的个数

    思路:根据勾股定理a^2+b^2=c^2

    可得:a^2=(c-b)(c+b)

    令x = c-b;

    y=c+b;

    又y-x=2*b;

    所以b=(y-x)/2;

    并且需要b>a,所以只需要对x进行枚举即可

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    
    int main()
    {
        while (true)
        {
            long long a;
            cin >> a;
            if (a == 0) break;
    
            long long count;
            count = 0;
            for (long long x=1; x <= a/2; x++)
            {
                if (a*a % x == 0)
                {
                    long long y = a*a / x;
                    if ((y-x)%2 == 0)
                    {
                        long long b = (y-x)/2;
                        if (b > a)
                        {
                            count++;
                        }
                    }
                }
            }
            cout << count << endl;
        }
    }
    
    


     

  • 相关阅读:
    怎样改动、扩展并重写Magento代码
    解决Gradle minifyEnabled无法找到错误
    使用Hadoop的MapReduce与HDFS处理数据
    cmake 学习笔记(一)
    简单的日志系统
    WebStorm 7.0 注冊码
    DOS命令大全--具体解释
    SQL SERVER之数据查询
    闰年的定义
    Javascript作用域链
  • 原文地址:https://www.cnblogs.com/riskyer/p/3291961.html
Copyright © 2020-2023  润新知