結果

問題 No.1574 Swap and Repaint
コンテスト
ユーザー Shlok Goyal
提出日時 2026-11-25 21:24:42
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 574 ms / 10,000 ms
+ 201µs
コード長 10,227 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,860 ms
コンパイル使用メモリ 372,528 KB
実行使用メモリ 21,772 KB
最終ジャッジ日時 2026-11-25 21:25:00
合計ジャッジ時間 16,026 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

namespace polynomial {
constexpr int MOD = 998244353;
using Poly = vector<int>;
using i64 = long long;

int power(int a, int exponent) {
    int result = 1;
    for (; exponent; exponent >>= 1, a = i64(a) * a % MOD)
        if (exponent & 1) result = i64(result) * a % MOD;
    return result;
}

void ntt(Poly& a, bool inverse = false) {
    int n = int(a.size());
    if (n == 1) return;
    static Poly roots{0, 1};
    for (int k = int(roots.size()); k < n; k <<= 1) {
        int step = power(3, (MOD - 1) / (2 * k));
        roots.resize(2 * k);
        for (int i = k / 2; i < k; ++i) {
            roots[2 * i] = roots[i];
            roots[2 * i + 1] = i64(roots[i]) * step % MOD;
        }
    }
    if (inverse) reverse(a.begin() + 1, a.end());
    for (int i = 1, j = 0; i < n; ++i) {
        int bit = n >> 1;
        for (; j & bit; bit >>= 1) j ^= bit;
        j ^= bit;
        if (i < j) swap(a[i], a[j]);
    }
    for (int half = 1; half < n; half <<= 1) {
        for (int first = 0; first < n; first += half * 2) {
            for (int j = 0; j < half; ++j) {
                int x = a[first + j];
                int y = i64(a[first + half + j]) * roots[half + j] % MOD;
                int sum = x + y, difference = x - y;
                if (sum >= MOD) sum -= MOD;
                if (difference < 0) difference += MOD;
                a[first + j] = sum;
                a[first + half + j] = difference;
            }
        }
    }
    if (inverse) {
        int invN = MOD - (MOD - 1) / n;
        for (int& x : a) x = i64(x) * invN % MOD;
    }
}

Poly multiply(Poly a, Poly b) {
    if (a.empty() || b.empty()) return {};
    int required = int(a.size() + b.size() - 1);
    if (min(a.size(), b.size()) <= 24) {
        Poly result(required);
        for (int i = 0; i < int(a.size()); ++i) if (a[i])
            for (int j = 0; j < int(b.size()); ++j) if (b[j])
                result[i + j] = (result[i + j] + i64(a[i]) * b[j]) % MOD;
        return result;
    }
    int n = 1;
    while (n < required) n <<= 1;
    a.resize(n); b.resize(n);
    ntt(a); ntt(b);
    for (int i = 0; i < n; ++i) a[i] = i64(a[i]) * b[i] % MOD;
    ntt(a, true);
    a.resize(required);
    return a;
}

Poly inverse(const Poly& a, int required) {
    Poly result{power(a[0], MOD - 2)};
    while (int(result.size()) < required) {
        int length = min(required, int(result.size()) * 2);
        Poly prefix(a.begin(), a.begin() + min(length, int(a.size())));
        Poly correction = multiply(prefix, result);
        correction.resize(length);
        for (int& x : correction) if (x) x = MOD - x;
        correction[0] += 2;
        if (correction[0] >= MOD) correction[0] -= MOD;
        result = multiply(result, correction);
        result.resize(length);
    }
    return result;
}

struct Arithmetic {
    Poly fact, invFact;
    explicit Arithmetic(int n) : fact(n + 1, 1), invFact(n + 1, 1) {
        for (int i = 1; i <= n; ++i) fact[i] = i64(fact[i - 1]) * i % MOD;
        invFact[n] = power(fact[n], MOD - 2);
        for (int i = n; i > 0; --i) invFact[i - 1] = i64(invFact[i]) * i % MOD;
    }

