結果

問題 No.3777 Train Seats
コンテスト
ユーザー HoyHoyCharhang
提出日時 2026-10-23 21:40:18
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 66 ms / 2,000 ms
+ 670µs
コード長 3,032 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,000 ms
コンパイル使用メモリ 342,380 KB
実行使用メモリ 58,240 KB
最終ジャッジ日時 2026-10-23 21:41:06
合計ジャッジ時間 5,883 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,g,n) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T, vector<T>, greater<T>>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;

template< int mod >
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
  
  static int get_mod() { return mod; }
};

using mint = ModInt<998244353>;

int main() {
  int n;
  cin >> n;
  int k = n; string s(n, '#');
  int flg = 1;
  rep(i,0,n-1) {
    if (s[i] == '#' && s[i+1] == '#') flg = 0;
  }
  vector<mint> fact(n+1, 1);
  rep(i,1,n+1) fact[i] = fact[i-1]*i;
  if (flg) {
    cout << fact[k] << endl;
    return 0;
  }
  vector<vector<vector<mint>>> dp(n, vector<vector<mint>>(n+1, vector<mint>(3, 0)));
  dp[0][0][2] = 1;
  if (s[0] == '#') dp[0][1][0] = 1;
  rep(i,1,n) rep(j,0,n+1) {
    if (s[i] == '.') {
      dp[i][j][1] += dp[i-1][j][0];
      dp[i][j][2] += dp[i-1][j][1];
    } else {
      dp[i][j][1] += dp[i-1][j][0];
      dp[i][j][2] += dp[i-1][j][1];
      if (j+1 <= n) dp[i][j+1][0] += dp[i-1][j][1]+dp[i-1][j][2];
    }
  }
  mint ans = 0;
  rep(j,0,k+1) {
    mint x = dp[n-1][j][0]+dp[n-1][j][1];
    ans += x*fact[j]*fact[k-j];
  }
  cout << ans << endl;
  return 0;
}
0