結果

問題 No.2243 Coaching Schedule
ユーザー Jeroen Op de BeekJeroen Op de Beek
提出日時 2023-03-10 22:56:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 694 ms / 4,000 ms
コード長 5,254 bytes
コンパイル時間 2,857 ms
コンパイル使用メモリ 215,772 KB
実行使用メモリ 28,428 KB
最終ジャッジ日時 2023-10-18 08:38:21
合計ジャッジ時間 10,550 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
21,360 KB
testcase_01 AC 30 ms
21,360 KB
testcase_02 AC 30 ms
21,360 KB
testcase_03 AC 30 ms
21,356 KB
testcase_04 AC 30 ms
21,356 KB
testcase_05 AC 187 ms
28,428 KB
testcase_06 AC 200 ms
26,704 KB
testcase_07 AC 199 ms
26,716 KB
testcase_08 AC 199 ms
26,700 KB
testcase_09 AC 210 ms
24,896 KB
testcase_10 AC 214 ms
24,892 KB
testcase_11 AC 213 ms
24,892 KB
testcase_12 AC 694 ms
23,784 KB
testcase_13 AC 553 ms
23,776 KB
testcase_14 AC 561 ms
23,776 KB
testcase_15 AC 553 ms
23,776 KB
testcase_16 AC 110 ms
23,740 KB
testcase_17 AC 73 ms
22,608 KB
testcase_18 AC 156 ms
26,092 KB
testcase_19 AC 194 ms
24,856 KB
testcase_20 AC 145 ms
22,624 KB
testcase_21 AC 102 ms
23,908 KB
testcase_22 AC 94 ms
23,696 KB
testcase_23 AC 38 ms
21,828 KB
testcase_24 AC 121 ms
23,824 KB
testcase_25 AC 43 ms
21,816 KB
testcase_26 AC 66 ms
22,608 KB
testcase_27 AC 126 ms
23,784 KB
testcase_28 AC 45 ms
21,912 KB
testcase_29 AC 161 ms
25,000 KB
testcase_30 AC 179 ms
26,216 KB
testcase_31 AC 187 ms
26,548 KB
testcase_32 AC 89 ms
23,372 KB
testcase_33 AC 68 ms
22,872 KB
testcase_34 AC 155 ms
25,728 KB
testcase_35 AC 174 ms
25,480 KB
testcase_36 AC 175 ms
26,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1<<19, oo = 1e9;
const long long MD =  998244353;
template<long long MOD=MD> struct mdint {
    int d=0;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    bool operator==(const mdint& o) { return d==o.d;}
    bool operator!=(const mdint& o) { return d!=o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
typedef mint cd;
void revperm(cd* in, int n) {
    for(int i=0,j=0;i<n;++i) {
        if(i<j) swap(in[i],in[j]);
		for(int k = n >> 1; (j ^= k) < k; k >>= 1);
    }
}
cd w[mxN+1]; // stores w^j for each j in [0,n-1]
void precomp() {
    w[0] = 1;
    int pw = (MD-1)/mxN;
    w[1] = mint(3)^pw;
    for(int i= 2;i<=mxN;++i) {
        w[i] = w[i-1]*w[1];
    }
}
void fft(cd* in, int n, bool reverse=false) {
    int lg = __lg(n);
    assert(1<<lg == n);
    int stride = mxN/n;
    revperm(in,n);
    for(int s=1;s<=lg;++s) {
        int pw = 1<<s;
        int mstride = stride*(n>>s);
        for(int j=0;j<n;j+=pw) {
            // do FFT merging on out array
            cd* even = in+j, *odd = in+j+pw/2;
            for(int i=0;i<pw/2;++i) {
                cd& power = w[reverse?mxN-mstride*i:mstride*i];
                auto tmp = power*odd[i];
                odd[i] = even[i] - tmp;
                even[i] = even[i] + tmp;
            }
        }
    }
    if(reverse) {
        mint fac = mint(1)/n;
        for(int i=0;i<n;++i) in[i]=in[i]*fac;
    }
}

vector<cd> polymul(vector<cd>& a, vector<cd>& b) {
    int n = a.size(), m = b.size(), ptwo = 1;
    while(ptwo<(n+m)) ptwo*=2;
    a.resize(ptwo), b.resize(ptwo);
    fft(a.data(),ptwo); 
    fft(b.data(),ptwo);
    for(int i=0;i<ptwo;++i) 
        a[i] = a[i]*b[i];
    fft(a.data(),ptwo,true);
    a.resize(n+m-1);
    return a;
}
const int mxF = 2e6+2;
mint fact[mxF], ifact[mxF];
cd ncr(int a, int b) {
    if(b>a or a<0 or b<0) return 0;
    return ifact[b]*ifact[a-b];
}
void precomp2() {
    fact[0]=1;
    for(int i=1;i<mxF;++i) {
        fact[i]=fact[i-1]*i;
    }
    ifact[mxF-1] = mint(1)/fact[mxF-1];
    for(int i=mxF-2;i>=0;--i) {
        ifact[i]=ifact[i+1]*(i+1);
    }
}
int main() {
    precomp();
    precomp2();
    // ok need to choose subsets but only of unique
    // first choose number of days?
    // then choose how to do it?
    // you choose a subset of exactly so many
    // but empty is not allowed.
    // do it just with empties.
    // then choose which days to empty
    int n,m; cin >> m >> n;
    map<int,int> cnt;
    for(int i=0;i<n;++i) {
        int a; cin >> a;
        cnt[a]++;
    }
    // only sqrt items.
    // have to subtract those with ncr factors in front.
    // and one of those is just binomial coefs multiplied.
    vector<mint> aa(n+1),bb(n+1);

    map<int,int>  cntcnt;
    for(auto [j,c] : cnt) cntcnt[c]++;
    
    for(int i=1;i<=n;++i) {
        aa[i]=fact[i]^cnt.size();
        for(auto [j,c] : cntcnt) {
            if(aa[i]==0) break;
            aa[i]*=ncr(i,j)^c;
        }
        aa[i]*=ifact[i];
    }
    mint pw=1;
    for(int i=0;i<=n;++i) {
        // /i!
        bb[i] = ifact[i]*pw;
        pw*=MD-1;
    }
    auto cc = polymul(aa,bb);
    mint ans=0;
    for(int i=1;i<=n;++i) {
        cc[i]*=fact[i];
        ans+=cc[i];
    }
    for(auto [j,c] : cnt) ans*=fact[c];
    cout << ans << '\n';

}
0