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;
        }
    }
    
    


     

  • 相关阅读:
    使 div 元素看上去像一个按钮
    ORM框架学习(二)
    ORM框架学习(一)
    创建我的第一个MVC4-基于ASPX
    js实现简单的验证码
    常见开发语言擅长领域
    解决IE兼容问题,IE6,IE7,IE8,IE9,IE10
    EF框架学习(7)---EF中的查询方法
    EF框架学习(6)---EF中的在线场景保存数据
    EF框架学习(5)---EF中的在线和离线场景
  • 原文地址:https://www.cnblogs.com/riskyer/p/3291961.html
Copyright © 2011-2022 走看看