P2508 [HAOI2008] 圆上的整点

Solution

移项,得 \(x^2=r^2-y^2=(r-y)(r+y)\)

\(d=\gcd(r-y,r+y),r-y=d\cdot a,r+y=d\cdot b\)

\(x^2=d^2\cdot a\cdot b\),由于 \(a,b\) 互质,因此 \(a,b\) 均为完全平方数,设 \(a=i^2,b=j^2\)

\[\begin{cases} i^2+j^2=\frac{2r}{d}\\ x=d*i*j\\ y=\frac{j^2-i^2}{2}d \end{cases} \]

先枚举 \(2r\) 的因数,在枚举 \(i\),求出 \(j\),最后检验是否满足条件即可。

Code

#include<bits/stdc++.h>
#define IOS cin.tie(0),cout.tie(0),ios::sync_with_stdio(0)
#define ll long long
#define db double
#define pb push_back
#define eb emplace_back
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define PLL pair<ll,ll>
#define PII pair<int,int>
//#define Tp template<typename T>
//#define Ts template<typename T,typename ...args>
#define lb(x) ((x)&(-x))
using namespace std;
const int N=1e5+20,M=1e5+20;
const ll INF=1ll<<60,mod=998244353;
namespace H_H{
	ll r;
	ll gcd(ll a,ll b){
		if(!b) return a;
		return gcd(b,a%b);
	}
	ll calc(ll d){
		ll ans=0;
		for(ll i=1;i*i<=r/d;i++){
			ll j=sqrt(r/d-i*i);
			if(i*i+j*j==r/d && gcd(i,j)==1){
				ll x=(i*i-j*j)/2*d,y=i*j*d;
				if(x>0 && y>0 && x*x+y*y==(r/2)*(r/2)) ans+=2;
			}
		}
		return ans;
	}
	int main(){
		cin>>r;r<<=1;
		ll ans=0;
		for(ll d=1;d*d<=r;d++){
			if(r%d) continue;
			ans+=calc(d);
			if(d*d!=r) ans+=calc(r/d); 
		}
		ans=ans*4+4;
		cout<<ans<<"\n";
		return 0;
	}
}
int main(){
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
	IOS;H_H::main();
	return 0;
}
posted @ 2026-01-11 14:58  tyh_27  阅读(2)  评论(0)    收藏  举报