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

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1041

【题意】

【题解】

X^2+Y^2=R^2
X^2=R^2-Y^2
X^2=(R+Y)(R-Y)

D = gcd(R+Y,R-Y);

gcd((R+Y)/D,(R-Y)/D)==1

X^2=D*u^2*D*v^2

Y=D*(v^2-u^2)/2
X=D*u*v
R=D*(u^2+v^2)/2

(具体的上界取sqrt((R+1)/d),不能取等号,不然会出现Y为0的情况(即U=V).而那种情况我们最后结果会加上去；

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;

LL r,ans = 0;
vector <LL> v;

void in()
{
rel(r);
}

void get_divisor(LL x)
{
LL i;
for (i = 1; i*i < x; i++)
if (x%i == 0)
v.push_back(i), v.push_back(x / i);
if (i*i == x)
v.push_back(i);
}

bool is_sqr(LL x)
{
double t = sqrt((double)x);
if (fabs(floor(t + 1e-7) - t) < 1e-7)
return true;
return false;
}

LL gcd(LL a, LL b)
{
if (b == 0)
return a;
else
return gcd(b, a%b);
}

void get_ans()
{
LL V;
int len = v.size();
rep1(i, 0, len - 1)
{
LL d = v[i];
for (V = 1; V*V < (r + 1) / d; V++)
{
LL U_2 = 2 * r / d - V*V;
if (is_sqr(U_2))
if (gcd(V*V, U_2) == 1)
{
ans++;
}
}
}
}

void o()
{
printf("%lld\n", ans*4+4);
}

int main()
{
//freopen("F:\\rush.txt", "r", stdin);
in();
if (r == 1)
{
puts("4");
return 0;
}
get_divisor(r << 1);
get_ans();
o();
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

posted @ 2017-10-04 18:45  AWCXV  阅读(65)  评论(0编辑  收藏