题意:给你一个n,让你找出一些勾股数组,a^2+b^2=c^2 , 需要满足a
思路:本题就是要求在n范围内的素勾股数,在维基百科内找到相关的资料
如果 (a, b, c) 是勾股数,它们的正整数倍数,也是勾股数,即 (na, nb, nc) 也是勾股数。若果 a, b, c 三者互质(它们的最大公因数是 1),它们就称为素勾股数。
以下的方法可用来找出勾股数。设 m > n 、 m 和 n 均是正整数,
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
#include
#include
#include
#include
const int N = 1000005;
long long n;
int vis[N];
long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
int main() {
while (scanf("%lld", &n) != EOF) {
int ans1 = 0, ans2 = 0;
memset(vis, 0, sizeof(vis));
long long m = sqrt(n + 0.5);
long long a, b, c;
for (long long i = 1; i <= m; i++) {
for (long long j = i + 1; j <= m; j += 2) {
if (gcd(j, i) == 1) {
a = j * j - i * i;
b = 2 * i * j;
c = i * i + j * j;
if (c <= n) {
ans1++;
vis[a] = vis[b] = vis[c] = 1;
}
for (int k = 2; c * k <= n; k++)
vis [k * a] = vis[k * b] = vis[k * c] = 1;
}
}
}
for (int i = 1; i <= n; i++)
if (vis[i])
ans2++;
printf("%d %d\n", ans1++, n - ans2++);
}
return 0;
}