【数论】[圆点坐标]P2508圆上的整点

题目描述

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

Solution

圆上的点坐标通解:\(x = d\frac{v^2-u^2}{2},y = duv, r = \frac{d(v^2-u^2)}{2}\)

枚举2r的因子d,对每个d枚举u,然后判断\(v^2\)是否是完全平方数,以及v与u是否互质。这样求出的答案再乘以4,再加上4(就是圆与坐标轴的交点)就好了。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
inline long long read() {
  long long x = 0; int f = 0; char c = getchar();
  while (c < '0' || c > '9') f |= c == '-', c = getchar();
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return f? -x : x;
}

long long r, ans;
inline long long gcd(long long x, long long y) {
  return y ? gcd(y, x % y) : x;
}
inline bool check(long long u, long long v) {
  long long x = (sqrt(v));//判断是否是完全平方数
  if (v == x * x) return gcd(u, x) == 1;
  return 0;
}
inline long long calc(long long x) {
  long long s = 0;
  for (long long i = 1; i * i * 2 < x; ++i)//枚举u
    s += check(i, x - i * i);
  return s;
}
int main() {
  r = read();
  for (long long d = 1; d * d <= 2 * r; ++d)//枚举d
    if (2 * r % d == 0)
      ans += calc(2 * r / d) + (d * d == 2 * r? 0 : calc(d));
  printf("%lld\n", ans * 4 + 4);
  return 0;
}
posted @ 2019-10-09 09:41  Kylin_Seven  阅读(358)  评论(0编辑  收藏  举报