    // U_d denotes U_d(x/2): U_0=1, U_1=x, U_d=x*U_(d-1)-U_(d-2).
    Poly chebyshev(int d) const {
        if (d < 0) return {0};
        Poly result(d + 1);
        for (int j = 0; j * 2 <= d; ++j) {
            int value = i64(fact[d - j]) * invFact[j] % MOD * invFact[d - 2 * j] % MOD;
            result[d - 2 * j] = (j & 1) && value ? MOD - value : value;
        }
        return result;
    }
};

// Convert sum c_j U_j(x/2) to ordinary polynomial coefficients.
// The second returned component is sum c_j U_(j-1)(x/2).
class BasisConverter {
    static constexpr int BASE = 32;
    struct Pair { Poly p, q; };
    struct Kernel { Poly u, v, w; };
    vector<Kernel> kernels;
    vector<Poly> small;
    int padded;

    Pair solve(const Poly& c, int first, int size) const {
        if (size <= BASE) {
            Pair result{Poly(size), Poly(size)};
            for (int j = 0; j < size; ++j) if (c[first + j]) {
                int coefficient = c[first + j];
                for (int d = 0; d <= j; ++d)
                    result.p[d] = (result.p[d] + i64(coefficient) * small[j][d]) % MOD;
                if (j)
                    for (int d = 0; d < j; ++d)
                        result.q[d] = (result.q[d] + i64(coefficient) * small[j - 1][d]) % MOD;
            }
            return result;
        }
        int half = size / 2;
        Pair left = solve(c, first, half);
        Pair right = solve(c, first + half, half);
        right.p.resize(size); right.q.resize(size);
        ntt(right.p); ntt(right.q);
        const Kernel& kernel = kernels[__builtin_ctz(unsigned(size))];
        Pair result{Poly(size), Poly(size)};
        for (int i = 0; i < size; ++i) {
            int p = (i64(kernel.u[i]) * right.p[i] - i64(kernel.v[i]) * right.q[i]) % MOD;
            int q = (i64(kernel.v[i]) * right.p[i] - i64(kernel.w[i]) * right.q[i]) % MOD;
            if (p < 0) p += MOD;
            if (q < 0) q += MOD;
            result.p[i] = p;
            result.q[i] = q;
        }
        ntt(result.p, true); ntt(result.q, true);
        for (int i = 0; i < half; ++i) {
            result.p[i] += left.p[i];
            result.q[i] += left.q[i];
            if (result.p[i] >= MOD) result.p[i] -= MOD;
            if (result.q[i] >= MOD) result.q[i] -= MOD;
        }
        return result;
    }

public:
    BasisConverter(int n, const Arithmetic& arithmetic) : padded(1) {
        while (padded < n) padded <<= 1;
        kernels.resize(__builtin_ctz(unsigned(padded)) + 1);
        for (int j = 0; j < BASE; ++j) small.push_back(arithmetic.chebyshev(j));
        for (int size = 2 * BASE; size <= padded; size <<= 1) {
            int half = size / 2;
            Kernel& kernel = kernels[__builtin_ctz(unsigned(size))];
            kernel.u = arithmetic.chebyshev(half);
            kernel.v = arithmetic.chebyshev(half - 1);
            kernel.w = arithmetic.chebyshev(half - 2);
            for (Poly* p : {&kernel.u, &kernel.v, &kernel.w}) {
                p->resize(size);
                ntt(*p);
            }
        }
    }

