1041: [HAOI2008]圆上的整点
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2196 Solved: 941
[ Submit][ Status]
Description
求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。
Input
r
Output
整点个数
Sample Input
4
Sample Output
4
HINT
n<=2000 000 000
接下来枚举d,a,判断求出的b是否和题意即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
LL r;
int gcd(LL a,LL b)
{
return a==0LL?b:gcd(b%a,a);
}
bool ok(LL a,LL b)
{
if (a==b||gcd(a,b)!=1LL) return false;
return true;
}
int main()
{
scanf("%lld",&r);
LL ans=0;
for (LL d=1LL;d<=sqrt(2LL*r);d++)
if ((2LL*r)%d==0LL)
{
for (LL a=1;a<=sqrt(r/d);a++)
{
LL b=sqrt(2LL*r/d-a*a);
if (b*b!=(2LL*r/d-a*a)) continue;
if (ok(a,b)) ans++;
}
if (d*d!=2LL*r)
{
for (LL a=1LL;a<=sqrt(d/2);a++)
{
LL b=sqrt(d-a*a);
if (b*b!=d-a*a) continue;
if (ok(a,b)) ans++;
}
}
}
cout<<ans*4LL+4LL<<endl;
return 0;
}
感悟:
1.其实这个题挺好推的,关键是有勇气推下去。。