1147.Pythagoras's Revenge
Description
The famous Pythagorean theorem states that a right triangle, having side lengths A 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 integers B > A exist such that the hypotenuse length C is integral?
Input
Output
Sample Input
3 12 2 1048574 1048575 0
Sample Output
1 2 0 1 175
Hint
Our hint is that you need not consider any value for B 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 of C2 for such an extreme case. (Which is, after all, what makes thisPythagoras's revenge!)
Source
题目链接:
SDNU 1147 Pythagoras's Revenge
题目大意:
解题思路:
Mycode:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 10005;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
int main()
{
LL a, b, ans;
while(scanf("%lld",&a) && a)
{
ans = 0;
for(LL i = 1; i < a; ++i)
{
if(a * a % i == 0)
{
LL j = a * a / i;
if((j-i) % 2 == 0)
{
b = (j-i) / 2;
if(b > a)
++ans;
}
}
}
printf("%lld\n",ans);
}
return 0;
}