結果
問題 | No.1193 Penguin Sequence |
ユーザー | snow39 |
提出日時 | 2020-08-22 16:37:59 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 699 ms / 2,000 ms |
コード長 | 4,302 bytes |
コンパイル時間 | 2,051 ms |
コンパイル使用メモリ | 120,132 KB |
実行使用メモリ | 39,848 KB |
最終ジャッジ日時 | 2024-04-23 10:39:14 |
合計ジャッジ時間 | 21,388 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 480 ms
39,704 KB |
testcase_01 | AC | 685 ms
39,720 KB |
testcase_02 | AC | 679 ms
39,844 KB |
testcase_03 | AC | 682 ms
39,724 KB |
testcase_04 | AC | 699 ms
39,848 KB |
testcase_05 | AC | 680 ms
39,844 KB |
testcase_06 | AC | 688 ms
39,848 KB |
testcase_07 | AC | 679 ms
39,720 KB |
testcase_08 | AC | 685 ms
39,632 KB |
testcase_09 | AC | 694 ms
39,720 KB |
testcase_10 | AC | 683 ms
39,716 KB |
testcase_11 | AC | 385 ms
34,068 KB |
testcase_12 | AC | 385 ms
34,176 KB |
testcase_13 | AC | 570 ms
38,196 KB |
testcase_14 | AC | 545 ms
37,504 KB |
testcase_15 | AC | 625 ms
39,176 KB |
testcase_16 | AC | 211 ms
28,296 KB |
testcase_17 | AC | 51 ms
26,912 KB |
testcase_18 | AC | 100 ms
28,024 KB |
testcase_19 | AC | 676 ms
39,544 KB |
testcase_20 | AC | 521 ms
37,272 KB |
testcase_21 | AC | 419 ms
34,616 KB |
testcase_22 | AC | 104 ms
28,368 KB |
testcase_23 | AC | 351 ms
33,484 KB |
testcase_24 | AC | 317 ms
32,752 KB |
testcase_25 | AC | 171 ms
29,540 KB |
testcase_26 | AC | 91 ms
27,648 KB |
testcase_27 | AC | 571 ms
37,976 KB |
testcase_28 | AC | 381 ms
34,184 KB |
testcase_29 | AC | 563 ms
38,172 KB |
testcase_30 | AC | 217 ms
30,532 KB |
testcase_31 | AC | 188 ms
29,956 KB |
testcase_32 | AC | 448 ms
36,164 KB |
testcase_33 | AC | 316 ms
32,948 KB |
testcase_34 | AC | 272 ms
32,092 KB |
testcase_35 | AC | 320 ms
33,036 KB |
testcase_36 | AC | 264 ms
31,964 KB |
testcase_37 | AC | 549 ms
37,892 KB |
testcase_38 | AC | 53 ms
26,880 KB |
testcase_39 | AC | 66 ms
27,008 KB |
testcase_40 | AC | 73 ms
27,008 KB |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=998244353; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} #define MAX_N 1000010 ll inv[MAX_N+10],fac[MAX_N+10],ifac[MAX_N+10]; void setComb(){ inv[0]=1;inv[1]=1;fac[1]=1;ifac[1]=1;fac[0]=1;ifac[0]=1; for(int i=2;i<MAX_N;i++){ inv[i]=(-MOD/i)*inv[MOD%i]%MOD; fac[i]=fac[i-1]*i%MOD; ifac[i]=ifac[i-1]*inv[i]%MOD; inv[i]=(inv[i]+MOD)%MOD; fac[i]=(fac[i]+MOD)%MOD; ifac[i]=(ifac[i]+MOD)%MOD; } return; } ll comb(ll n,ll k){ if(n<k||n<0||k<0) return 0; else return ((fac[n]*ifac[k]%MOD*ifac[n-k]%MOD+MOD)%MOD); } ll hcomb(ll n,ll r){// this size is really ok?? if(n==0&&r==0) return 1; else if(n<0||r<0) return 0; else return comb(n+r-1,r); } ll mod_pow(ll x,ll n){ x%=MOD; ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n>>=1; } return res; } void add(ll &a,ll b){ a=(a+b)%MOD; } void mul(ll &a,ll b){ a%=MOD;b%=MOD; a=a*b%MOD; } #include <functional> #include <climits> //SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX); template< typename T> class SegmentTree{ public: using F = function<T(T,T)>; int n; vector<T> tree; F operation; T def; SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){ n=1; while(n<size) n*=2; tree.resize(2*n-1,def); } SegmentTree(){} void embody(int size,F _operation,T _def){ operation=_operation; def=_def; n=1; while(n<size) n*=2; tree.resize(2*n-1,def); } void initialize(vector<T> v){ int vSize=v.size(); n=1; while(n<vSize) n*=2; tree.resize(2*n-1,def); for(int i=0;i<vSize;i++) tree[i+n-1]=v[i]; for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]); } void update(int index,T value){ index+=n-1; tree[index]=value; while(index>0){ index=(index-1)/2; tree[index]=operation(tree[2*index+1],tree[2*index+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b),getMin(a,b,0,0,-1) if(r<0) r=n; if(r<=a||b<=l) return def; else if(a<=l&&r<=b) return tree[k]; else{ T lval=query(a,b,2*k+1,l,(l+r)/2); T rval=query(a,b,2*k+2,(l+r)/2,r); return operation(lval,rval); } } int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal) if(r<0) r=n; if(r-l==1) return k-(n-1); if(tree[2*k+1]>=x) return find(x,2*k+1,l,(l+r)/2); else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r); } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin>>N; vector<int> A(N); rep(i,N) cin>>A[i]; vector<int> v=A; sort(all(v)); v.erase(unique(all(v)),v.end()); int V=v.size(); map<int,int> mp; rep(i,V) mp[v[i]]=i; rep(i,N) A[i]=mp[A[i]]; setComb(); ll all=1; for(int i=1;i<=N;i++) mul(all,comb(N,i)); ll minus=0; for(int i=1;i<=N;i++){ ll val=comb(N-1,i-1)*mod_pow(comb(N,i),MOD-2)%MOD; mul(val,val); add(minus,val); } ll one=0; for(int i=1;i<=N;i++) add(one,comb(N-1,i-1)*mod_pow(comb(N,i),MOD-2)%MOD); mul(one,one); add(one,MOD-minus); mul(one,inv[2]); ll two=0; for(int i=2;i<=N;i++) add(two,comb(N-2,i-2)*mod_pow(comb(N,i),MOD-2)%MOD); ll ans=0; { SegmentTree<int> seg(V,[](int a,int b){return a+b;},0); for(int i=0;i<N;i++){ ll num=seg.query(A[i]+1,V); ll res=1; mul(res,all*one%MOD); mul(res,num); add(ans,res); res=1; mul(res,all*two%MOD); mul(res,num); add(ans,res); int pval=seg.query(A[i],A[i]+1); seg.update(A[i],pval+1); } } { SegmentTree<int> seg(V,[](int a,int b){return a+b;},0); for(int i=N-1;i>=0;i--){ ll num=seg.query(A[i]+1,V); ll res=1; mul(res,all*one%MOD); mul(res,num); add(ans,res); int pval=seg.query(A[i],A[i]+1); seg.update(A[i],pval+1); } } cout<<ans<<endl; return 0; }