結果

問題 No.937 Ultra Sword
ユーザー kimiyukikimiyuki
提出日時 2019-12-01 16:01:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 3,000 ms
コード長 2,210 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 206,940 KB
実行使用メモリ 5,208 KB
最終ジャッジ日時 2023-08-13 15:01:23
合計ジャッジ時間 8,098 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 7 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 85 ms
5,128 KB
testcase_06 AC 105 ms
4,852 KB
testcase_07 AC 116 ms
4,928 KB
testcase_08 AC 56 ms
4,644 KB
testcase_09 AC 43 ms
4,608 KB
testcase_10 AC 127 ms
5,096 KB
testcase_11 AC 26 ms
4,376 KB
testcase_12 AC 122 ms
4,480 KB
testcase_13 AC 117 ms
4,380 KB
testcase_14 AC 135 ms
4,624 KB
testcase_15 AC 100 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 111 ms
4,380 KB
testcase_19 AC 65 ms
4,384 KB
testcase_20 AC 56 ms
4,380 KB
testcase_21 AC 136 ms
5,208 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 111 ms
4,880 KB
testcase_25 AC 91 ms
4,864 KB
testcase_26 AC 77 ms
4,848 KB
testcase_27 AC 76 ms
4,868 KB
testcase_28 AC 88 ms
4,932 KB
testcase_29 AC 9 ms
4,380 KB
testcase_30 AC 65 ms
4,928 KB
testcase_31 AC 99 ms
5,196 KB
testcase_32 AC 60 ms
4,952 KB
testcase_33 AC 99 ms
5,192 KB
testcase_34 AC 23 ms
4,376 KB
testcase_35 AC 13 ms
4,496 KB
testcase_36 AC 87 ms
4,996 KB
testcase_37 AC 14 ms
4,376 KB
testcase_38 AC 69 ms
4,884 KB
testcase_39 AC 10 ms
4,416 KB
testcase_40 AC 79 ms
4,896 KB
testcase_41 AC 34 ms
4,732 KB
testcase_42 AC 63 ms
5,024 KB
testcase_43 AC 58 ms
4,632 KB
testcase_44 AC 27 ms
4,376 KB
testcase_45 AC 40 ms
4,764 KB
testcase_46 AC 47 ms
4,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }

/**
 * @note O(\sqrt{n})
 */
struct prepared_primes {
    int size;
    std::vector<int> sieve;
    std::vector<int> primes;

    prepared_primes(int size_)
        : size(size_) {

        sieve.resize(size);
        REP3 (p, 2, size) if (sieve[p] == 0) {
            primes.push_back(p);
            for (int k = p; k < size; k += p) {
                if (sieve[k] == 0) {
                    sieve[k] = p;
                }
            }
        }
    }
};

int64_t solve(int n, const vector<int64_t> & a) {
    // Gaussian elimination
    vector<int64_t> basis;
    for (int64_t a_i : a) {
        for (int k = 0; (a_i << k) < (1ull << 40); ++ k) {
            int64_t x = a_i << k;
            for (int64_t b : basis) {
                chmin(x, x ^ b);
            }
            if (x) {
                basis.push_back(x);
            }
        }
    }

    // list constructibles
    int64_t max_a = *max_element(ALL(a));
    vector<bool> constructed(max_a + 1);
    constructed[0] = true;
    for (int64_t b : basis) {
        REP (x, max_a) if (constructed[x]) {
            if ((b ^ x) < constructed.size()) {
                constructed[b ^ x] = true;
            }
        }
    }

    // fast zeta transform
    prepared_primes primes(1e5 + 3);
    vector<int64_t> acc(max_a + 1);
    for (int64_t a_i : a) {
        acc[a_i] += a_i;
    }
    for (int64_t p : primes.primes) {
        REP_R (x, max_a / p + 1) {
            acc[x] += acc[x * p];
        }
    }

    // answer
    int64_t sum = accumulate(ALL(a), 0ll);
    int64_t answer = INT64_MAX;
    REP3 (x, 1, max_a + 1) if (constructed[x]) {
        chmin(answer, sum - acc[x] + acc[x] / x);
    }
    return answer;
}

int main() {
    int n; cin >> n;
    vector<int64_t> a(n);
    REP (i, n) {
        cin >> a[i];
    }
    cout << solve(n, a) << endl;
    return 0;
}
0