結果
問題 | No.1821 LEQ-GEQ Permutations |
ユーザー |
![]() |
提出日時 | 2022-01-11 00:53:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 3,604 bytes |
コンパイル時間 | 2,355 ms |
コンパイル使用メモリ | 207,272 KB |
最終ジャッジ日時 | 2025-01-27 10:22:12 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 14 TLE * 6 MLE * 2 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p_ll; template<class T> void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; } #define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++) #define all(vec) vec.begin(), vec.end() #define rep(i,N) repr(i,0,N) #define per(i,N) for (int i=(int)N-1; i>=0; i--) const ll LLINF = pow(2,61)-1; const int INF = pow(2,30)-1; // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- const ll MOD = 998244353; struct MLL { ll x, mod; MLL(ll y=0, ll m=MOD) { x = y; mod = m; } MLL &operator+= (const MLL &p) { x = (x+p.x)%mod; return *this; } MLL &operator-= (const MLL &p) { x = (x-p.x+mod)%mod; return *this; } MLL &operator*= (const MLL &p) { x = (x*p.x)%mod; return *this; } MLL &operator/= (const MLL &p) { x = (x*p.inv().x)%mod; return *this; } MLL operator+ (const MLL &p) const { return MLL(*this)+=p; } MLL operator- (const MLL &p) const { return MLL(*this)-=p; } MLL operator* (const MLL &p) const { return MLL(*this)*=p; } MLL operator/ (const MLL &p) const { return MLL(*this)/=p; } bool operator== (const MLL &p) const { return x==p.x; } bool operator!= (const MLL &p) const { return x!=p.x; } bool operator< (const MLL &p) const { return x< p.x; } bool operator<= (const MLL &p) const { return x<=p.x; } bool operator> (const MLL &p) const { return x> p.x; } bool operator>= (const MLL &p) const { return x>=p.x; } MLL pow(MLL n) const { MLL result(1), p(x); ll tn = n.x; while(tn){ if (tn&1) result*=p; p*=p; tn>>=1; } return result; } MLL inv() const { return pow(MOD-2); } }; MLL operator+ (ll x, MLL p) { return (MLL)x+p; } MLL operator- (ll x, MLL p) { return (MLL)x-p; } MLL operator* (ll x, MLL p) { return (MLL)x*p; } MLL operator/ (ll x, MLL p) { return (MLL)x/p; } vector<MLL> fac; void c_fac(ll x=pow(10,7)+10) { fac.resize(x); rep(i,x) fac[i] = i ? fac[i-1]*i : 1; } vector<vector<MLL>> nck; void c_nck(ll N) { nck.resize(N+1, vector<MLL>(N+1)); nck[0][0] = 1; rep(i,N+1) rep(j,N+1) { if (i) nck[i][j] += nck[i-1][j]; if (i&&j) nck[i][j] += nck[i-1][j-1]; } } // MLL nck(MLL n, MLL k) { return n<0||n<k?0:fac[n.x]/(fac[k.x]*fac[(n-k).x]); }; ostream &operator<< (ostream &ost, const MLL &p) { return ost << p.x; } istream &operator>> (istream &ist, MLL &p) { return ist >> p.x; } // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- int main() { ll N; cin >> N; string S; cin >> S; ll c0 = 0, c1 = 0; rep(i,N) { if (S[i]=='0') c0++; else c1++; } c_fac(N+1); c_nck(N); assert(1<=N && N<=5000); rep(i,N) assert(S[i]=='0'||S[i]=='1'); vector<vector<MLL>> dp(N+1, vector<MLL>(N+1)); dp[0][0] = 1; rep(i,N) rep(j,N+1) { if (i+2<=N&&j+1<=N) dp[i+2][j+1] += dp[i][j] * (i+1); if (j+1<=N) dp[i+1][j+1] += dp[i][j] * (i-j); dp[i+1][j] += dp[i][j] * j; } // rep(i,N+1) debug(all(dp[i])); MLL result = 0; rep(eq01,N+1) { MLL select_eq = nck[c0+c1][eq01] * fac[eq01]; rep(eq0,eq01+1) { ll eq1 = eq01 - eq0; if (eq0>c0||eq1>c1) continue; MLL select_01 = nck[c0][eq0] * fac[c0-eq0] * nck[c1][eq1] * fac[c1-eq1]; result += select_eq * select_01 * dp[N-eq01][c0-eq0]; // cout << eq0 << " " << eq1 << "->" << select_eq * select_01 * dp[N-eq01][c0-eq0] << endl; } } cout << result << endl; return 0; }