結果
| 問題 |
No.1289 RNG and OR
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2020-11-14 00:46:11 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 74 ms / 2,000 ms |
| コード長 | 2,921 bytes |
| コンパイル時間 | 1,146 ms |
| コンパイル使用メモリ | 74,904 KB |
| 最終ジャッジ日時 | 2025-01-16 00:16:18 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#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(); i++) {
if (dp[i].val > 0) {
dp[i] /= trans[i];
}
}
walsh_hadamard(dp, moebius);
cout << accumulate(dp.begin(), dp.end() - 1, mint(0)).val << '\n';
}
hitonanode