結果

問題 No.3123 Inversion
ユーザー Yu_212
提出日時 2025-04-19 22:09:30
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3,117 ms / 10,000 ms
コード長 4,609 bytes
コンパイル時間 4,064 ms
コンパイル使用メモリ 281,732 KB
実行使用メモリ 244,480 KB
最終ジャッジ日時 2025-04-19 22:10:47
合計ジャッジ時間 75,697 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int iinf = 1e9;
const ll inf = 1e18;
template<ll mod>
struct Mint {
    using M=Mint; ll v;
    M& put(ll x) { v=(x<mod)?x:x-mod; return *this; }
    Mint(ll x=0) { put(x%mod+mod); }
    M operator+(M m) {return M().put(v+m.v);}
    M operator-(M m) {return M().put(v+mod-m.v);}
    M operator*(M m) {return M().put(v*m.v%mod);}
    M operator/(M m) {return M().put(v*m.inv().v%mod);}
    M operator+=(M m) { return put(v+m.v); }
    M operator-=(M m) { return put(v+mod-m.v); }
    M operator*=(M m) { return put(v*m.v%mod); }
    M operator/=(M m) { return put(v*m.inv().v%mod); }
    bool operator==(M m) { return v==m.v; }
    M pow(ll m) const {
        M x=v, res=1;
        while (m) {
            if (m&1) res=res*x;
            x=x*x; m>>=1;
        }
        return res;
    }
    M inv() { return pow(mod-2); }
};
template<ll mod>
ostream&operator<<(ostream&o,Mint<mod>v){return o<<v.v;}


template<typename T>
ostream& operator<<(ostream &o, vector<T> v) {
    for (int i = 0; i < v.size(); i++)
        o << v[i] << (i+1<v.size()?" ":"");
    return o;
}
template <typename T>
struct SegTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T ti;
    vector<T> dat;
    SegTree() {}
    SegTree(F f, T ti,int num) : f(f), ti(ti) {
        n = max(__bit_ceil(num), 1);
        dat.assign(n << 1, ti);
    }
    SegTree(F f,T ti,vector<T>&v):SegTree(f,ti,v.size()){
        for (int i = 0; i < v.size(); i++)
            dat[n + i] = v[i];
        for(int i=n-1;i;i--) dat[i]=f(dat[i*2], dat[i*2+1]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while(k >>= 1) dat[k] = f(dat[k*2], dat[k*2+1]);
    }
    T query(int a, int b) {
        if (a >= b) return ti;
        T vl = ti, vr = ti;
        for (int l=a+n, r=b+n; l<r; l>>=1, r>>=1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};

const int MOD = 998244353;
using mint = Mint<MOD>;

vector<ll> involution_counts(int N, ll mod) {
    vector<ll> a(N+1);
    if (N >= 0) a[0] = 1;
    if (N >= 1) a[1] = 1;
    for (int i = 2; i <= N; ++i) {
        a[i] = (a[i-1] + ll(i-1) * a[i-2]) % mod;
    }
    return a;
}

// 2) inv(P)==P かつ rev(P)==inv(rev(P)) となる順列数
vector<ll> inv_and_rev_involution_counts(int N, ll mod) {
    vector<ll> a(N+1);
    a[0] = 1;
    a[1] = 1;
    a[2] = 2;
    for (int i = 3; i <= N; ++i) {
        if (i & 1) {
            a[i] = a[i-1];
        } else {
            a[i] = (a[i-2]*2+a[i-4]*ll(i-2))%mod;
        }
    }
    return a;
}

// 3) rev(inv(P)) == inv(rev(P)) == P となる順列数
vector<ll> rev_inv_eq_inv_rev_eq_P_counts(int N, ll mod) {
    vector<ll> a(N+1);
    int M = N/2;
    // m! を前計算
    vector<ll> v(M+1, 1);
    for (int i = 1; i <= M; ++i)
        v[i] = i;

    SegTree<ll> seg([&](ll x, ll y) { return (x*y)%mod; }, 1, v);

    for (int n = 0; n <= N; ++n) {
        int m = n / 2;
        // m が偶数なら m!/(m/2)!, そうでなければ 0
        if (m % 2 == 0) {
            a[n] = seg.query(m/2+1, m+1);
            // a[n] = fact[m] / fact[m/2];
        } else {
            a[n] = 0;
        }
    }
    return a;
}

// 4) rev(inv(P)) == inv(rev(P)) となる順列数(R と可換)
vector<ll> commute_counts(int N, ll mod) {
    vector<ll> a(N+1);
    int M = N / 2;
    vector<ll> fact(M+1, 1);
    vector<ll> pow2(M+1, 1);
    for (int i = 1; i <= M; ++i) {
        fact[i] = fact[i-1] * i % mod;
        pow2[i] = pow2[i-1] * 2 % mod;
    }
    for (int n = 0; n <= N; ++n) {
        int m = n / 2;
        a[n] = fact[m] * pow2[m] % mod;
    }
    return a;
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int n;
    ll m;
    cin >> n >> m;
    // int N = 500;
    int N = 5000001;
    vector<ll> fact(N+1, 1);
    for (int i = 1; i <= N; ++i)
        fact[i] = fact[i-1] * i % m;
    auto a         = involution_counts(N,m);
    auto b = inv_and_rev_involution_counts(N,m);
    auto c     = rev_inv_eq_inv_rev_eq_P_counts(N,m);
    auto d       = commute_counts(N,m);
    // cout << a<< endl;
    // cout << b<< endl;
    // cout << c<< endl;
    // cout <<d<< endl;
    vector<ll> ans(N+1);
    for (int i = 0; i <= N; ++i) {
        ans[i] = fact[i]*8-a[i]*8+b[i]*6-c[i]*2-d[i]*4;
        ans[i] %= m;
        if (ans[i] < 0) ans[i] += m;
    }
    ans[1] = 1 % m;
    // cout << fact << endl;
    // cout << ans << endl;
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        cout << ans[t] << endl;
    }
}
0