#include <bits/stdc++.h> using namespace std; const int M = 5e3; const int N = M * M + 5; int cnt[N]; // count (a, b) such that a^2 + b^2 = N void prepare() { for (int i = 1; i <= M; i++) { for (int j = i; j <= M; j++) { int x = i * i + j * j; if (x > N) break; cnt[x]++; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); prepare(); int n; cin >> n; int ans = 0; for (int d = 1; d <= n; d++) { int d2 = d * d; for (int h = 1; h <= n; h++) { int h2 = h * h; int x = d2 - h2; if (x > 0 && x < N) { ans += cnt[x]; } } } cout << ans << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <bits/stdc++.h> using namespace std; const int M = 5e3; const int N = M * M + 5; int cnt[N]; // count (a, b) such that a^2 + b^2 = N void prepare() { for (int i = 1; i <= M; i++) { for (int j = i; j <= M; j++) { int x = i * i + j * j; if (x > N) break; cnt[x]++; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); prepare(); int n; cin >> n; int ans = 0; for (int d = 1; d <= n; d++) { int d2 = d * d; for (int h = 1; h <= n; h++) { int h2 = h * h; int x = d2 - h2; if (x > 0 && x < N) { ans += cnt[x]; } } } cout << ans << '\n'; } |