結果

問題 No.1289 RNG and OR
ユーザー 👑 hitonanodehitonanode
提出日時 2020-11-14 02:30:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,882 bytes
コンパイル時間 716 ms
コンパイル使用メモリ 76,948 KB
実行使用メモリ 5,252 KB
最終ジャッジ日時 2023-09-30 04:41:45
合計ジャッジ時間 2,167 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 10 ms
4,380 KB
testcase_18 AC 19 ms
4,380 KB
testcase_19 AC 36 ms
4,380 KB
testcase_20 AC 71 ms
5,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

template <int mod>
struct ModInt
{
    using lint = long long;
    int val;
    constexpr ModInt() : val(0) {}
    constexpr ModInt &_setval(lint v) { val = (v >= mod ? v - mod : v); return *this; }
    constexpr ModInt(lint v) { _setval(v % mod + mod); }
    explicit operator bool() const { return val != 0; }
    constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val + x.val); }
    constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val - x.val + mod); }
    constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val * x.val % mod); }
    constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val * x.inv() % mod); }
    constexpr ModInt operator-() const { return ModInt()._setval(mod - val); }
    constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; is >> t; x = ModInt(t); return is; }
    constexpr lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % mod;
            tmp = tmp * tmp % mod;
            n /= 2;
        }
        return ans;
    }
    constexpr lint inv() const { return this->power(mod - 2); }
};
using mint = ModInt<998244353>;

// Fast Walsh-Hadamard transform
// Tutorials: <https://codeforces.com/blog/entry/71899>
//            <https://csacademy.com/blog/fast-fourier-transform-and-variations-of-it>
template <typename T, typename F>
void walsh_hadamard(std::vector<T>& seq, F f)
{
    const int n = seq.size();
    for (int w = 1; w < n; w *= 2) {
        for (int i = 0; i < n; i += w * 2) {
            for (int j = 0; j < w; j++) {
                f(seq[i + j], seq[i + j + w]);
            }
        }
    }
}
auto zeta = [](mint& lo, mint& hi) { hi += lo; };
auto moebius = [](mint& lo, mint& hi) { hi -= lo; };


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

    int N;
    cin >> N;
    vector<mint> trans(1 << N);
    for (auto &x : trans) {
        cin >> x;
    }
    mint Sinv = accumulate(trans.begin(), trans.end(), mint(0)).inv();
    for (auto &x : trans) {
        x = -x * Sinv;
    }
    trans[0] += 1;

    walsh_hadamard(trans, zeta);

    vector<mint> dp(1 << N);
    dp[0] = 1;
    walsh_hadamard(dp, zeta);

    for (size_t i = 0; i < dp.size() - 1; i++) {
        dp[i] /= trans[i];
    }
    walsh_hadamard(dp, moebius);
    cout << accumulate(dp.begin(), dp.end() - 1, mint(0)).val << '\n';
}
0