HUNNU11351:Pythagoras's Revenge

来源:互联网 发布:java怎么调用数组 编辑:程序博客网 时间:2024/06/07 05:56

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 andB and hypotenuse length C, satisfies the formula

A2 +B2 =C2

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 lengthC is integral?



Input  

Each line contains a single integer A, such that 2 ≤ A < 1048576 = 220. 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 andB has a hypotenuse with integral length.

Sample Input
3122104857410485750
Sample Output
1201175
Judge Tips  

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

Our warning is that for values of A ≈ 220, there could be solutions withB ≈ 239, and thus values ofC2 >B2 ≈ 278.

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