結果

問題 No.937 Ultra Sword
ユーザー noshi91noshi91
提出日時 2019-11-29 22:32:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 3,000 ms
コード長 6,613 bytes
コンパイル時間 826 ms
コンパイル使用メモリ 79,492 KB
実行使用メモリ 5,464 KB
最終ジャッジ日時 2023-08-11 14:55:25
合計ジャッジ時間 3,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 10 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 32 ms
5,404 KB
testcase_06 AC 43 ms
5,268 KB
testcase_07 AC 46 ms
5,380 KB
testcase_08 AC 28 ms
4,824 KB
testcase_09 AC 25 ms
5,040 KB
testcase_10 AC 50 ms
5,212 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 31 ms
4,376 KB
testcase_13 AC 29 ms
4,376 KB
testcase_14 AC 34 ms
4,380 KB
testcase_15 AC 25 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 28 ms
4,380 KB
testcase_19 AC 17 ms
4,380 KB
testcase_20 AC 14 ms
4,380 KB
testcase_21 AC 47 ms
5,464 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 37 ms
5,324 KB
testcase_25 AC 33 ms
5,192 KB
testcase_26 AC 29 ms
5,240 KB
testcase_27 AC 28 ms
5,244 KB
testcase_28 AC 33 ms
5,300 KB
testcase_29 AC 7 ms
4,736 KB
testcase_30 AC 26 ms
5,092 KB
testcase_31 AC 36 ms
5,384 KB
testcase_32 AC 23 ms
5,056 KB
testcase_33 AC 36 ms
5,388 KB
testcase_34 AC 11 ms
4,652 KB
testcase_35 AC 8 ms
4,724 KB
testcase_36 AC 32 ms
5,268 KB
testcase_37 AC 9 ms
4,728 KB
testcase_38 AC 27 ms
5,192 KB
testcase_39 AC 7 ms
4,696 KB
testcase_40 AC 30 ms
5,088 KB
testcase_41 AC 14 ms
4,760 KB
testcase_42 AC 24 ms
5,148 KB
testcase_43 AC 23 ms
5,240 KB
testcase_44 AC 14 ms
4,668 KB
testcase_45 AC 16 ms
4,928 KB
testcase_46 AC 19 ms
4,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>
#include <limits>
namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  if (a < b) {
    return b - a;
  } else {
    return a - b;
  }
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a) {
    a = b;
  }
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b) {
    a = b;
  }
}
template <class F> class fix_point : private F {
public:
  explicit constexpr fix_point(F &&f) : F(std::forward<F>(f)) {}

  template <class... Args>
  constexpr decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) make_fix(F &&f) {
  return fix_point<F>(std::forward<F>(f));
}
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}

} // namespace n91

#include <cassert>
#include <cstddef>
#include <numeric>
#include <utility>
#include <vector>

namespace n91 {

class prime_sieve {
public:
  using size_type = std::size_t;

private:
  std::vector<size_type> prime, factor;

public:
  prime_sieve() {}
  explicit prime_sieve(const size_type size) : prime(), factor(size) {
    if (size == 0) {
      return;
    }
    std::iota(factor.begin(), factor.end(), static_cast<size_type>(0));
    factor[0] = 1;
    if (size == 1) {
      return;
    }
    factor[1] = 0;
    prime.reserve(size);
    for (size_type i{2}; i != size; ++i) {
      const size_type fi{factor[i]};
      if (fi == i) {
        prime.push_back(i);
      }
      for (const size_type p : prime) {
        if (p > fi) {
          break;
        }
        const size_type ip{i * p};
        if (ip >= size) {
          break;
        }
        factor[ip] = p;
      }
    }
    prime.shrink_to_fit();
  }
  size_type len() const noexcept { return factor.size(); }
  bool is_prime(const size_type i) const noexcept {
    assert(i < len());
    return factor[i] == i;
  }
  std::vector<size_type> factorize(size_type i) const noexcept {
    assert(i != 0);
    std::vector<size_type> ret;
    while (i != 1) {
      ret.push_back(factor[i]);
      i /= factor[i];
    }
    return std::move(ret);
  }
  template <class C> void divisor_zeta(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{1}; j * prime[i] < n; ++j) {
        c[j * prime[i]] += c[j];
      }
    }
  }
  template <class C> void divisor_mobius(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{(n - 1) / prime[i]}; j != 0; --j) {
        c[j * prime[i]] -= c[j];
      }
    }
  }
  template <class C> void multiple_zeta(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{(n - 1) / prime[i]}; j != 0; --j) {
        c[j] += c[j * prime[i]];
      }
    }
  }
  template <class C> void multiple_mobius(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{1}; j * prime[i] < n; ++j) {
        c[j] -= c[j * prime[i]];
      }
    }
  }
};

} // namespace n91

#include <algorithm>
#include <iostream>
#include <utility>

namespace n91 {

void main_() {
  /*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/
  const usize n = scan<usize>();
  std::vector<usize> a(n);
  for (auto &e : a) {
    std::cin >> e;
  }
  const usize am = *std::max_element(a.cbegin(), a.cend()) + 1;

  const auto get = [](const usize a, const usize i) -> bool {
    return a >> i & 1;
  };

  const auto gcd = [get](usize a, usize b) {
    for (const usize i : revrep(0, 17)) {
      if (a < b) {
        std::swap(a, b);
      }
      if (get(b, i)) {
        b ^= a;
      }
      if (b == 0) {
        return a;
      }
      if (get(a, i)) {
        usize t = b;
        while (!get(t, i)) {
          t *= 2;
        }
        a ^= t;
      }
    }
    assert(false);
  };

  usize g = 0;
  for (const auto e : a) {
    g = gcd(g, e);
  }

  const auto ok = [g, get](usize a) {
    for (const usize i : revrep(0, 17)) {
      if (get(a, i)) {
        usize t = g;
        while (!get(t, i)) {
          t *= 2;
        }
        a ^= t;
      }
      if (get(g, i)) {
        return a == 0;
      }
    }
    assert(false);
  };

  const prime_sieve ps(am);
  std::vector<u64> rev(am);
  for (const auto e : a) {
    rev[e] += e;
  }
  ps.multiple_zeta(rev);
  const u64 sum = std::accumulate(a.cbegin(), a.cend(), static_cast<u64>(0));
  u64 ans = std::numeric_limits<u64>::max();
  for (const usize i : rep(1, am)) {
    if (ok(i)) {
      chmin(ans, sum - rev[i] + rev[i] / i);
    }
  }
  std::cout << ans << std::endl;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}
0