結果

問題 No.800 四平方定理
ユーザー tokoharu_procontokoharu_procon
提出日時 2019-03-17 21:41:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 708 ms / 2,000 ms
コード長 5,962 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 179,368 KB
実行使用メモリ 133,888 KB
最終ジャッジ日時 2023-09-22 04:37:26
合計ジャッジ時間 13,612 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,868 KB
testcase_01 AC 14 ms
6,124 KB
testcase_02 AC 10 ms
4,912 KB
testcase_03 AC 11 ms
5,316 KB
testcase_04 AC 10 ms
4,884 KB
testcase_05 AC 11 ms
4,972 KB
testcase_06 AC 16 ms
6,688 KB
testcase_07 AC 13 ms
6,168 KB
testcase_08 AC 17 ms
6,648 KB
testcase_09 AC 15 ms
6,352 KB
testcase_10 AC 344 ms
69,332 KB
testcase_11 AC 400 ms
102,276 KB
testcase_12 AC 393 ms
98,480 KB
testcase_13 AC 359 ms
70,352 KB
testcase_14 AC 401 ms
100,572 KB
testcase_15 AC 396 ms
100,836 KB
testcase_16 AC 402 ms
102,444 KB
testcase_17 AC 398 ms
101,752 KB
testcase_18 AC 401 ms
101,032 KB
testcase_19 AC 401 ms
102,740 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 407 ms
103,708 KB
testcase_23 AC 708 ms
133,420 KB
testcase_24 AC 703 ms
133,284 KB
testcase_25 AC 706 ms
133,388 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 699 ms
133,888 KB
testcase_29 AC 700 ms
133,224 KB
testcase_30 AC 700 ms
133,872 KB
testcase_31 AC 652 ms
128,372 KB
testcase_32 AC 708 ms
133,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;

// chmax, chmin
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
function<T(T, T)> op_max = [](T a, T b) -> T { return max(a, b); };
template <typename T>
function<T(T, T)> op_min = [](T a, T b) -> T { return min(a, b); };

template <typename T>
function<T(T, T)> op_sum = [](T a, T b) -> T { return a + b; };

namespace detail {
template <typename T, std::size_t NDIMS>
struct vector_builder {
  using type = std::vector<typename vector_builder<T, NDIMS - 1>::type>;

  static type make(std::vector<std::size_t> dims, const T& v = {}) {
    const auto vec = std::vector<T>(dims.empty() ? 0 : dims.back(), v);
    if (!dims.empty()) dims.pop_back();
    return vector_builder<std::vector<T>, NDIMS - 1>::make(dims, vec);
  }
};

template <typename T>
struct vector_builder<T, 1> {
  using type = std::vector<T>;

  static type make(std::vector<std::size_t> dims, const T& v = {}) {
    return type(dims.empty() ? 0 : dims.back(), v);
  }
};
}  // namespace detail

template <typename T, typename... SIZE_T>
auto exvector(const T& v, SIZE_T... dims) {
  static_assert(sizeof...(dims) != 0, "invalid dimension");
  std::vector<std::size_t> vec_dims{dims...};
  return detail::vector_builder<T, sizeof...(dims)>::make(vec_dims, v);
}

using namespace std;
//********************  dumps ************************//

template <typename T>
void dump(const T& data, vector<int>& iter) {
  cout << data << " ";
}
template <typename T>
void dump(const vector<T>& data, vector<int>& iter) {
  for (auto elem : data) dump(elem, iter);
  cout << endl;
}

template <typename T>
void dump(const vector<vector<T>>& data, vector<int>& iter) {
  for (auto elem : iter) {
    cout << "[" << elem << "]";
  }
  cout << endl;
  for (int i = 0; i < data.size(); i++) {
    iter.push_back(i);
    dump(data[i], iter);
    iter.pop_back();
  }
  cout << endl;
}

template <typename T>
void dump(const vector<T>& data, int dummy) {
  for (int i = 0; i < data.size(); i++) {
    cout << "[" << i << "] " << data[i] << endl;
  }
}
template <typename T>
void dump(const T& data) {  // T : data, U = base type val.
  vector<int> iter;
  dump(data, iter);
}
///////////////////////////////////////////////

long long mul(long long a, long long b, const long long MOD) {
  return b ? (mul(a * 2, b / 2, MOD) + (b & 1 ? a : 0)) % MOD : 0;
}

