結果

問題 No.800 四平方定理
ユーザー HaraTakashiHaraTakashi
提出日時 2022-01-29 11:12:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,533 ms / 2,000 ms
コード長 2,890 bytes
コンパイル時間 2,548 ms
コンパイル使用メモリ 197,724 KB
最終ジャッジ日時 2025-01-27 17:46:50
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;
#define al(a) a.begin(), a.end()
#define ral(a) a.rbegin(), a.rend()
#define sz(a) (int)a.size()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define db(a, b) cout << #a << ": " << a << " " << #b << ": " << b << endl;

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;
#define al(a) a.begin(), a.end()
#define ral(a) a.rbegin(), a.rend()
#define sz(a) (int)a.size()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define db(a, b) cout << #a << ": " << a << " " << #b << ": " << b << endl;

int main() {
  ll n, d;
  cin >> n >> d;
  vector<int> ls;
  for (int w = 1; w <= n; ++w) {
    for (int z = 1; z <= n; ++z) {
      int t = w * w - z * z + d;
      if (t < 0) break;
      ls.push_back(t);
    }
  }
  sort(al(ls));
  ll ans = 0;
  for (int x = 1; x <= n; ++x) {
    for (int y = 1; y <= n; ++y) {
      int s = x * x + y * y;

      int now = upper_bound(al(ls), s) - lower_bound(al(ls), s);
      ans += now;
    }
  }

  cout << ans << endl;
  return 0;
}

// int main() {
//   ll n, d;
//   cin >> n >> d;
//   unordered_map<int, int> mp;
//   for (int x = 1; x <= n; ++x) {
//     for (int y = 1; y <= n; ++y) {
//       int s = x * x + y * y;
//       mp[s]++;
//     }
//   }

//   ll ans = 0;
//   for (int z = 1; z <= n; ++z) {
//     for (int w = 1; w <= n; ++w) {
//       int t = w * w - z * z + d;
//       if (t < 0) break;
//       ans += mp[t];
//     }
//   }
//   cout << ans << endl;
//   return 0;
// }
// int main() {
//   ll n, d;
//   cin >> n >> d;
//   const int MX = n * n + n * n;
//   vector<int> cnt1(MX + 1), cnt2(MX + 1);
//   for (int x = 1; x <= n; ++x) {
//     for (int y = 1; y <= n; ++y) {
//       int s = x * x + y * y;
//       cnt1[s]++;
//     }
//   }

//   ll ans = 0;
//   for (int w = 1; w <= n; ++w) {
//     for (int z = 1; z <= n; ++z) {
//       int t = w * w - z * z + d;
//       if (t < 0) break;
//       if (t > MX) continue;
//       cnt2[t]++;
//     }
//   }

//   rrep(i, MX) ans += ll(cnt1[i]) * cnt2[i];
//   cout << ans << endl;
//   return 0;
// }
// int main() {
//   ll n, d;
//   cin >> n >> d;
//   unordered_map<int, int> mp;
//   for (int x = 1; x <= n; ++x) {
//     for (int y = 1; y <= n; ++y) {
//       int s = x * x + y * y;
//       mp[s]++;
//     }
//   }

//   ll ans = 0;
//   for (int z = 1; z <= n; ++z) {
//     for (int w = 1; w <= n; ++w) {
//       int t = w * w - z * z + d;
//       if (t < 0) break;
//       ans += mp[t];
//     }
//   }
//   cout << ans << endl;
//   return 0;
// }
0