結果

問題 No.2904 Distinct Multisets in a Way
ユーザー Today03Today03
提出日時 2024-09-28 03:52:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 8,316 bytes
コンパイル時間 5,328 ms
コンパイル使用メモリ 296,456 KB
実行使用メモリ 24,312 KB
最終ジャッジ日時 2024-09-28 03:52:50
合計ジャッジ時間 13,151 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 490 ms
24,312 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 507 ms
24,308 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 234 ms
13,572 KB
testcase_25 AC 488 ms
24,304 KB
testcase_26 AC 27 ms
6,944 KB
testcase_27 AC 230 ms
11,188 KB
testcase_28 AC 486 ms
22,948 KB
testcase_29 AC 112 ms
8,656 KB
testcase_30 AC 234 ms
13,268 KB
testcase_31 AC 485 ms
23,740 KB
testcase_32 AC 27 ms
6,944 KB
testcase_33 AC 54 ms
6,940 KB
testcase_34 AC 54 ms
6,940 KB
testcase_35 AC 487 ms
21,488 KB
testcase_36 AC 483 ms
21,504 KB
testcase_37 AC 487 ms
24,248 KB
testcase_38 AC 13 ms
6,944 KB
testcase_39 AC 508 ms
21,832 KB
testcase_40 AC 233 ms
12,440 KB
testcase_41 AC 233 ms
12,788 KB
testcase_42 AC 482 ms
19,240 KB
testcase_43 AC 7 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

/*
    https://x.com/hamamu_kyopro/status/1839680787920322683
    https://x.com/eco4kb/status/1839666741406011512

    f(x)=(1+x)(1+x^2)(1+x^4)…(1+x^(2^(N-1)))として、
    f(x)^2=g(x)h(x)(ただし、係数和が両方ともN)と因数分解する方法に帰着される。
*/

/* from: https://potato167.github.io/po167_library */

#include <atcoder/convolution>
#include <atcoder/modint>

template <typename T>
vector<T> fpsMul(const vector<T> &a, const vector<T> &b) {
    return atcoder::convolution(a, b);
}

template <typename T>
vector<T> fpsAdd(const vector<T> &a, const vector<T> &b) {
    vector<T> res(max(a.size(), b.size()));
    for (int i = 0; i < res.size(); i++) {
        if (i < a.size()) res[i] += a[i];
        if (i < b.size()) res[i] += b[i];
    }
    return res;
}

template <typename T>
vector<T> fpsSub(const vector<T> &a, const vector<T> &b) {
    vector<T> res(max(a.size(), b.size()));
    for (int i = 0; i < res.size(); i++) {
        if (i < a.size()) res[i] += a[i];
        if (i < b.size()) res[i] -= b[i];
    }
    return res;
}

template <typename T>
vector<T> fpsInv(vector<T> f, int len = -1) {
    if (len == -1) len = f.size();
    assert(f[0] != 0);
    vector<T> g = {1 / f[0]};
    int s = 1;
    while (s < len) {
        // g = 2g_s - f(g_s)^2 (mod x ^ (2 * s))
        // g = g - (fg - 1)g
        // (fg - 1) = 0 (mod x ^ (s))
        vector<T> n_g(s * 2, 0);
        vector<T> f_s(s * 2, 0);
        g.resize(s * 2);
        for (int i = 0; i < s * 2; i++) {
            if (int(f.size()) > i) f_s[i] = f[i];
            n_g[i] = g[i];
        }
        atcoder::internal::butterfly(g);
        atcoder::internal::butterfly(f_s);
        for (int i = 0; i < s * 2; i++) f_s[i] *= g[i];
        atcoder::internal::butterfly_inv(f_s);
        T iz = 1 / (T)(s * 2);
        for (int i = s; i < s * 2; i++) f_s[i] *= iz;
        for (int i = 0; i < s; i++) f_s[i] = 0;
        atcoder::internal::butterfly(f_s);
        for (int i = 0; i < s * 2; i++) f_s[i] *= g[i];
        atcoder::internal::butterfly_inv(f_s);
        for (int i = s; i < s * 2; i++) n_g[i] -= f_s[i] * iz;
        swap(n_g, g);
        s *= 2;
    }
    g.resize(len);
    return g;
}

