[HAOI2008]圆上的整点

题目描述

求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。

输入输出格式

输入格式:

r

输出格式:

整点个数

输入输出样例

输入样例#1:
4
输出样例#1:
4

说明

n<=2000 000 000

接下来枚举d,a

为什么要除d?

因为他们不互质,a*b是完全平方数≠a,b都是完全平方数

记住还要a*a,b*b互质

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 long long r,k,ans,p,q;
 8 int tot;
 9 long long ys[500001];
10 void Dvide(long long x)
11 {int i;
12   for (i=1;i<=sqrt(x);i++)
13     if (x%i==0)
14       {
15     if (i*i==x)
16       {
17         tot++;
18         ys[tot]=i;
19       }
20     else
21       {
22         tot++;
23         ys[tot]=i;
24         tot++;
25         ys[tot]=x/i;
26       }
27       }
28 }
29 long long GCD(long long a,long long b)
30 {
31   if (b==0)
32     return a;
33   return GCD(b,a%b);
34 }
35 int main()
36 {int i;
37   cin>>r;
38   Dvide(2*r);
39   for (i=1;i<=tot;i++)
40     {
41       for (p=1;p*p<(r/ys[i]);p++)
42     {
43       q=sqrt(2*r/ys[i]-p*p);
44       if (p*p+q*q==2*r/ys[i])
45         if (GCD(p*p,q*q)==1) ans++;
46     }
47     }
48   cout<<ans*4+4;
49 }
原文地址:https://www.cnblogs.com/Y-E-T-I/p/7554192.html