結果

問題 No.2149 Vanitas Vanitatum
ユーザー lgswdn
提出日時 2023-11-30 16:42:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,608 bytes
コンパイル時間 2,021 ms
コンパイル使用メモリ 198,396 KB
最終ジャッジ日時 2025-02-18 02:30:53
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

//vanitas vanitatum et omnia vanitas
#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
  return x*w;
}

const int N=1e6+5,mod=998244353;
int n,a[N],m,fac[N],ifac[N],b[N],c[N],d[N],m1,m2,inv[N];

int ksm(int x,int y,int r=1) {
  for(;y;y>>=1,x=x*x%mod) if(y&1) r=r*x%mod;
  return r;
}
void pre(int n) {
  fac[0]=1; rep(i,1,n) fac[i]=fac[i-1]*i%mod;
  ifac[n]=ksm(fac[n],mod-2), inv[0]=1;
  per(i,n-1,0) ifac[i]=ifac[i+1]*(i+1)%mod;
  rep(i,1,n) inv[i]=ifac[i]*fac[i-1]%mod;
}
int C(int n,int m) {
  if(n<m||n<0||m<0) return 0;
  else return fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
pii work(int *c,int m) {
  n=0; int cur=0;
  rep(i,1,m) {
    if(c[i]==0) ++n, a[n]=cur;
    else cur++;
  }
  int sum=0; rep(i,1,n) sum+=a[i];
  int ans=fac[sum];
  rep(i,1,n) {
    int t=1;
    rep(j,1,a[i]) {
      while(a[t]<j) ++t;
      int h=a[i]-j+1+i-t;
      ans=ans*inv[h]%mod;
    }
  }
  return pii(sum,ans);
}

signed main() {
  n=read(); rep(i,1,n) a[i]=read();
  rep(i,1,n) {
    rep(j,a[i-1]+1,a[i]) b[++m]=1;
    b[++m]=0;
  }
  int sum=0; rep(i,1,n) sum+=a[i];
  pre(sum);
  for(int i=1;i<=m;i+=2) c[++m1]=b[i];
  for(int i=2;i<=m;i+=2) d[++m2]=b[i];
  pii p=work(c,m1), q=work(d,m2);
  sort(c+1,c+m1+1), sort(d+1,d+m2+1);
  rep(i,1,m) {
    if(i&1) b[i]=c[(i+1)/2];
    else b[i]=d[i/2];
  }
  rep(i,1,m) if(b[i]==0&&b[i-1]==1) return puts("0"), 0;
  printf("%lld\n",p.se*q.se%mod*C(p.fi+q.fi,p.fi)%mod);
  return 0;
}
0