1. 程式人生 > >【bzoj1041】[HAOI2008]圓上的整點 數論

【bzoj1041】[HAOI2008]圓上的整點 數論

個數 描述 cst scan images 多少 family pri microsoft

題目描述

求一個給定的圓(x^2+y^2=r^2),在圓周上有多少個點的坐標是整數。

輸入

只有一個正整數n,n<=2000 000 000

輸出

整點個數

樣例輸入

4

樣例輸出

4


題解

數論

技術分享

#include <cmath>
#include <cstdio>
typedef long long ll;
ll judge(ll k)
{
	ll t = (ll)sqrt(k);
	return t * t == k ? t : 0;
}
ll gcd(ll a , ll b)
{
	return b ? gcd(b , a % b) : a;
}
ll calc(ll k)
{
	ll i , t , ans = 0;
	for(i = 1 ; i * i <= k / 2 ; i ++ )
	{
		t = judge(k - i * i);
		if(t && gcd(i , t) == 1) ans ++ ;
	}
	return ans;
}
int main()
{
	ll n , i , ans = 0;
	scanf("%lld" , &n);
	for(i = 1 ; i * i <= 2 * n ; i ++ )
	{
		if(2 * n % i == 0)
		{
			ans += calc(i);
			if(i * i != 2 * n) ans += calc(2 * n / i);
		}
	}
	printf("%lld\n" , ans * 4);
	return 0;
}

【bzoj1041】[HAOI2008]圓上的整點 數論