P2508 [HAOI2008]圆上的整点

2022-09-19 08:16:21 浏览数 (1)

Description

题目链接:P2508

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

rleq 2times 10^9

Solution

begin{align}x^2 y^2=r^2 & Leftrightarrow y=sqrt{r^2-x^2}& Leftrightarrow y=sqrt{(r x)(r-x)}end{align}

,则有

y^2=d^2ABLeftrightarrow AB=(frac yd)^2

gcd(A,B)=1

A,B 分别为平方数,即 A=i^2,B=j^2,(i,jin Z)

那么:

i^2=frac{r x}dj^2=frac{r-x}d

两式相加得:

i^2 j^2=frac {2r}d

那么枚举一下

时间复杂度:mathcal O(N)

Code

代码语言:javascript复制
#include<bits/stdc  .h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define W while
#define I inline
#define RI register int
#define int long long
#define Cn const
#define CI Cn int&
#define gc getchar
#define D isdigit(c=gc())
#define pc(c) putchar((c))
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
using namespace std;
namespace Debug{
    Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;}
    Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f  ;cerr<<'='<<x<<",";_debug(f 1,y...);}
    Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;}
    #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
namespace FastIO{
    Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3) (x<<1) (c&15),D);x*=f;}
    Ts I void read(Ty& x,Ar&... y){read(x),read(y...);}
    Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x '0'),0):(write(x/10),pc(x '0'),0);}
    Tp I void writeln(Cn Ty& x){write(x),pc('n');}
}using namespace FastIO;
int r,Ans;
I int gcd(CI a,CI b){return !b?a:gcd(b,a%b);}
I void Work(CI d){
    RI i,j,x,y;for(i=1;i*i<=r/d;i  ){
        j=sqrt(r/d-i*i);
        if(j*j i*i==r/d&&gcd(i,j)==1){
            x=d*i*i-r/2,y=sqrt((r/2 x)*(r/2-x));
            if(x>0&&y>0&&x*x y*y==(r/2)*(r/2)) Ans  ;
        }
    }
}
signed main(){
    RI i,j;for(read(r),r*=2,i=1;i*i<=r;i  ) if(!(r%i)) Work(i),i*i<r&&(Work(r/i),0);
    return writeln((Ans 1)*4),0;
}
ode

0 人点赞