毕达哥斯三元组的模板题
练习练习
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; const int maxn = 1e6 +131; bool Jug[maxn]; int ans, sum; int gcd(int a,int b) { return b == 0 ? a : gcd(b,a%b); } void solve(int); int main() { int n; while(~scanf("%d",&n)) { solve(n); } return 0; } void solve(int n) { memset(Jug,false,sizeof(Jug)); ans = sum = 0; int tmp = int(sqrt(n +1.0)); for(int i = 1; i <= tmp; ++i) { for(int j = i+1; j <= tmp; ++j) { if(i*i + j*j > n) break; if(gcd(i,j) == 1 && (i % 2 != j % 2)) { int x = j * j - i * i; int y = 2 * i * j; int z = j*j + i*i; if(x*x + y*y == z*z) { ans ++; for(int i = 1; ; i ++) { if(i * z > n) break; Jug[i*x] = true; Jug[i*y] = true; Jug[i*z] = true; } } } } } for(int i = 1; i <= n; ++i) if(Jug[i] == false) sum++; printf("%d %d\n",ans,sum); }