1041: [HAOI2008]圆上的整点
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4258 Solved: 1933
[ Submit][ Status][ Discuss]
Description
求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。
Input
只有一个正整数n,n<=2000 000 000
Output
整点个数
Sample Input
4
Sample Output
4
HINT
Source
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const double EPS = 1e-7;
LL ans;
LL r;
inline LL gcd(LL a,LL b) {return !b ? a : gcd(b,a % b);}
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
int main()
{
r = getint();
if (!r) {printf("1"); return 0;}
LL a,b;
for (LL d = 1; d <= sqrt(2 * r); d++)
{
if (2LL * r % d) continue;
for (a = 1; a <= sqrt(2 * r / d); a++)
{
b = sqrt(2 * r / d - a * a);
if (a >= b) continue;
if (b * b != 2 * r / d - a * a) continue;
if (!b) continue;
if (gcd(a,b) != 1) continue;
ans++;
}
for (a = 1; a <= sqrt(d); a++)
{
b = sqrt(d - a * a);
if (a >= b) continue;
if (b * b != d - a * a) continue;
if (!b) continue;
if (gcd(a,b) != 1) continue;
ans++;
}
}
printf("%lld",ans * 4 + 4);
return 0;
}