zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    
    


     

  • 相关阅读:
    深入理解JavaScript的闭包特性 如何给循环中的对象添加事件
    兼容低版本浏览器的getElementByClassName方法
    印象深刻的bug
    pyinstaller将python编写的打卡程序打包成exe
    自动化环境搭建遇到问题
    VS2010带不出System.Data.OracleClient这个引用的解决方案
    迭代与列表生成式、生成器
    Python函数
    Python基础
    python+Selenium 环境搭建
  • 原文地址:https://www.cnblogs.com/riskyer/p/3291961.html
Copyright © 2011-2022 走看看