#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; // a*a + b*b == r*r - h*h = (r-h)*(r+h) vector<int> ab(n*n+1), rh(n*n+1); for (int x = 1; x <= n; x++) { for (int y = 1; y <= x; y++) { int s = x*x + y*y, d = x*x - y*y; if (s <= n*n) ab[s]++; if (d <= n*n) rh[d]++; } } int res = 0; for (int z = 0; z <= n*n; z++) { res += ab[z] * rh[z]; } cout << res << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; // a*a + b*b == r*r - h*h = (r-h)*(r+h) vector<int> ab(n*n+1), rh(n*n+1); for (int x = 1; x <= n; x++) { for (int y = 1; y <= x; y++) { int s = x*x + y*y, d = x*x - y*y; if (s <= n*n) ab[s]++; if (d <= n*n) rh[d]++; } } int res = 0; for (int z = 0; z <= n*n; z++) { res += ab[z] * rh[z]; } cout << res << '\n'; } |