結果

問題 No.1821 LEQ-GEQ Permutations
ユーザー saksak
提出日時 2022-01-11 00:48:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,373 bytes
コンパイル時間 2,441 ms
コンパイル使用メモリ 204,108 KB
実行使用メモリ 437,912 KB
最終ジャッジ日時 2024-04-26 20:27:49
合計ジャッジ時間 9,477 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 220 ms
166,508 KB
testcase_01 AC 222 ms
159,448 KB
testcase_02 AC 222 ms
159,544 KB
testcase_03 AC 220 ms
159,620 KB
testcase_04 AC 220 ms
159,520 KB
testcase_05 AC 219 ms
159,596 KB
testcase_06 AC 220 ms
159,424 KB
testcase_07 AC 279 ms
163,832 KB
testcase_08 AC 446 ms
177,852 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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; }
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();

  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;
}
0