【bzoj1041】[HAOI2008]圆上的整点

1041: [HAOI2008]圆上的整点

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 4258   Solved: 1933
[ Submit][ Status][ Discuss]

Description

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

Input

只有一个正整数n,n<=2000 000 000

Output

整点个数

Sample Input

4

Sample Output

4

HINT

Source

[ Submit][ Status][ Discuss]



。。。数学真难

感觉这种尿性的题好像以前初中数学竞赛有学过


但是忘了。。。。。。。。。。。。。。。。。。。。。。。



无奈百度题解。。


然后惊呼水题


这题很明显要推导推导
a ^ 2 + b ^ 2 = r ^ 2
变形一下b = sqrt((r - a)(r + a))
如果r - a == r + a
那么a = 0 明显不符题意

那么r - a != r + a

这时我们设一下g = gcd(r + a,r - a)
再设g * x = r + a, g * y = r - a
然后就有b = sqrt(g * g * x * y)

于是可以发现x * y一定是一个完全平方数
又考虑x 和 y互质,所以x和y两个数一定是完全平方数

再设i ^ 2 = x,j ^ 2 = y
于是有
i ^ 2 = (r + a) / g
j ^ 2 = (r - a) / g

两式相加,得
i ^ 2 + j ^ 2 = 2 * r / g
于是可以发现g必然是2 * r的约数

那么我们只需要枚举每一个g,再枚举每一个i即可

复杂度O(sqrt(r) * sqrt(sqrt(r)))

注意:
考虑到不论是i > j 还是 i < j ,对于b的取值来说都没有影响,因此我们在统计答案的时候只需要考虑一个即可
特别的,如果i == j,那么就是满足r - a == r + a,这时是不在讨论范围之内的,可以直接舍去
如果i == 0 || j == 0,也不符题意,需要舍去

代码:
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;

const double EPS = 1e-7;

LL ans;
LL r;

inline LL gcd(LL a,LL b) {return !b ? a : gcd(b,a % b);}

inline LL getint()
{
	LL ret = 0,f = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		ret = ret * 10 + c - '0',c = getchar();
	return ret * f;
}

int main()
{
	r = getint();
	if (!r) {printf("1"); return 0;}
	LL a,b;
	for (LL d = 1; d <= sqrt(2 * r); d++)
	{
		if (2LL * r % d) continue;
		for (a = 1; a <= sqrt(2 * r / d); a++)
		{
			b = sqrt(2 * r / d - a * a);
			if (a >= b) continue;
			if (b * b != 2 * r / d - a * a) continue;
			if (!b) continue;
			if (gcd(a,b) != 1) continue;
			ans++;
		}
		for (a = 1; a <= sqrt(d); a++)
		{
			b = sqrt(d - a * a);
			if (a >= b) continue;
			if (b * b != d - a * a) continue;
			if (!b) continue;
			if (gcd(a,b) != 1) continue;
			ans++;
		}
	}
	printf("%lld",ans * 4 + 4);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值