結果

問題 No.3123 Inversion
ユーザー GOTKAKO
提出日時 2025-04-20 11:51:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,522 ms / 10,000 ms
コード長 10,094 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 212,892 KB
実行使用メモリ 329,676 KB
最終ジャッジ日時 2025-04-20 11:52:38
合計ジャッジ時間 37,356 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long mod = 998244353;
//入力が必ず-mod<a<modの時.
struct mint{
    long long v = 0;
    mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;}
    mint(unsigned long long a){v = a;}
    long long val(){return v;}

    mint &operator=(const mint &b) = default;
    mint operator-() const {return mint(0)-(*this);}
    mint operator+(const mint b){return mint(v)+=b;}
    mint operator-(const mint b){return mint(v)-=b;}
    mint operator*(const mint b){return mint(v)*=b;}
    mint operator/(const mint b){return mint(v)/=b;}

    mint operator+=(const mint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    mint operator-=(const mint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    mint operator*=(const mint b){v = v*b.v%mod; return *this;}
    mint operator/=(mint b){
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }

    mint operator++(){*this += 1; return *this;}
    mint operator--(){*this -= 1; return *this;}
    mint operator++(int){*this += 1; return *this;}
    mint operator--(int){*this -= 1; return *this;}
    bool operator==(const mint b){return v == b.v;}
    bool operator!=(const mint b){return v != b.v;}
    bool operator>(const mint b){return v > b.v;}
    bool operator>=(const mint b){return v >= b.v;}
    bool operator<(const mint b){return v < b.v;}
    bool operator<=(const mint b){return v <= b.v;}

    mint pow(long long n){
        mint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    mint inv(){return mint(1)/v;}
};

class UnionFind{
    private:
    vector<int> par,siz;
    public:
    UnionFind(int N){
        par.resize(N,-1);
        siz.resize(N,1);
    }
 
    int root(int x){ //連結成分の代表頂点を返す.
        if(par.at(x) == -1) return x;
        else return par.at(x) = root(par.at(x));
    }
    bool unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す.
        u = root(u),v = root(v);
        if(u == v) return false;
 
        if(siz.at(u) < siz.at(v)) swap(u,v); //Union by size.
        par.at(v) = u;
        siz.at(u) += siz.at(v);
        return true;
    }
    bool issame(int u, int v){ //同じ連結成分ならtrue.
        if(root(u) == root(v)) return true;
        else return false;
    }
    int size(int pos){return siz.at(root(pos));} //posの連結成分の大きさを返す.
};

long long inv(vector<int> P){
    int N = P.size();
    long long ret = 0;
    for(int i=0; i<N; i++) for(int k=i+1; k<N; k++) ret += P.at(i)>P.at(k);
    return ret;
}

using SS = mint;
class SegmentTree{
    public:
    int siz = -1,n = -1;
    vector<SS> dat;
 
    SS op(SS a, SS b){return a*b;}
    SS e(){return 1;}
    void renew (SS &a,SS x){
        a = op(a,x);
        //a = x; //set(pos,x)で可能.
        //その他.
    }
 
    SegmentTree(int N){init(N);}
    SegmentTree(const vector<SS> &A){//長さ配列サイズに合わせる.
        siz = 1; n = A.size();
        while(siz < n) siz *= 2;
        dat.resize(siz*2,e());
        for(int i=0; i<n; i++) dat.at(i+siz) = A.at(i);
        for(int i=siz-1; i>0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1));
    }
    void init(int N){
        //全要素単位元に初期化.
        siz = 1; n = N;
        while(siz < n) siz *= 2;
        dat.assign(siz*2,e());
    }
    void init(const vector<SS> &A){//長さ配列サイズに合わせる.
        siz = 1; n = A.size();
        dat.resize(siz*2,e());
        for(int i=0; i<n; i++) dat.at(i+siz) = A.at(i);
        for(int i=siz-1; i>0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1));
    }
    void set(int pos,SS x){
        pos = pos+siz;
        dat.at(pos) = x;
        while(pos != 1){
            pos = pos/2;
            dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1));
        }
    }
    void update(int pos,SS x){
        pos = pos+siz;
        renew(dat.at(pos),x);
        while(pos != 1){
            pos = pos/2;
            dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1));
        }
    } 
    SS findans(int l, int r){
        SS retl = e(),retr = e();
        l += siz,r += siz;
        while(l < r){
            if(l&1) retl = op(retl,dat.at(l++));
            if(r&1) retr = op(dat.at(--r),retr);
            l >>= 1; r >>= 1;
        }
        return op(retl,retr);
    }
    SS get(int pos){return dat.at(pos+siz);}
    SS rangeans(int l, int r){return findans(l,r);}
    SS allrange(){return dat.at(1);}
 
    //rightは) leftは[で 渡す&返す. 
    int maxright(const function<bool(SS)> f,int l = 0){
        //fを満たさない最小の箇所を返す なければn.
        l += siz; int r = n+siz;
        vector<int> ls,rs;
        while(l < r){
            if(l&1) ls.push_back(l++);
            if(r&1) rs.push_back(--r);
            l >>= 1; r >>= 1; 
        }
        SS okl = e();
        for(int i=0; i<ls.size(); i++){
            l = ls.at(i);
            SS now = op(okl,dat.at(l));
            if(!f(now)){
                while(l < siz){
                    l <<= 1;
                    now = op(okl,dat.at(l));
                    if(f(now)){okl = now; l++;}
                }
                return l-siz;
            } 
            okl = now;
        }
        for(int i=rs.size()-1; i>=0; i--){
            l = rs.at(i);
            SS now = op(okl,dat.at(l));
            if(!f(now)){
                while(l < siz){
                    l <<= 1;
                    now = op(okl,dat.at(l));
                    if(f(now)){okl = now; l++;}
                }
                return l-siz;
            } 
            okl = now;
        }
        return n;
    }
    int minleft(const function<bool(SS)> f,int r = -1){
        //fを満たす最小の箇所を返す なければ0.
        if(r == -1) r = n;
        int l = siz; r += siz;
        vector<int> ls,rs;
        while(l < r){
            if(l&1) ls.push_back(l++);
            if(r&1) rs.push_back(--r);
            l >>= 1; r >>= 1; 
        }
        SS okr = e();
        for(int i=0; i<rs.size(); i++){
            r = rs.at(i);
            SS now = op(dat.at(r),okr);
            if(!f(now)){
                while(r < siz){
                    r <<= 1; r++;
                    now = op(dat.at(r),okr);
                    if(f(now)){okr = now; r--;}
                }
                return r+1-siz;
            }
            okr = now;
        }
        for(int i=ls.size()-1; i>=0; i--){
            r = ls.at(i);
            SS now = op(dat.at(r),okr);
            if(!f(now)){
                while(r < siz){
                    r <<= 1; r++;
                    now = op(dat.at(r),okr);
                    if(f(now)){okr = now; r--;}
                }
                return r+1-siz;
            }
            okr = now;
        }
        return 0;
    }
};

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

    int T; cin >> T;
    cin >> mod;
    if(mod == 1){
        while(T--) cout << "0\n";
        return 0;
    }

    int n = 5000000;
    vector<mint> fac(n+1,1); for(int i=1; i<=n; i++) fac.at(i) = fac.at(i-1)*i;
    vector<mint> give(n+1);
    for(int i=0; i<=n; i++) give.at(i) = i%mod;
    SegmentTree Seg(give);

    //OEIS最高!→え~何が違うの→mod合成数の時facの逆元が悪さしてる.
    //連結成分サイズ2 逆順列=revとなる数 A037224->A001813 2n!/n!->(n+1)*(n+2)*...*2nなのでセグ木.
    //連結成分サイズ2 それ以外 A000898.
    //連結成分サイズ8  A000899*8. n!-2Xn-Y[n/2]+2*Two[n/2]

    vector<mint> Two(n/2+1),Eight(n+1),X(n+1),Y(n/2+1);
    Two.at(0) = 1; Two.at(1) = 2%mod; X.at(0) = 1; X.at(1) = 1; X.at(2) = 2%mod;
    for(int i=2; i<=n/2; i++) Two.at(i) = (Two.at(i-1)+Two.at(i-2)*(i-1))*2;
    for(int i=3; i<=n; i++) X.at(i) = X.at(i-1)+X.at(i-2)*(i-1);
    mint p2 = 1;
    for(int i=0; i<=n/2; i++) Y.at(i) = fac.at(i)*p2,p2 += p2;
    for(int i=1; i<=n; i++) Eight.at(i) = fac.at(i)-X.at(i)*2-Y.at(i/2)+Two.at(i/2)*2;

    while(T--){
        int N; cin >> N;
        if(N == 1){cout << 1 << "\n"; continue;}
        mint two = Two.at(N/2),answer = 0,eight = Eight.at(N),four = 0;
        if(N%4 == 0 || N%4 == 1){
            int M = N/4;
            two += Seg.rangeans(M+1,2*M+1);
        }
            four = fac.at(N)-two-eight;
        answer = two*2+four*4+eight*8;
        answer.v %= mod;
        cout << answer.v << "\n"; continue;

        long long f = 1;
        for(int i=1; i<=N; i++) f *= i;
        UnionFind Z(f);

        map<vector<int>,long long> name;
        vector<int> P(N);
        iota(P.begin(),P.end(),0);
        do{
            name[P] = name.size();
        }while(next_permutation(P.begin(),P.end()));
        iota(P.begin(),P.end(),0);
        vector<int> Q(N);
        int idx = -1;
        do{
            idx++;
            reverse(P.begin(),P.end());
            Z.unite(idx,name[P]);
            reverse(P.begin(),P.end());
            for(int i=0; i<N; i++) Q.at(P.at(i)) = i;
            Z.unite(idx,name[Q]); 
        }while(next_permutation(P.begin(),P.end()));

        map<long long,long long> ap;
        for(auto [K,v] : name){
            ap[Z.size(v)]++;
            if(Z.size(v) == 2) continue;
            //continue;

            
            vector<int> S(N);
            for(int i=0; i<N; i++) S[i] = K.at(i)+K.at(N-1-i);
            
            for(auto p : K) cout << p+1 << " ";
            cout << " ";
            for(auto p : S) cout << p+1 << " ";
            cout << " ";            
            //cout << " " << inv(K);
            cout << " " << Z.size(v) << endl;
            vector<bool> B(N);
        }
        //cout << N << endl;
        long long ans = 0;
        for(auto [k,v] : ap) cout << k << "->" << v << endl,ans += k*v;
        cout << ans << endl;
        cout << endl;

    }
}  
0