結果

問題 No.937 Ultra Sword
ユーザー emthrm
提出日時 2020-02-25 02:01:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 485 ms / 3,000 ms
コード長 2,458 bytes
コンパイル時間 2,979 ms
コンパイル使用メモリ 209,612 KB
最終ジャッジ日時 2025-01-09 02:11:42
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
template <typename T> using posteriority_queue = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
template <typename T> void unique(vector<T> &a) { a.erase(unique(ALL(a)), a.end()); }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
vector<T> divisor(T val) {
  vector<T> res;
  for (T i = 1; i * i <= val; ++i) {
    if (val % i == 0) {
      res.emplace_back(i);
      if (i * i != val) res.emplace_back(val / i);
    }
  }
  sort(ALL(res));
  return res;
}

int main() {
  const int N = 17;
  int n; cin >> n;
  ll sum = 0;
  map<int, ll> mp;
  bitset<N> a;
  {
    int ai; cin >> ai;
    sum += ai;
    a = ai;
    vector<int> d = divisor(ai);
    for (int e : d) mp[e] += ai - ai / e;
  }
  int msb = N - 1;
  for (; msb >= 0; --msb) {
    if (a.test(msb)) break;
  }
  while (--n) {
    int ai; cin >> ai;
    sum += ai;
    vector<int> d = divisor(ai);
    for (int e : d) mp[e] += ai - ai / e;
    bitset<N> bit = ai;
    int ai_msb = N - 1;
    for (; ai_msb >= 0; --ai_msb) {
      if (bit.test(ai_msb)) break;
    }
    if (ai_msb > msb) {
      swap(a, bit);
      swap(msb, ai_msb);
    }
    while (bit.any()) {
      a = a ^ (bit << (msb - ai_msb));
      for (; msb >= 0; --msb) {
        if (a.test(msb)) break;
      }
      if (msb < ai_msb) {
        swap(a, bit);
        swap(msb, ai_msb);
      }
    }
  }
  set<int> mul;
  FOR(i, 1, 1 << (N - msb)) {
    bitset<N> bit;
    REP(j, N - msb) {
      if (i >> j & 1) bit ^= a << j;
    }
    mul.emplace(int(bit.to_ulong()));
  }
  ll ans = sum;
  for (auto pr : mp) {
    if (mul.count(pr.first) == 1) chmin(ans, sum - pr.second);
  }
  cout << ans << '\n';
  return 0;
}
0