結果
問題 | No.2898 Update Max |
ユーザー | 👑 potato167 |
提出日時 | 2024-09-20 21:47:32 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 28 ms / 2,000 ms |
コード長 | 5,849 bytes |
コンパイル時間 | 2,154 ms |
コンパイル使用メモリ | 211,708 KB |
実行使用メモリ | 8,436 KB |
最終ジャッジ日時 | 2024-09-20 21:47:36 |
合計ジャッジ時間 | 3,856 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 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 | 22 ms
5,632 KB |
testcase_09 | AC | 23 ms
5,632 KB |
testcase_10 | AC | 21 ms
5,632 KB |
testcase_11 | AC | 21 ms
5,760 KB |
testcase_12 | AC | 22 ms
5,632 KB |
testcase_13 | AC | 24 ms
5,888 KB |
testcase_14 | AC | 22 ms
5,760 KB |
testcase_15 | AC | 22 ms
5,888 KB |
testcase_16 | AC | 21 ms
5,888 KB |
testcase_17 | AC | 22 ms
5,888 KB |
testcase_18 | AC | 25 ms
7,028 KB |
testcase_19 | AC | 28 ms
7,156 KB |
testcase_20 | AC | 26 ms
7,032 KB |
testcase_21 | AC | 26 ms
7,028 KB |
testcase_22 | AC | 26 ms
7,024 KB |
testcase_23 | AC | 25 ms
8,388 KB |
testcase_24 | AC | 25 ms
8,308 KB |
testcase_25 | AC | 26 ms
8,436 KB |
testcase_26 | AC | 25 ms
8,436 KB |
testcase_27 | AC | 24 ms
8,304 KB |
testcase_28 | AC | 24 ms
8,436 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
ソースコード
#line 1 "g.cpp" #include <bits/stdc++.h> using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; const ll ILL=2167167167167167167; const int INF=2100000000; #define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++) #define all(p) p.begin(),p.end() template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>; template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template<class T> bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;} template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;} template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());} template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} bool yneos(bool a,bool upp=0){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;} template<class T> void vec_out(vector<T> &p,int ty=0){ if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";} else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}} template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;} template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;} template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;} int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;} template<class T> bool inside(T l,T x,T r){return l<=x&&x<r;} #include <atcoder/modint> using mint = atcoder::modint998244353; #line 2 "/Users/Shared/po167_library/math/Binomial.hpp" #line 5 "/Users/Shared/po167_library/math/Binomial.hpp" namespace po167{ template<class T> struct Binomial{ std::vector<T> fact_vec, fact_inv_vec; void extend(int m = -1){ int n = fact_vec.size(); if (m == -1) m = n * 2; if (n >= m) return; fact_vec.resize(m); fact_inv_vec.resize(m); for (int i = n; i < m; i++){ fact_vec[i] = fact_vec[i - 1] * T(i); } fact_inv_vec[m - 1] = T(1) / fact_vec[m - 1]; for (int i = m - 1; i > n; i--){ fact_inv_vec[i - 1] = fact_inv_vec[i] * T(i); } } Binomial(int MAX = 0){ fact_vec.resize(1, T(1)); fact_inv_vec.resize(1, T(1)); extend(MAX + 1); } T fact(int i){ if (i < 0) return 0; while (int(fact_vec.size()) <= i) extend(); return fact_vec[i]; } T invfact(int i){ if (i < 0) return 0; while (int(fact_inv_vec.size()) <= i) extend(); return fact_inv_vec[i]; } T C(int a, int b){ if (a < b || b < 0) return 0; return fact(a) * invfact(b) * invfact(a - b); } T invC(int a, int b){ if (a < b || b < 0) return 0; return fact(b) * fact(a - b) *invfact(a); } T P(int a, int b){ if (a < b || b < 0) return 0; return fact(a) * invfact(a - b); } T inv(int a){ if (a < 0) return inv(-a) * T(-1); if (a == 0) return 1; return fact(a - 1) * invfact(a); } T Catalan(int n){ if (n < 0) return 0; return fact(2 * n) * invfact(n + 1) * invfact(n); } T narayana(int n, int k){ if (n <= 0 || n < k || k < 1) return 0; return C(n, k) * C(n, k - 1) * inv(n); } T Catalan_pow(int n,int d){ if (n < 0 || d < 0) return 0; if (d == 0){ if (n == 0) return 1; return 0; } return T(d) * inv(d + n) * C(2 * n + d - 1, n); } // retrun [x^a] 1/(1-x)^b T ruiseki(int a,int b){ if (a < 0 || b < 0) return 0; if (a == 0){ return 1; } return C(a + b - 1, b - 1); } // (a, b) -> (c, d) // always x + e >= y T mirror(int a, int b, int c, int d, int e = 0){ if (a + e < b || c + e < d) return 0; if (a > c || b > d) return 0; a += e; c += e; return C(c + d - a - b, c - a) - C(c + d - a - b, c - b + 1); } // return sum_{i = 0, ... , a} sum_{j = 0, ... , b} C(i + j, i) // return C(a + b + 2, a + 1) - 1; T gird_sum(int a, int b){ if (a < 0 || b < 0) return 0; return C(a + b + 2, a + 1) - 1; } // return sum_{i = a, ..., b - 1} sum_{j = c, ... , d - 1} C(i + j, i) // AGC 018 E T gird_sum_2(int a, int b, int c, int d){ if (a >= b || c >= d) return 0; a--, b--, c--, d--; return gird_sum(a, c) - gird_sum(a, d) - gird_sum(b, c) + gird_sum(b, d); } }; } #line 33 "g.cpp" void solve(); // CYAN / FREDERIC int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; rep(i, 0, t) solve(); } void solve(){ int N; cin >> N; vector<int> A(N); rep(i, 0, N) cin >> A[i], A[i]--; vector<int> sum(N + 1); vector<int> inv(N, -1); rep(i, 0, N){ if (A[i] < 0) sum[i + 1] = 1; else inv[A[i]] = i; sum[i + 1] += sum[i]; } int tmp = 0; po167::Binomial<mint> table; mint ans = 0; int tp = N; for (int i = N - 1; i >= 0; i--){ if (inv[i] == -1){ tmp += 1; mint val = 0; val = table.P(sum[N], tmp) - table.P(sum[N] - sum[tp], tmp); val *= table.inv(tmp); val *= table.fact(sum[N] - tmp); ans += val; } else{ if (chmin(tp, inv[i])){ ans += table.P(sum[N] - sum[tp], tmp) * table.fact(sum[N] - tmp); } } } cout << ans.val() << "\n"; }