結果

問題 No.3051 Make All Divisible
ユーザー risujiroh
提出日時 2025-03-07 23:03:10
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 3,509 ms
コンパイル使用メモリ 295,152 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 02:25:43
合計ジャッジ時間 4,281 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

void Solve() {
  int n, k;
  IN(n, k);
  vector<int64_t> a(n);
  IN(a);

  vector<int> b(n);
  for (int i : Rep(0, n)) {
    b[i] = int(Mod(a[i], k));
  }

  int64_t sum = reduce(ALL(b));
  if (Mod(sum, k) != 0) {
    OUT(-1);
    return;
  }

  int64_t mx = ranges::max(b);
  if (mx * k <= sum) {
    OUT(sum / k);
    return;
  }

  priority_queue q(greater{}, vector<pair<int, int>>{});
  for (int i : Rep(0, n)) {
    if (b[i] < a[i]) {
      q.emplace(b[i], i);
    }
  }

  int lim = 10000 * n;
  while (Sz(q)) {
    if (lim == 0) {
      break;
    }
    --lim;
    auto [bi, i] = q.top();
    q.pop();

    b[i] += k;
    sum += k;
    SetMax(mx, b[i]);
    if (b[i] < a[i]) {
      q.emplace(b[i], i);
    }

    if (mx * k <= sum) {
      OUT(sum / k);
      return;
    }
  }

  OUT(-1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  IN(t);
  while (t--) {
    Solve();
  }
}

#elif __INCLUDE_LEVEL__ == 1

#include <bits/stdc++.h>

template <class T> concept MyRange = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;
template <class T> concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;

namespace std {

istream& operator>>(istream& is, MyRange auto&& r) {
  for (auto&& e : r) is >> e;
  return is;
}
istream& operator>>(istream& is, MyTuple auto&& t) {
  apply([&](auto&... xs) { (is >> ... >> xs); }, t);
  return is;
}

ostream& operator<<(ostream& os, MyRange auto&& r) {
  auto sep = "";
  for (auto&& e : r) os << exchange(sep, " ") << e;
  return os;
}
ostream& operator<<(ostream& os, MyTuple auto&& t) {
  auto sep = "";
  apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t);
  return os;
}

}  // namespace std

using namespace std;

#define LAMBDA2(x, y, ...) ([&](auto&& x, auto&& y) -> decltype(auto) { return __VA_ARGS__; })
#define LAMBDA3(x, y, z, ...) ([&](auto&& x, auto&& y, auto&& z) -> decltype(auto) { return __VA_ARGS__; })
#define ALL(r) begin(r), end(r)
#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Sz(r) int(size(r))
#define SetMax(...) LAMBDA2(x, y, x < y && (x = y, 1))(__VA_ARGS__)
#define DivFloor(...) LAMBDA2(x, y, x / y - ((x ^ y) < 0 && x % y != 0))(__VA_ARGS__)
#define Floor(...) LAMBDA3(x, m, r, DivFloor(x - r, m) * m + r)(__VA_ARGS__)
#define Mod(...) LAMBDA2(x, m, x - Floor(x, m, 0))(__VA_ARGS__)
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')

#endif  // __INCLUDE_LEVEL__ == 1
0