#include<iostream>
using namespace std;
int AB_COUNT[5000 * 5000 + 1];
int MAX_N2 = 5000 * 5000;
void preprocessing() {
for (int i = 0; i <= 5000 * 5000; i++) {
AB_COUNT[i] = 0;
}
for (int a = 1; a <= 5000; a++) {
for (int b = 1; b <= a; b++) {
int sumsquare = a * a + b * b;
if (sumsquare > MAX_N2) {
break;
}
AB_COUNT[sumsquare]++;
}
}
}
int main() {
preprocessing();
int n;
cin >> n;
int count = 0;
for (int p = 1; p <= n; p++) {
for (int h = 1; h <= p; h++) {
int diff = p * p - h * h;
count += AB_COUNT[diff];
}
}
cout << count;
}
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 36 37 38 39 40 41 | #include<iostream> using namespace std; int AB_COUNT[5000 * 5000 + 1]; int MAX_N2 = 5000 * 5000; void preprocessing() { for (int i = 0; i <= 5000 * 5000; i++) { AB_COUNT[i] = 0; } for (int a = 1; a <= 5000; a++) { for (int b = 1; b <= a; b++) { int sumsquare = a * a + b * b; if (sumsquare > MAX_N2) { break; } AB_COUNT[sumsquare]++; } } } int main() { preprocessing(); int n; cin >> n; int count = 0; for (int p = 1; p <= n; p++) { for (int h = 1; h <= p; h++) { int diff = p * p - h * h; count += AB_COUNT[diff]; } } cout << count; } |
English