#include <iostream> #include <unordered_map> using namespace std; static int n, n2, iCnt = 0; static unordered_map<int, int> mapSum2Cnt; // key: sum of 2 squares; value: number of occurrences static void InitSumsOf2Squares() { for (int b = 2; b < n; ++b) { int b2 = b * b; for (int a = 1; a <= b; ++a) { int iSum2 = a * a + b2; if (iSum2 >= n2) break; mapSum2Cnt[iSum2]++; } } } static void CountAquariums() { for (int d = 3; d <= n; ++d) { int d2 = d * d; for (int h = 1; h < d; ++h) { int iSumAB2 = d2 - (h * h); if (mapSum2Cnt.count(iSumAB2) == 0) continue; iCnt += mapSum2Cnt[iSumAB2]; } } } int main() { cin >> n; n2 = n * n; InitSumsOf2Squares(); CountAquariums(); cout << iCnt; }
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> #include <unordered_map> using namespace std; static int n, n2, iCnt = 0; static unordered_map<int, int> mapSum2Cnt; // key: sum of 2 squares; value: number of occurrences static void InitSumsOf2Squares() { for (int b = 2; b < n; ++b) { int b2 = b * b; for (int a = 1; a <= b; ++a) { int iSum2 = a * a + b2; if (iSum2 >= n2) break; mapSum2Cnt[iSum2]++; } } } static void CountAquariums() { for (int d = 3; d <= n; ++d) { int d2 = d * d; for (int h = 1; h < d; ++h) { int iSumAB2 = d2 - (h * h); if (mapSum2Cnt.count(iSumAB2) == 0) continue; iCnt += mapSum2Cnt[iSumAB2]; } } } int main() { cin >> n; n2 = n * n; InitSumsOf2Squares(); CountAquariums(); cout << iCnt; } |