template <typename T>
vector<T> fpsCyclicConvolution(vector<T> f, vector<T> g) {
    atcoder::internal::butterfly(f);
    atcoder::internal::butterfly(g);
    for (int i = 0; i < (int)f.size(); i++) f[i] *= g[i];
    atcoder::internal::butterfly_inv(f);
    T iz = (T)(1) / (T)(f.size());
    for (int i = 0; i < (int)f.size(); i++) f[i] *= iz;
    return f;
}

template <typename T>
vector<T> fpsIntegral(vector<T> f) {
    if (f.empty()) return f;
    vector<T> num_inv((int)f.size() + 1);
    num_inv[0] = 1;
    num_inv[1] = 1;
    auto m = T::mod();
    for (int i = 2; i <= (int)f.size(); i++) num_inv[i] = (0 - num_inv[m % i]) * (T)(m / i);
    f.reserve((int)f.size() + 1);
    f.push_back(0);
    for (int i = (int)f.size() - 1; i > 0; i--) f[i] = f[i - 1] * num_inv[i];
    f[0] = 0;
    return f;
}

template <typename T>
vector<T> fpsDifferential(vector<T> f) {
    if (f.empty()) return f;
    for (int i = 0; i < (int)f.size() - 1; i++) f[i] = f[i + 1] * (T)(i + 1);
    f.pop_back();
    return f;
}

template <typename T>
vector<T> fpsExp(vector<T> f, int len = -1) {
    if (len == -1) len = f.size();
    if (len == 0) return {};
    if (len == 1) return {T(1)};
    assert(!f.empty() && f[0] == 0);
    int s = 1;
    // simple
    vector<T> g = {T(1)};
    while (s < len) {
        // g' / g
        // A * B
        vector<T> A = g, B = g;
        A = fpsDifferential(A);
        B = fpsInv(B, 2 * s);
        A.resize(2 * s);
        A = fpsCyclicConvolution(A, B);
        A.pop_back();
        A = fpsIntegral(A);
        for (int i = 0; i < s; i++) A[i] = 0;
        for (int i = s; i < s * 2; i++) A[i] = (i < (int)f.size() ? f[i] : 0) - A[i];
        // g_hat = g (1 - g + f)
        // g += B = g * A
        g.resize(2 * s);
        B = fpsCyclicConvolution(A, g);
        for (int i = s; i < s * 2; i++) g[i] = B[i];
        s *= 2;
    }
    g.resize(len);
    return g;
}

template <typename T>
vector<T> fpsLog(vector<T> f, int len = -1) {
    if (len == -1) len = f.size();
    if (len == 0) return {};
    if (len == 1) return {T(0)};
    assert(!f.empty() && f[0] == 1);
    vector<T> res = atcoder::convolution(fpsDifferential(f), fpsInv(f, len));
    res.resize(len - 1);
    return fpsIntegral(res);
}

template <class T>
vector<T> fpsPow(vector<T> f, long long M, int len = -1) {
    if (len == -1) len = f.size();
    vector<T> res(len, 0);
    if (M == 0) {
        res[0] = 1;
        return res;
    }
    for (int i = 0; i < (int)f.size(); i++) {
        if (f[i] == 0) continue;
        if (i > (len - 1) / M) break;
        vector<T> g((int)f.size() - i);
        T v = (T)(1) / (T)(f[i]);
        for (int j = i; j < (int)f.size(); j++) g[j - i] = f[j] * v;
        long long zero = i * M;
        if (i) len -= i * M;
        g = fpsLog(g, len);
        for (T &x : g) x *= M;
        g = fpsExp(g, len);
        v = (T)(1) / v;
        T c = 1;
        while (M) {
            if (M & 1) c = c * v;
            v = v * v;
            M >>= 1;
        }
        for (int i = 0; i < len; i++) res[i + zero] = g[i] * c;
        return res;
    }
    return res;
}

