1041: [HAOI2008]圆上的整点
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 2951 Solved: 1296
[Submit][Status][Discuss]
Description
求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。
Input
r
Output
整点个数
Sample Input
4
Sample Output
4
HINT
n<=2000 000 000
#include<stdio.h>
#include<cmath>
#include<string.h>
#define LL long long
LL r;
LL ans;
LL gcd(LL x,LL y)
{
return x%y==0?y:gcd(y,x%y);
}
bool check(LL y,double x)
{
if(x==floor(x))
{
LL x1=(LL)floor(x);
if(gcd(x1*x1,y*y)==1&&x1*x1!=y*y)
{
return true;
}
}
return false;
}
int main()
{
scanf("%lld",&r);
for(LL d=1;d<=sqrt(2*r);d++)
{
if((2*r)%d==0)
{
for(LL a=1;a<=(LL)sqrt(2*r/(2*d));a++)
{
double b=sqrt(((2*r)/d)-a*a);
if(check(a,b))ans++;
}
if(d!=(2*r)/d)
{
for(LL a=1;a<=(LL)sqrt(d/2);a++)
{
double b=sqrt(d-a*a);
if(check(a,b))
ans++;
}
}
}
}
printf("%lld\n",ans*4+4);
return 0;
}