結果

問題 No.1901 bitwise xor convolution (characteristic 2)
ユーザー suisensuisen
提出日時 2022-04-23 15:40:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,686 ms / 4,000 ms
コード長 2,458 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 82,888 KB
実行使用メモリ 196,840 KB
最終ジャッジ日時 2024-04-27 04:06:28
合計ジャッジ時間 9,777 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1,686 ms
196,776 KB
testcase_08 AC 1,629 ms
196,840 KB
testcase_09 AC 1,576 ms
196,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <array>
#include <cassert>
#include <iostream>
#include <vector>

using Poly = std::array<int64_t, 63>;

Poly operator+(const Poly& x, const Poly& y) {
    Poly z;
    for (std::size_t i = 0; i < 63; ++i) {
        z[i] = x[i] + y[i];
    }
    return z;
}
Poly operator-(const Poly& x, const Poly& y) {
    Poly z;
    for (std::size_t i = 0; i < 63; ++i) {
        z[i] = x[i] - y[i];
    }
    return z;
}
Poly operator*(const Poly& x, const Poly& y) {
    std::array<int64_t, 63> dat{};
    for (std::size_t i = 0; i < 32; ++i) for (std::size_t j = 0; j < 32; ++j) {
        dat[i + j] += int64_t(x[i]) * int64_t(y[j]);
    }
    return dat;
}

void unit_walsh_hadamard_transform(Poly& x0, Poly& x1) {
    Poly y0 = x0, y1 = x1;
    x0 = y0 + y1;   // 1,  1
    x1 = y0 - y1;   // 1, -1
}

template <typename UnitTransform, typename ReferenceGetter, std::size_t... Seq>
void unit_transform(UnitTransform transform, ReferenceGetter ref_getter, std::index_sequence<Seq...>) {
    transform(ref_getter(Seq)...);
}

void walsh_hadamard(std::vector<Poly>& x) {
    const std::size_t n = x.size();
    for (std::size_t block = 1; block < n; block *= 2) {
        for (std::size_t l = 0; l < n; l += 2 * block) {
            for (std::size_t offset = l; offset < l + block; ++offset) {
                const auto ref_getter = [&](std::size_t i) -> Poly& { return x[offset + i * block]; };
                unit_transform(unit_walsh_hadamard_transform, ref_getter, std::make_index_sequence<2>());
            }
        }
    }
}

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

    std::size_t n;
    std::cin >> n;

    std::vector<Poly> a(1 << n, Poly{}), b(1 << n, Poly{}), c(1 << n, Poly{});

    for (std::size_t i = 0; i < 1U << n; ++i) {
        for (std::size_t j = 0; j < 32; ++j) {
            std::cin >> a[i][j];
        }
    }

    for (std::size_t i = 0; i < 1U << n; ++i) {
        for (std::size_t j = 0; j < 32; ++j) {
            std::cin >> b[i][j];
        }
    }

    walsh_hadamard(a);
    walsh_hadamard(b);
    for (std::size_t i = 0; i < 1U << n; ++i) c[i] = a[i] * b[i];
    walsh_hadamard(c);

    for (std::size_t i = 0; i < 1U << n; ++i) {
        for (std::size_t j = 0; j < 63; ++j) {
            std::cout << ((c[i][j] >> n) & 1) << ' ';
        }
        std::cout << '\n';
    }

    return 0;
}
0