結果
問題 | No.2347 Swap!! |
ユーザー | ytqm3 |
提出日時 | 2023-06-04 18:57:37 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 18,760 bytes |
コンパイル時間 | 5,071 ms |
コンパイル使用メモリ | 289,676 KB |
実行使用メモリ | 27,444 KB |
最終ジャッジ日時 | 2024-06-09 04:04:19 |
合計ジャッジ時間 | 6,481 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
27,148 KB |
testcase_01 | AC | 45 ms
27,152 KB |
testcase_02 | AC | 46 ms
27,272 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 39 ms
26,792 KB |
testcase_11 | AC | 39 ms
26,804 KB |
ソースコード
#include <bits/stdc++.h> #if __has_include(<atcoder/all>) #include <atcoder/all> #endif namespace ttl{ using namespace std; using f80=long double; using i64=int64_t; using u64=uint64_t; template<int mod> struct mymodint{ i64 vl; static constexpr i64 get_mod(){ return mod; } i64 val(){ return vl; } mymodint(i64 vl_=0):vl((vl_%mod+mod)%mod){} mymodint operator-(){ return (vl==0)?0:mod-vl; } mymodint operator+(mymodint rhs){ return mymodint(*this)+=rhs; } mymodint operator-(mymodint rhs){ return mymodint(*this)-=rhs; } mymodint operator*(mymodint rhs){ return mymodint(*this)*=rhs; } mymodint operator/(mymodint rhs){ return mymodint(*this)/=rhs; } mymodint pow(i64 rhs){ mymodint res=1,now=(*this); while(rhs){ res*=((rhs&1)?now:1),now*=now,rhs>>=1; } return res; } mymodint& operator+=(mymodint rhs){ vl+=rhs.vl,vl-=((vl>=mod)?mod:0); return (*this); } mymodint& operator-=(mymodint rhs){ vl+=((vl<rhs.vl)?mod:0),vl-=rhs.vl; return (*this); } mymodint& operator*=(mymodint rhs){ vl=vl*rhs.vl%mod; return (*this); } mymodint& operator/=(mymodint rhs){ return (*this)*=rhs.pow(mod-2); } bool operator==(mymodint rhs){ return vl==rhs.vl; } bool operator!=(mymodint rhs){ return vl!=rhs.vl; } }; template<u64 mod> using modint= #if __has_include(<atcoder/all>) atcoder::static_modint<mod>; #else mymodint<mod>; #endif template<int mod> std::ostream& operator<<(std::ostream& os,modint<mod> x){ return os<<(x.val()); } template<int mod> std::istream& operator>>(std::istream& is,modint<mod>& x){ i64 t; is>>t,x=t; return is; } template<class T> void scn_(T& a){ cin>>a; } template<class T,class U> void scn_(pair<T,U>& a){ scn_(a.first),scn_(a.second); } template<class T> void scn_(vector<T>& a){ for(auto& v:a){ scn_(v); } } template<class T> void scn_(vector<vector<T>>& a){ for(auto& v:a){ for(auto& u:v){ cin>>u; } } } void scn(){} template<class T,class... Args> void scn(T& n,Args&... args){ scn_(n),scn(args...); } template<class T> void prt_(T a){ cout<<a; } template<class T,class U> void prt_(pair<T,U> a){ cout<<"{"<<a.first<<", "<<a.second<<"}"; } void prt_(f80 a){ printf("%.15Lf",a); } template<class T> void prt(vector<T> a){ for(size_t i=0;i<a.size();++i){ prt_(a[i]); cout<<" \n"[i==a.size()-1]; } } template<class T> void prt(vector<vector<T>> a){ for(auto& v:a){ for(size_t i=0;i<v.size();++i){ cout<<v[i]<<" \n"[i==v.size()-1]; } } } template<class T> void prt(T a){ prt_(a),cout<<"\n"; } template<class T,class... Args> void prt(T a,Args... args){ prt_(a),cout<<" ",prt(args...); } template<class Head,class... Tail> struct multi_dim_vector{ using type=vector<typename multi_dim_vector<Tail...>::type>; }; template<class T> struct multi_dim_vector<T>{ using type=T; }; template<class T,class Arg> vector<T> mvec(int n,Arg&& arg){ return vector<T>(n,arg); } template<class T,class... Args> class multi_dim_vector<Args...,T>::type mvec(int n,Args&&... args){ return typename multi_dim_vector<Args...,T>::type(n,mvec<T>(args...)); } template<class T> void rev(T& a){ reverse(a.begin(),a.end());} template<class T> void srt(T& a){ sort(a.begin(),a.end()); } template<class T> void rsrt(T& a){ sort(a.rbegin(),a.rend()); } template<class T> T revd(T a){ reverse(a.begin(),a.end()); return a; } template<class T> T srtd(T a){ sort(a.begin(),a.end()); return a; } template<class T> T rsrtd(T a){ sort(a.rbegin(),a.rend()); return a; } template<class T> T summ(vector<T> a){ return accumulate(a.begin(),a.end(),T(0)); } template<class T> T maxi(vector<T> a){ return *max_element(a.begin(),a.end()); } template<class T> T mini(vector<T> a){ return *min_element(a.begin(),a.end()); } template<class T> void chmx(T& a,T b){ a=max(a,b); } template<class T> void chmn(T& a,T b){ a=min(a,b); } i64 ppcnt(u64 k){ return __builtin_popcountll(k); } template<class T> T powe(T a,i64 n){ T res=1; while(n){ if(n&1){ res*=a; } a*=a; n/=2; } return res; } i64 powe(i64 a,i64 n,i64 m){ i64 res=1; while(n){ if(n&1){ res=res*a%m; } a=a*a%m; n/=2; } return res; } template<class T> void zip(vector<T>& a){ auto b=srtd(a); b.erase(unique(b.begin(),b.end()),b.end()); map<T,int> mp; for(int i=0;i<b.size();++i){ mp[b[i]]=i; } for(auto& v:a){ v=mp[v]; } } i64 sqrf(i64 n){ i64 ok=0,ng=1e9+5; while(std::abs(ok-ng)>1){ i64 mid=(ok+ng)/2; (mid*mid<=n?ok:ng)=mid; } return ok; } i64 sqrc(i64 n){ i64 ok=1e9+5,ng=0; while(std::abs(ok-ng)>1){ i64 mid=(ok+ng)/2; (mid*mid>=n?ok:ng)=mid; } return ok; } i64 dvf(i64 a,i64 b){ if(b<0){ a*=-1,b*=-1; } if(a<0){ return -(-a+b-1)/b; } return a/b; } i64 dvc(i64 a,i64 b){ if(b<0){ a*=-1,b*=-1; } if(a<0){ return -(-a)/b; } return (a+b-1)/b; } vector<int> bfs(vector<vector<int>> G,int v){ int N=G.size(); vector<int> dst(N,-1); queue<int> q; dst[v]=0; q.emplace(v); while(q.size()){ int t=q.front(); q.pop(); for(auto u:G[t]){ if(dst[u]==-1){ dst[u]=dst[t]+1; q.emplace(u); } } } return dst; } vector<i64> dijkstra(vector<vector<pair<int,i64>>>& G,int s){ int N=G.size(); vector<i64> dst(N,1e18); dst[s]=0; priority_queue<pair<i64,int>,vector<pair<i64,int>>,greater<pair<i64,int>>> pq; pq.emplace(0,s); vector<int> f(N); while(pq.size()){ auto [t,u]=pq.top(); pq.pop(); if(f[u]){ continue; } f[u]=1; for(auto [v,w]:G[u]){ if(dst[v]>dst[u]+w){ dst[v]=dst[u]+w; pq.emplace(dst[v],v); } } } return dst; } vector<pair<i64,i64>> pfct(i64 n){ vector<pair<i64,i64>> res; for(i64 i=2;i*i<=n;++i){ if(n%i!=0){ continue; } i64 ex=0; while(n%i==0){ ex++,n/=i; } res.emplace_back(i,ex); } if(n!=1){ res.emplace_back(n,1); } return res; } vector<i64> ediv(i64 n){ vector<i64> res; for(i64 i=1;i*i<=n;++i){ if(n%i!=0){ continue; } res.emplace_back(i); if(i*i!=n){ res.emplace_back(n/i); } } srt(res); return res; } template<class T> struct csm2d{ int n,m; vector<vector<T>> a,c; csm2d(vector<vector<T>> a_):n(a_.size()),m(a_[0].size()),a(a_){ auto b=mvec<T>(n,m+1,0); for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ b[i][j+1]=b[i][j]+a[i][j]; } } c=mvec<T>(n+1,m+1,0); for(int j=0;j<m+1;++j){ for(int i=0;i<n;++i){ c[i+1][j]=c[i][j]+b[i][j]; } } } T operator()(int p,int q,int r,int s){ return c[p][r]-c[p][s]-c[q][r]+c[q][s]; } }; template<class T> auto runlng(T a) -> vector<pair<typename decltype(a)::value_type,i64>>{ int n=a.size(); vector<pair<typename decltype(a)::value_type,i64>> res; if(n==0){ return res; } typename decltype(a)::value_type now=a[0]; i64 l=1; for(int i=1;i<n;++i){ if(a[i-1]==a[i]){ l++; } else{ res.emplace_back(now,l); now=a[i],l=1; } } res.emplace_back(now,l); return res; } template<class T> struct cmb{ vector<T> fac,ifac; cmb(int mx=3000000):fac(mx+1,1),ifac(mx+1,1){ for(int i=1;i<=mx;++i){ fac[i]=fac[i-1]*i; } ifac[mx]/=fac[mx]; for(int i=mx;i>0;--i){ ifac[i-1]=ifac[i]*i; } } T operator()(i64 n,i64 k){ if(n<0||k<0||n<k){ return 0; } return fac[n]*ifac[k]*ifac[n-k]; } T f(i64 n){ return n<0?T(0):fac[n]; } T fi(i64 n){ return n<0?T(0):ifac[n]; } }; } using namespace ttl; template<class mint> struct Poly{ vector<mint> val; Poly():val({0}){} Poly(mint t):val({t}){} Poly(int siz):val(max(1,siz)){} Poly(initializer_list<mint> init):val(init){} Poly(vector<mint> init):val(init){} mint &operator[](int i){ return val[i]; } int size(){ return val.size(); } void resize(int siz){ val.resize(siz); } void shrink(){ for(int i=val.size()-1;i>0;--i){ if(val[i]==0){ val.pop_back(); } else{ return; } } } Poly operator+(Poly rhs){ return Poly(*this)+=rhs; } Poly operator-(Poly rhs){ return Poly(*this)-=rhs; } Poly operator*(Poly rhs){ return Poly(*this)*=rhs; } Poly operator/(Poly rhs){ return Poly(*this)/=rhs; } Poly operator%(Poly rhs){ return Poly(*this)%=rhs; } Poly operator-(){ for(mint& v:val){ v=mint(0)-v; } return (*this); } Poly operator+=(Poly rhs){ resize(max(this->size(),rhs.size())); for(int i=0;i<int(rhs.size());++i){ (*this)[i]+=rhs[i]; } shrink(); return (*this); } Poly operator-=(Poly rhs){ resize(max(this->size(),rhs.size())); for(int i=0;i<int(rhs.size());++i){ (*this)[i]-=rhs[i]; } shrink(); return (*this); } Poly operator*=(Poly rhs){ val=atcoder::convolution(val,rhs.val); return (*this); } Poly operator/=(Poly rhs){ if(val.size()<rhs.size()){ val.resize(0); return (*this); } int rsiz=val.size()-rhs.size()+1; reverse(val.begin(),val.end()); reverse(rhs.val.begin(),rhs.val.end()); val.resize(rsiz),rhs.inv(rsiz); (*this)*=rhs; val.resize(rsiz); reverse(val.begin(),val.end()); return (*this); } Poly operator%=(Poly rhs){ if(val.size()<rhs.size()){ return (*this); } (*this)-=(*this)/rhs*rhs; val.resize(rhs.size()-1); shrink(); return (*this); } mint eval(mint a){ mint t=1,res=0; int n=(*this).size(); for(int i=0;i<n;++i){ res+=val[i]*t; t*=a; } return res; } void diffrent(){ Poly f(val.size()-1); for(int i=1;i<val.size();++i){ f[i-1]=val[i]*i; } (*this)=f; } void integral(){ Poly f(val.size()+1); for(int i=0;i<val.size();++i){ f[i+1]=val[i]/(i+1); } (*this)=f; } void inv(int mx){ Poly g({mint(1)/val[0]}); int now=1; while(now<mx){ now<<=1; Poly t=(*this); t.resize(now); t*=g,t=-t; t[0]+=2,g*=t; g.resize(now); } g.resize(mx); (*this)=g; } vector<mint> MultiEval(vector<mint> a){ int siz=1; while(siz<int(a.size())){ siz<<=1; } vector<Poly> t(siz*2-1,{1}); for(int i=0;i<int(a.size());++i){ t[i+siz-1]={-a[i],1}; } for(int i=siz-2;i>=0;--i){ t[i]=t[2*i+1]*t[2*i+2]; } vector<Poly> g(siz*2-1); g[0]=(*this)%t[0]; for(int i=1;i<siz*2-1;++i){ g[i]=g[(i-1)/2]%t[i]; } vector<mint> res(a.size()); for(int i=0;i<int(a.size());++i){ res[i]=g[i+siz-1][0]; } return res; } void TaylorShift(mint c){ cmb<mint> C(val.size()); vector<mint> g(val.size()); mint now=1; for(size_t i=0;i<val.size();++i){ val[i]*=C.dat[i]; g[i]=now*C.idat[i]; now*=c; } reverse(val.begin(),val.end()); g=atcoder::convolution(val,g); g.resize(val.size()); reverse(g.begin(),g.end()); for(size_t i=0;i<val.size();++i){ g[i]*=C.idat[i]; } val=g; } void LagrangeInterpolation(vector<mint> x,vector<mint> y){ int N=x.size(); vector<Poly> h(N); for(int i=0;i<N;++i){ h[i]={-x[i],1}; } auto g=Product(h); auto g_=g; g_.diffrent(); vector<mint> t=g_.MultiEval(x); for(int i=0;i<N;++i){ t[i]=y[i]/t[i]; } vector<Poly> den(2*N-1),num(2*N-1,{1}); for(int i=0;i<N;++i){ den[i+N-1]={-x[i],1}; num[i+N-1]={t[i]}; } for(int i=N-2;i>=0;--i){ den[i]=den[2*i+1]*den[2*i+2]; num[i]=num[2*i+1]*den[2*i+2]+num[2*i+2]*den[2*i+1]; } (*this)=num[0]; } }; template<class mint> struct FPS{ vector<mint> val; FPS():val({0}){} FPS(mint t):val({t}){} FPS(int siz):val(max(1,siz)){} FPS(initializer_list<mint> init):val(init){} FPS(vector<mint> init):val(init){} mint &operator[](int i){ return val[i]; } int size(){ return val.size(); } void resize(int siz){ val.resize(siz); } FPS& resize_(int siz){ auto tmp=val; tmp.resize(siz); return (*this)=tmp; } FPS operator-(){ for(mint& v:val){ v=mint(0)-v; } return (*this); } FPS& operator+=(mint rhs){ val[0]+=rhs; return (*this); } FPS& operator-=(mint rhs){ val[0]-=rhs; return (*this); } FPS& operator*=(mint rhs){ for(auto& v:val){ v*=rhs; } return (*this); } FPS& operator/=(mint rhs){ for(auto& v:val){ v/=rhs; } return (*this); } FPS operator+(mint rhs){ return FPS(*this)+=rhs; } FPS operator-(mint rhs){ return FPS(*this)-=rhs; } FPS operator*(mint rhs){ return FPS(*this)*=rhs; } FPS operator/(mint rhs){ return FPS(*this)/=rhs; } FPS& operator+=(FPS rhs){ resize(max(this->size(),rhs.size())); for(size_t i=0;i<rhs.size();++i){ (*this)[i]+=rhs[i]; } return (*this); } FPS& operator-=(FPS rhs){ resize(max(this->size(),rhs.size())); for(size_t i=0;i<rhs.size();++i){ (*this)[i]-=rhs[i]; } return (*this); } FPS& operator*=(FPS rhs){ val=atcoder::convolution(val,rhs.val); return (*this); } FPS& operator/=(FPS rhs){ return (*this)*=rhs.inv(); } FPS& operator>>=(int k){ if(int(val.size())<=k){ return (*this)={0}; } FPS res=val; res.val.erase(res.val.begin(),res.val.begin()+k); return (*this)=res; } FPS& operator<<=(int k){ FPS res=val; res.val.insert(res.val.begin(),k,mint(0)); return (*this)=res; } FPS operator+(FPS rhs){ return FPS(*this)+=rhs; } FPS operator-(FPS rhs){ return FPS(*this)-=rhs; } FPS operator*(FPS rhs){ return FPS(*this)*=rhs; } FPS operator/(FPS rhs){ return FPS(*this)/=rhs; } FPS operator%(FPS rhs){ return FPS(*this)%=rhs; } FPS operator<<(int k){ return FPS(*this)<<=k; } FPS operator>>(int k){ return FPS(*this)>>=k; } FPS shrink(){ for(int i=val.size()-1;i>0;--i){ if(val[i]==0){ val.pop_back(); } else{ break; } } return (*this); } FPS diff_(){ if(val.size()==1){ return (*this)={0}; } FPS f(val.size()-1); for(size_t i=1;i<val.size();++i){ f[i-1]=val[i]*i; } return f; } FPS integral_(){ FPS f(val.size()+1); for(size_t i=0;i<val.size();++i){ f[i+1]=val[i]/(i+1); } return f; } FPS inv_(i64 mx=-1){ if(mx==-1){ mx=val.size(); } if(val[0]==0){ assert(0); } FPS g({mint(1)/val[0]}); i64 now=1; while(now<mx){ now<<=1; FPS t=(*this); t.resize(now); t*=g; t=-t+mint(2); g*=t; g.resize(now); } g.resize(mx); return g; } FPS exp_(i64 mx=-1){ if(mx==-1){ mx=val.size(); } if(val[0]!=0){ assert(0); } FPS g(mint(1)); i64 now=1; while(now<mx){ now<<=1; FPS t=(*this); t.resize(now); g*=t-g.log_(now)+mint(1); g.resize(now); } g.resize(mx); return g; } FPS log_(i64 mx=-1){ if(mx==-1){ mx=val.size(); } if(val[0]!=1){ assert(0); } auto f=(*this); f.resize(mx); return (f.diff_()/f).integral_().resize_(mx); } FPS pow_(i64 k,int mx=-1){ if(mx==-1){ mx=val.size(); } i64 t=0; for(auto v:val){ if(v==0){ t++; } else{ break; } } if(t==val.size()){ FPS res(mx); if(k==0){ res[0]=1; } return res; } auto f=(*this)>>t; if(f[0]==0){ f={0},f.resize(mx); return f; } mint c=f[0]; f/=c; (f.log(mx)*=mint(k)).exp(mx)*=(c.pow(k)); if(k==0?true:(t<=mx/k)){ f<<=t*k; f.resize(mx); return f; } else{ f={0},f.resize(mx); return f; } } FPS& diff(){ return (*this)=diff_(); } FPS& integral(){ return (*this)=integral_(); } FPS& inv(i64 mx=-1){ return (*this)=inv_(mx); } FPS& exp(i64 mx=-1){ return (*this)=exp_(mx); } FPS& log(i64 mx=-1){ return (*this)=log_(mx); } FPS& pow(i64 k,i64 mx=-1){ return (*this)=pow_(k,mx); } }; template<typename T> T Product(vector<T> a){ int siz=1; while(siz<int(a.size())){ siz<<=1; } vector<T> res(siz*2-1,{1}); for(size_t i=0;i<a.size();++i){ res[i+siz-1]=a[i]; } for(int i=siz-2;i>=0;--i){ res[i]=res[2*i+1]*res[2*i+2]; } return res[0]; } template<class mint> vector<mint> StirlingNumber2(int N){ FPS<mint> f(N+1),g(N+1); mint fact=1; for(int i=0;i<=N;++i){ f[i]=(mint(i).pow(N))/fact; g[i]=(mint(-1).pow(i))/fact; fact*=i+1; } f*=g; vector<mint> res(N+1); for(int i=0;i<=N;++i){ res[i]=f[i]; } return res; } template<class T> T Even(T f){ T g((f.size()+1)/2); for(size_t i=0;i<(f.size()+1)/2;++i){ g[i]=f[i*2]; } return g; } template<class T> T Odd(T f){ T g(f.size()/2); for(size_t i=0;i<f.size()/2;++i){ g[i]=f[i*2+1]; } return g; } template<class T> T Minus(T f){ for(size_t i=1;i<f.size();i+=2){ f[i]*=-1; } return f; } template<class mint> mint BostanMori(FPS<mint> f,FPS<mint> g,i64 k){ while(k>0){ if(k%2==0){ f=Even(f*Minus(g)); } else{ f=Odd(f*Minus(g)); } g=Even(g*Minus(g)); k/=2; } return f[0]/g[0]; } template<class mint> vector<mint> SubsetSum(int N,vector<int> A,int T){ vector<mint> C(T+1); for(int i=0;i<N;++i){ if(A[i]<=T){ C[A[i]]+=1; } } FPS<mint> f(T+1); for(int k=1;k<=T;++k){ for(int i=1;i*k<=T;++i){ f[i*k]-=C[k]/i*(i%2?-1:1); } } f.exp(); vector<mint> res(T+1); for(int i=0;i<=T;++i){ res[i]=f[i]; } return res; } template<class mint> vector<mint> Montmort(int N){ vector<mint> res(N+1); res[0]=1,res[1]=0; for(int n=2;n<=N;++n){ res[n]=(res[n-2]+res[n-1])*(n-1); } return res; } int sol(vector<int> A,vector<int> B){ int N=A.size(); vector<vector<int>> C(N); for(int i=0;i<N;++i){ C[A[i]].emplace_back(i); } for(int i=0;i<N;++i){ rev(C[i]); } vector<int> P(N); for(int i=0;i<N;++i){ P[i]=C[B[i]].back(); C[B[i]].pop_back(); } int res=0; for(int i=0;i<N;++i){ for(int j=i+1;j<N;++j){ res+=(P[i]>P[j]); } } return res; } int main(){ constexpr int mod=998244353; using mint=modint<mod>; cmb<mint> Cb; int N,M; scn(N,M); if(M==1){ cout<<0<<endl; return 0; } vector<mint> f(1<<17); for(int i=0;i<=N;++i){ f[i]=Cb.fi(i)*Cb.fi(i); } atcoder::internal::butterfly(f); for(auto& v:f){ v=v.pow(M-2); } atcoder::internal::butterfly_inv(f); mint I=mint(1)/(1<<17); for(auto& v:f){ v*=I; } mint ans=0; for(int n=2;n<=N;++n){ ans+=Cb(M,2)*Cb(N,n)*Cb(N,n)*Cb.f(N-n)*Cb.f(N-n)*f[N-n]*mint(2).pow(2*n-3)*(n-1); } prt(ans); }