結果
問題 | No.2904 Distinct Multisets in a Way |
ユーザー |
|
提出日時 | 2024-09-27 20:35:38 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 6,448 bytes |
コンパイル時間 | 6,510 ms |
コンパイル使用メモリ | 329,360 KB |
実行使用メモリ | 39,168 KB |
最終ジャッジ日時 | 2024-09-27 20:35:48 |
合計ジャッジ時間 | 9,245 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 TLE * 1 |
other | -- * 42 |
ソースコード
#include<bits/stdc++.h>using namespace std;#include<atcoder/all>using namespace atcoder;using mint=atcoder::modint998244353;#define int long longtemplate<class mint>struct FormalPowerSeries:vector<mint>{using vector<mint>::vector;using vector<mint>::operator=;using fps=FormalPowerSeries;using sfps=vector<pair<int,mint>>;fps& operator+=(const fps& g){if(g.size()>this->size())this->resize(g.size());for(int i=0;i<(int)g.size();i++)(*this)[i]+=g[i];return *this;}fps& operator+=(const mint& v){if(this->empty())this->resize(1);(*this)[0]+=v;return *this;}fps operator+(const fps& g)const{return fps(*this)+=g;}fps operator+(const mint& v)const{return fps(*this)+=v;}friend fps operator+(const mint& v,const fps& f){return f+v;}fps& operator+=(const int& v){*this+=mint(v);return *this;}fps operator+(const int& v){return fps(*this)+=v;;}friend fps operator+(const int& v,const fps& f){return f+v;}fps& operator-=(const fps& g){if(g.size()>this->size())this->resize(g.size());for(int i=0;i<(int)g.size();i++)(*this)[i]-=g[i];return *this;}fps& operator-=(const mint& v){if(this->empty())this->resize(1);(*this)[0]-=v;return *this;}fps operator-(const fps& g)const{return fps(*this)-=g;}fps operator-(const mint& v)const{return fps(*this)-=v;}friend fps operator-(const mint& v,const fps& f){return -(f-v);}fps& operator-=(const int& v){*this-=v;return *this;}fps operator-(const int& v){return fps(*this)-=v;}friend fps operator-(const int& v,const fps& f){return -(f-v);}fps operator-()const{return fps(*this)*=-1;}fps& operator*=(const mint& v){for(auto&e:*this)e*=v;return *this;}fps operator*(const mint& v)const{return fps(*this)*=v;}friend fps operator*(const mint& v,const fps& f){return f*v;}fps& operator*=(const int& v){*this*=mint(v);return *this;}fps operator*(const int& v)const{return fps(*this)*=v;}friend fps operator*(const int&v,const fps& f){return f*v;}fps& operator<<=(const int d){this->insert(this->begin(),d,0);return *this;}fps operator<<(const int d)const{return fps(*this)<<=d;}fps& operator>>=(const int d){this->erase(this->begin(),this->begin()+min((int)this->size(),d));return *this;}fps operator>>(const int d)const{return fps(*this)>>=d;}//fastfps& operator*=(const fps& g){*this=atcoder::convolution(*this,g);return *this;}//naive// fps& operator*=(const fps& g){// this->resize(this->size()+g.size()-1);// for(int i=(int)this->size()-1;i>=0;i--){// for(int j=1;j<=(int)g.size();j++){// if(i+j>=(int)this->size())break;// (*this)[i+j]+=(*this)[i]*g[j];// }// (*this)[i]*=g[0];// }// return *this;// }fps operator*(const fps& g)const{return fps(*this)*=g;}fps inv(int d)const{fps g={(*this)[0].inv()};for(int k=1;k<d;k*=2){g=(2-*this*g)*g;g.resize(2*k);}g.resize(d+1);return g;}fps& operator/=(const fps& g){return *this=fps(*this*=g.inv(this->size())).pre(this->size());}fps& operator/=(const mint& v){for(auto&e:*this)e/=v;return *this;}fps operator/(const fps& g)const{return fps(*this)/=g;}fps operator/(const mint& v)const{return fps(*this)/=v;}fps quotient(const fps& g)const{if(this->size()<g.size())return fps();return (fps(this->rev()/g.rev()).pre(this->size()-g.size()+1)).rev();}fps reminder(const fps& g)const{return fps(*this-this->quotient(g)*g).pre(g.size()-1);}pair<fps,fps> quotient_reminder(const fps& g)const{pair<fps,fps> res;res.first=this->quotient(g);res.second=fps(*this-res.first*g).pre(g.size()-1);return res;}void shrink(){while(this->size()&&this->back()==mint(0))this->pop_back();}fps rev()const{fps g(*this);reverse(g.begin(),g.end());return g;}fps dot(fps g)const{fps res(min(this->size(),g.size()));for(int i=0;i<(int)res.size();i++)res[i]=(*this)[i]*g[i];return res;}fps pre(int d)const{fps res(begin(*this),begin(*this)+min((int)this->size(),d));if((int)res.size()<d)res.resize(d);return res;}fps& operator*=(const sfps& g){auto it0=g.begin();mint g0=0;if(it0->first==0){g0=it0->second;it0++;}for(int i=this->size()-1;i>=0;i--){for(auto it=it0;it!=g.end();it++){auto[j,gj]=*it;if(i+j>=this->size())break;(*this)[i+j]+=(*this)[i]*gj;}(*this)[i]*=g0;}return *this;}fps operator*(const sfps& g)const{return fps(*this)*=g;}fps& operator/=(const sfps& g){auto it0=g.begin();assert(it0->first==0&&it0->second!=0);mint g0_inv=it0->second.inv();it0++;for(int i=0;i<(int)this->size();i++){(*this)[i]*=g0_inv;for(auto it=it0;it!=g.begin();it++){auto[j,gj]=*it;if(i+j>=this->size())break;(*this)[i+j]-=(*this)[i]*gj;}}return *this;}fps operator/(const sfps& g)const{return fps(*this)/=g;}fps pow(long long d,const fps& g)const{fps res={1},pow2=*this;while(d>0){if(d&1)res=(res*pow2).reminder(g);pow2=(pow2*pow2).reminder(g);d>>=1;}return res;}fps derivative()const{fps res;for(int i=1;i<(int)this->size();i++)res.push_back((*this)[i]*i);return res;}fps integral()const{fps res={0};for(int i=0;i<(int)this->size();i++)res.push_back((*this)[i]/(i+1));return res;}fps log(int d)const{return fps(this->derivative()*this->inv(d)).integral().pre(d);}fps exp(int d)const{fps g={1};for(int k=1;k<d;k*=2){g=g*(*this+1-g.log(2*k));g.resize(2*k);}return g.pre(d);}fps pow(long long k,int d)const{if(k==0){fps res(d,mint(0));if(d)res[0]=1;return res;}int i0=0;while(i0<(int)this->size()&&(*this)[i0]==mint(0))i0++;if(i0==(int)this->size())return fps(d,mint(0));mint c0=(*this)[i0];fps fs=(*this>>i0)/c0;if(i0>=(d+k-1)/k)return fps(d,mint(0));int ds=(int)(d-k*i0);fps gs=fps(mint(k)*fs.log(ds)).exp(ds);fps g=fps(gs*c0.pow(k))<<(int)(k*i0);return g;}friend istream& operator>>(istream& is,fps&f){for(auto&e:f)cin>>e;return is;}friend ostream& operator<<(ostream& os,const fps& f){if((int)f.size()==0)os<<0;else{for(int i=0;i<(int)f.size();i++){os<<f[i].val();if(i<(int)f.size()-1)os<<" ";}return os;}return os;}};using fps=FormalPowerSeries<mint>;using sfps=vector<pair<int,mint>>;signed main(){int n;cin>>n;fps f={1,1,1};f=f.pow(n,n+1);cout<<((f[n]-1)/2).val()<<endl;}