    Poly convert(Poly coefficients) const {
        int length = int(coefficients.size());
        coefficients.resize(padded);
        Poly result = solve(coefficients, 0, padded).p;
        result.resize(length);
        return result;
    }
};

Poly remainder(Poly value, const Poly& divisor, const Poly& invReversedDivisor) {
    int degree = int(divisor.size()) - 1;
    if (int(value.size()) <= degree) {
        value.resize(degree);
        return value;
    }
    int length = int(value.size()) - degree;
    Poly top(value.rbegin(), value.rbegin() + length);
    Poly inversePrefix(invReversedDivisor.begin(), invReversedDivisor.begin() + length);
    Poly quotient = multiply(top, inversePrefix);
    quotient.resize(length);
    reverse(quotient.begin(), quotient.end());
    Poly product = multiply(quotient, divisor);
    value.resize(degree);
    for (int i = 0; i < degree; ++i) {
        value[i] -= product[i];
        if (value[i] < 0) value[i] += MOD;
    }
    return value;
}
} // namespace polynomial

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    using namespace polynomial;

    int n;
    cin >> n;
    Poly a(n);
    for (int& x : a) cin >> x;
    Arithmetic arithmetic(max(n, 32));
    int schedules = i64(power(4, n - 1)) * arithmetic.fact[n - 1] % MOD;

    if (all_of(a.begin(), a.end(), [&](int x) { return x == a[0]; })) {
        int answer = i64(schedules) * n % MOD * a[0] % MOD;
        for (int s = 0; s <= n; ++s) {
            cout << answer << '\n';
            answer = i64(answer) * (n - 1) % MOD;
        }
        return 0;
    }

    Poly weight(n);
    int inverseTwoPower = 1, sum = 1;
    for (int j = 1; j < n; ++j) {
        inverseTwoPower = i64(inverseTwoPower) * ((MOD + 1) / 2) % MOD;
        int b = i64(inverseTwoPower) * arithmetic.invFact[j] % MOD;
        weight[j - 1] = 1 - b;
        if (weight[j - 1] < 0) weight[j - 1] += MOD;
        sum += b;
        if (sum >= MOD) sum -= MOD;
    }
    weight[n - 1] = sum;

    // e_(j+1) = (U_j(J/2)-U_(j-1)(J/2)) e_1.
    auto difference = [&](const Poly& values) {
        Poly result = values;
        for (int i = 0; i + 1 < n; ++i) {
            result[i] -= values[i + 1];
            if (result[i] < 0) result[i] += MOD;
        }
        return result;
    };
    BasisConverter converter(n, arithmetic);
    Poly polynomialA = converter.convert(difference(a));
    Poly polynomialW = converter.convert(difference(weight));

    Poly u = arithmetic.chebyshev(n - 1);
    Poly previous = arithmetic.chebyshev(n - 2);
    Poly characteristic(n + 1), boundary = u;
    for (int i = 0; i < n; ++i) {
        characteristic[i + 1] = u[i];
        int twice = 2LL * u[i] % MOD;
        characteristic[i] -= twice;
        if (characteristic[i] < 0) characteristic[i] += MOD;
    }
    for (int i = 0; i + 1 < n; ++i) {
        boundary[i] -= previous[i];
        if (boundary[i] < 0) boundary[i] += MOD;
    }
    Poly reversedCharacteristic(characteristic.rbegin(), characteristic.rend());
    Poly inverseDenominator = inverse(reversedCharacteristic, n + 1);
    Poly numerator = remainder(multiply(polynomialA, polynomialW), characteristic, inverseDenominator);
    numerator = remainder(multiply(numerator, boundary), characteristic, inverseDenominator);
    reverse(numerator.begin(), numerator.end());
    Poly moments = multiply(numerator, inverseDenominator);
    moments.resize(n + 1);

    // B = J + (n-3)I. Apply the binomial transform using one convolution.
    Poly first(n + 1), second(n + 1);
    int shift = (n - 3 + MOD) % MOD, shiftPower = 1;
    for (int s = 0; s <= n; ++s) {
        first[s] = i64(moments[s]) * arithmetic.invFact[s] % MOD;
        second[s] = i64(shiftPower) * arithmetic.invFact[s] % MOD;
        shiftPower = i64(shiftPower) * shift % MOD;
    }
    Poly answer = multiply(first, second);
    for (int s = 0; s <= n; ++s)
        cout << i64(answer[s]) * arithmetic.fact[s] % MOD * schedules % MOD << '\n';
}
0