結果

問題 No.2105 Avoid MeX
ユーザー eQe
提出日時 2025-09-25 05:41:49
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 3,934 bytes
コンパイル時間 3,022 ms
コンパイル使用メモリ 285,916 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2025-09-25 05:41:54
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include<atcoder/modint>
#endif
using namespace std;
#define done(...) return pp(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);}
#define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{
#define use_ml998244353 using ml=atcoder::modint998244353;
namespace my{
auto&operator<<(ostream&o,const atcoder::modint998244353&x){return o<<(int)x.val();}
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
template<class T>constexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};}
void lin(auto&...a){(cin>>...>>a);}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace my{
template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>;
template<class V>constexpr int depth=0;
template<vectorial V>constexpr int depth<V> =depth<typename V::value_type>+1;
template<class T>struct core_t_helper{using type=T;};
template<vectorial V>struct core_t_helper<V>{using type=typename core_t_helper<typename V::value_type>::type;};
template<class T>using core_t=core_t_helper<T>::type;
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
  static constexpr int D=depth<V>+1;
  using C=core_t<V>;
  using vector<V>::vector;
  vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);}
  void resizes(const auto&...a){if constexpr(sizeof...(a)==D){ }else*this=make(a...);}
  static auto make(ll n,const auto&...a){
    if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
    else return vec<decltype(make(a...))>(n,make(a...));
  }
  ll size()const{return vector<V>::size();}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
}
namespace my{
template<class T>T inv(ll n){static vec<T>v{0,1};if(ll m=v.size();m<=n){v.resize(n+1);fo(i,m,n+1)v[i]=-v[T::mod()%i]*(T::mod()/i);}return v[n];}
}
namespace my{entry
void main(){
  LL(C,X);
  use_ml998244353
  if(X==0)done(inv<ml>(C+1));
  // https://yukicoder.me/problems/no/2105/editorial
  // 累積mexがXとならない
  // ⇔0,1,2,...,X-1が全て揃わないか,0,1,2,...,X-1が揃う前にXが出る
  // であるここで,
  // N→\inftyとすると,任意の数xについて,それが一度も現れない確率は0となる.
  // よって,この下で,
  // 累積mexがXとならない⇔0,1,2,...,X-1が揃う前にXが出る
  // である.この確率はXが0,1,2,...,XのX+1個の中でXが最後以外で出る確率で,1-1/(X+1).
  // X-C個目まではX未満しか出ず,それ以降はX以上が出る.
  // X-C個目までにX未満がj種類出る確率をq_jとすると,求める確率は
  // \sum_{j=0}^{X} q_j*(X-C個までに出ていないX-j種類の数とXの中で,Xが最後以外で出る確率)
  // \sum_{j=0}^{X} q_j*(1-1/(X-j+1))
  // である.
  if(X<=C)done(1-inv<ml>(X+1)); // 初めからXが出うるとき.

  ll M=X-C;
  vec dp(M+1,X+1,ml{}); // dp[i][j]:i個まででX未満がj種類出ている確率.
  dp[0][0]=1;
  fo(i,M){
    fo(j,X+1){
      dp[i+1][j]+=dp[i][j]*(j*inv<ml>(C+i+1));
      if(j+1<=X)dp[i+1][j+1]+=dp[i][j]*(1-j*inv<ml>(C+i+1));
    }
  }

  ml ans=0;
  fo(j,X+1)ans+=dp[M][j]*(1-inv<ml>(X-j+1));
  pp(ans);
}}
0