// in  : DFT(v) (len(v) = z)
// out : DFT(v) (len(v) = 2 * z)
template <typename T>
void fpsExtend(vector<T> &v) {
    int z = v.size();
    T e = (T(atcoder::internal::primitive_root_constexpr(T::mod()))).pow(T::mod() / (2 * z));
    auto cp = v;
    atcoder::internal::butterfly_inv(cp);
    T tmp = (T)(1) / (T)(z);
    for (int i = 0; i < z; i++) {
        cp[i] *= tmp;
        tmp *= e;
    }
    atcoder::internal::butterfly(cp);
    for (int i = 0; i < z; i++) v.push_back(cp[i]);
}

// s.t |v| = 2 ^ s (no assert)
template <typename T>
void fpsPickEvenOdd(vector<T> &v, int odd) {
    int z = v.size() / 2;
    T half = (T)(1) / (T)(2);
    if (odd == 0) {
        for (int i = 0; i < z; i++) v[i] = (v[i * 2] + v[i * 2 + 1]) * half;
        v.resize(z);
    } else {
        T e = (T(atcoder::internal::primitive_root_constexpr(T::mod()))).pow(T::mod() / (2 * z));
        T ie = T(1) / e;
        vector<T> es = {half};
        while ((int)es.size() != z) {
            vector<T> n_es((int)es.size() * 2);
            for (int i = 0; i < (int)es.size(); i++) {
                n_es[i * 2] = (es[i]);
                n_es[i * 2 + 1] = (es[i] * ie);
            }
            ie *= ie;
            swap(n_es, es);
        }
        for (int i = 0; i < z; i++) v[i] = (v[i * 2] - v[i * 2 + 1]) * es[i];
        v.resize(z);
    }
}

// return [x^k] P(x) / Q(x)
template <typename T>
T bostanMori(long long k, vector<T> P, vector<T> Q) {
    assert(!Q.empty() && Q[0] != 0);
    int z = 1;
    while (z < (int)max(P.size(), Q.size())) z *= 2;
    P.resize(z * 2, 0);
    Q.resize(z * 2, 0);
    atcoder::internal::butterfly(P);
    atcoder::internal::butterfly(Q);
    // fast
    while (k) {
        // Q(-x)
        vector<T> Q_n(z * 2);
        for (int i = 0; i < z; i++) {
            Q_n[i * 2] = Q[i * 2 + 1];
            Q_n[i * 2 + 1] = Q[i * 2];
        }
        for (int i = 0; i < z * 2; i++) {
            P[i] *= Q_n[i];
            Q[i] *= Q_n[i];
        }
        fpsPickEvenOdd(P, k & 1);
        fpsPickEvenOdd(Q, 0);
        k /= 2;
        if (k == 0) break;
        fpsExtend(P);
        fpsExtend(Q);
    }
    T SP = 0, SQ = 0;
    for (int i = 0; i < z; i++) SP += P[i], SQ += Q[i];
    return SP / SQ;
}

template <typename T>
// 0 = a[i] * c[0] + a[i - 1] * c[1] + a[i - 2] * c[2] + ... + a[i - d] * c[d]
// a.size() + 1 == c.size()
// c[0] = - 1 ?
// return a[k]
T kthLinear(long long k, vector<T> a, vector<T> c) {
    int d = a.size();
    assert(d + 1 == int(c.size()));
    vector<T> P = atcoder::convolution(a, c);
    P.resize(d);
    return bostanMori(k, P, c);
}

#include <atcoder/modint>
using mint = atcoder::modint998244353;

int main() {
    int N;
    cin >> N;

    vector<mint> F(3);
    F[0] = F[1] = F[2] = 1;
    F = fpsPow(F, N, N + 1);

    mint ans = F[N];
    ans--;
    ans /= 2;

    cout << ans.val() << endl;
}
0