long long bpow(long long a, long long b, const long long MOD) {
  return (b ? bpow(a * a % MOD, b / 2, MOD) * (b & 1 ? a : 1) : 1) % MOD;
}

long long inv(long long a, const long long MOD) {
  return bpow(a, MOD - 2, MOD);
}

vector<long long> MODS = {1000000007};
template <int kind = 0>
class mint {
 public:
  long long v;

  mint() : v(0) {}
  mint(long long v) : v((v % MODS[kind] + MODS[kind]) % MODS[kind]) {}
  long long get_mod() { return MODS[kind]; }
  long long get_val() { return v; }
};

template <int kind>
ostream& operator<<(ostream& os, const mint<kind>& x) {
  return os << x.v;
}

template <int kind>
mint<kind>& operator+=(mint<kind>& a, mint<kind> b) {
  return a = a.v + b.v;
}
template <int kind>
mint<kind>& operator-=(mint<kind>& a, mint<kind> b) {
  return a = a.v - b.v;
}
template <int kind>
mint<kind>& operator*=(mint<kind>& a, mint<kind> b) {
  return a = a.v * b.v;
}
template <int kind>
mint<kind>& operator/=(mint<kind>& a, mint<kind> b) {
  return a = a.v * inv(b.v, a.get_mod());
}
template <int kind>
mint<kind> operator+(mint<kind> a, mint<kind> b) {
  return a += b;
}
template <int kind>
mint<kind> operator-(mint<kind> a, mint<kind> b) {
  return a -= b;
}
template <int kind>
mint<kind> operator*(mint<kind> a, mint<kind> b) {
  return a *= b;
}
template <int kind>
mint<kind> operator/(mint<kind> a, mint<kind> b) {
  return a /= b;
}
template <int kind>
mint<kind>& operator+=(mint<kind>& a, long long b) {
  return a = a.v + b;
}
template <int kind>
mint<kind>& operator-=(mint<kind>& a, long long b) {
  return a = a.v - b;
}
template <int kind>
mint<kind>& operator*=(mint<kind>& a, long long b) {
  return a = a.v * b;
}
template <int kind>
mint<kind>& operator/=(mint<kind>& a, long long b) {
  return a = a.v * inv(b, a.get_mod());
}
template <int kind>
mint<kind> operator+(mint<kind> a, long long b) {
  return a += b;
}
template <int kind>
mint<kind> operator-(mint<kind> a, long long b) {
  return a -= b;
}
template <int kind>
mint<kind> operator*(mint<kind> a, long long b) {
  return a *= b;
}
template <int kind>
mint<kind> operator/(mint<kind> a, long long b) {
  return a /= b;
}

vector<mint<>> kaijo;
void init() {
  kaijo.push_back(mint<>(1));
  for (int i = 1; i <= 400000; i++) {
    kaijo.push_back(kaijo.back() * i);
  }
}

mint<> comb(LL N, LL K) {
  if (K < 0) return mint<>(0);
  if (K > N) return mint<>(0);
  return kaijo[N] / kaijo[N - K] / kaijo[K];
}

int main() {
  LL ans = 0;
  LL N, D;
  cin >> N >> D;
  // map<LL, LL> A, B;
  vector<LL> A, B;
  for (LL i = 1; i <= N; i++) {
    for (LL j = 1; j <= N; j++) {
      // A[i * i + j * j]++;
      // B[i * i - j * j]++;
      A.push_back(i * i + j * j);
      B.push_back(i * i - j * j);
    }
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  vector<PII> X, Y;
  for (auto a : A) {
    if (X.empty() || X.back().first != a)
      X.push_back(PII(a, 1));
    else
      X[X.size() - 1].second += 1;
  }
  for (auto b : B) {
    if (Y.empty() || Y.back().first != b)
      Y.push_back(PII(b, 1));
    else
      Y[Y.size() - 1].second += 1;
  }

  Y.push_back(PII(100000000, 0));
  int iter = 0;
  for (auto x : X) {
    LL val = x.first;
    LL cnt = x.second;
    while (Y[iter].first < val - D) iter++;
    if (Y[iter].first == val - D) {
      ans += Y[iter].second * cnt;
    }
  }

  cout << ans << endl;

  return 0;
}
0