【數論】[圓點座標]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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章