Pythagoras's Revenge | 프로그래밍의 벗 PivotOJ
PivotOJ

Pythagoras's Revenge

시간 제한: 1000ms메모리 제한: 128MB출처: ICPC Mid-Central Regional 2012BOJ 4563
이 문제는 본문 이미지 일부가 표시되지 않습니다. 텍스트만으로 풀이가 어려울 수 있습니다.

문제

The famous Pythagorean theorem states that a right triangle, having side lengths A and B 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: 

[이미지 1]

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

[이미지 2]

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?

입력

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.

출력

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

힌트

Our hint is that you need not consider any value for B that is greater than (A2-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 ≈ 220, there could be solutions with B ≈ 239, and thus values of C2 > 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 of C2 for such an extreme case. (Which is, after all, what makes this Pythagoras's revenge!)

예제

예제 1

입력
3
12
2
1048574
1048575
0
출력
1
2
0
1
175
코드를 제출하려면 로그인하세요.