题意:
给一个正整数N,x2 + y2 = z2
其中x,y和Z都限定为正整数且小于等于N。
你要计算出所有满足x < y < z的三元组(x,y,z)的数量,并且使得x,y和z两两互质,
即没有大于1的公因数。你还要计算出所有满足下面条件的p的数量:0 < p ≤ N,且p没有在所有这样的三元组中出现(并不限定为两两互质的三元组)
思路:
http://www.cnblogs.com/devymex/archive/2010/08/07/1799713.html
#include <cstdio>
#include <cstring>
#include <cstring>
#include <cmath>
bool hash[1000010];
int gcd(int x, int y)
{
int k = x % y;
while (k)
{
x = y;
y = k;
k = x % y;
}
return y;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int count = 0;
memset(hash, false, sizeof(hash));
for (int i = 1; i <= (int)sqrt(n*1.0); ++i)
for (int j = i + 1; j <= (int)sqrt(n*1.0); j += 2)
if (gcd(i, j) == 1) {
int a, b, c;
a = j * j - i * i;
b = 2 * i * j;
c = i * i + j * j;
if (c > n)
break;
for (int k = 1; k * c <= n; ++k)
hash[k*a] = true, hash[k*b] = true, hash[k*c] = true;
++count;
}
int ans = 0;
for (int i = 1; i <= n; ++i)
if (!hash[i])
++ans;
printf("%d %d\n", count, ans);
}
return 0;
}