码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ 1041】 [HAOI2008]圆上的整点

时间:2015-01-25 13:57:09      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:bzoj   oi   数学推导   

1041: [HAOI2008]圆上的整点

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2196  Solved: 941
[Submit][Status]

Description

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

Input

r

Output

整点个数

Sample Input

4

Sample Output

4

HINT

n<=2000 000 000


技术分享


接下来枚举d,a,判断求出的b是否和题意即可。


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
LL r;
int gcd(LL a,LL b)
{
	return a==0LL?b:gcd(b%a,a);
}
bool ok(LL a,LL b)
{
	if (a==b||gcd(a,b)!=1LL) return false;
	return true;
}
int main()
{
        scanf("%lld",&r);
	LL ans=0;
	for (LL d=1LL;d<=sqrt(2LL*r);d++)
		if ((2LL*r)%d==0LL)
		{
			for (LL a=1;a<=sqrt(r/d);a++)
			{
				LL b=sqrt(2LL*r/d-a*a);
				if (b*b!=(2LL*r/d-a*a)) continue;
				if (ok(a,b)) ans++;
			}
			if (d*d!=2LL*r)
			{
				for (LL a=1LL;a<=sqrt(d/2);a++)
				{
					LL b=sqrt(d-a*a);
					if (b*b!=d-a*a) continue;
					if (ok(a,b)) ans++;
				}
			}
		}
	cout<<ans*4LL+4LL<<endl;
	return 0;
}

技术分享


感悟:

1.其实这个题挺好推的,关键是有勇气推下去。。

【BZOJ 1041】 [HAOI2008]圆上的整点

标签:bzoj   oi   数学推导   

原文地址:http://blog.csdn.net/regina8023/article/details/43112801

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!