結果

問題 No.2898 Update Max
ユーザー shkiiii_shkiiii_
提出日時 2024-09-21 01:54:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 4,958 bytes
コンパイル時間 2,518 ms
コンパイル使用メモリ 207,912 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-09-21 01:54:09
合計ジャッジ時間 4,959 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 31 ms
12,672 KB
testcase_09 AC 39 ms
12,672 KB
testcase_10 AC 32 ms
12,672 KB
testcase_11 AC 32 ms
12,672 KB
testcase_12 AC 31 ms
12,544 KB
testcase_13 AC 33 ms
12,672 KB
testcase_14 AC 33 ms
12,672 KB
testcase_15 AC 32 ms
12,544 KB
testcase_16 AC 31 ms
12,672 KB
testcase_17 AC 32 ms
12,672 KB
testcase_18 AC 36 ms
12,672 KB
testcase_19 AC 36 ms
12,672 KB
testcase_20 AC 36 ms
12,672 KB
testcase_21 AC 36 ms
12,672 KB
testcase_22 AC 38 ms
12,672 KB
testcase_23 AC 32 ms
12,672 KB
testcase_24 AC 32 ms
12,672 KB
testcase_25 AC 32 ms
12,672 KB
testcase_26 AC 33 ms
12,800 KB
testcase_27 AC 31 ms
12,672 KB
testcase_28 AC 26 ms
12,800 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


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 modint = ModInt< MOD >;
using mvi = vector<modint>;
using mvvi = vector<mvi>;

class Comb{
  public:
  const int n;
  // const int mod = 1000000007;
  // const int mod = 998244353;
  mvi fac,refac;
  Comb(int N) : n(N),fac(N+1,1),refac(N+1,1) {
    for(int i = 0;i < n;i++)fac[i+1] = fac[i]*(i+1);
    refac[n] = fac[n].inverse();
    for(int i = n;i > 0;i--)refac[i-1] = refac[i]*i;
  }
  modint comb(long long n,long long r){
    if(n < r)return 0;
    modint res = fac[n]*refac[r];
    res = res*refac[n-r];
    return res;
  }
  long long modpow(long long n,long long r){
    long long res = 1;
    while(r){
      if(r & 1)res = res*n;
      n = n*n;
      r >>= 1;
    }
    return res;
  }
};

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;cin >> n;
  vi a(n);cin >> a;
  modint X = 0;
  rep(i,n)X += (a[i] == -1);
  vi inv(n+1,-1);
  rep(i,n)if(a[i] != -1)inv[a[i]] = i;
  vi x(n+1);
  vi r(n+1,MOD);
  vi cnt(n+1);
  for(int i = n;i > 0;i--){
    if(i != n)x[i] = x[i+1];
    if(inv[i] == -1)x[i]++;

    if(i != n)chmin(r[i],r[i+1]);
    if(inv[i] != -1)chmin(r[i],inv[i]);
  }
  for(int i = n-1;i >= 0;i--){
    if(a[i] == -1)cnt[i]++;
    if(i != n-1)cnt[i] += cnt[i+1];
  }
  Comb cb(n+1);
  modint res = 0;
  for(int k = 1;k <= n;k++){
    if(inv[k] != -1){
      if(r[k] < inv[k])continue;
      res += cb.comb(cnt[r[k]],x[k])*cb.fac[x[k]]*cb.fac[X.x-x[k]];
      continue;
    }
    res += cb.comb(X.x,x[k])*cb.fac[x[k]-1]*cb.fac[X.x-x[k]];
    if(r[k] != MOD){
      res -= cb.comb(cnt[r[k]],x[k])*cb.fac[x[k]-1]*cb.fac[X.x-x[k]];
    }
    modint xx = cb.comb(X.x,x[k])*cb.fac[x[k]-1]*cb.fac[X.x-x[k]];
    if(r[k] != MOD)xx -= cb.comb(cnt[r[k]],x[k])*cb.fac[x[k]-1]*cb.fac[X.x-x[k]];
  }
  cout << res << "\n";

  

  return 0;
}
0