题解:AT_abc473_g [ABC473G] Wipeout

· · 题解

单老哥做法。

很难绷吧。

以下内容,小写字母表示题目中的大写对应字母。

由于我们不知道排列,所以寻找顺序无关。于是以如下策略寻找:

我们寻找 x 时,已知集合 S:

我们怎么统计有多少个排列符合条件呢?

容易发现,假设为了寻找 x,从 l 一直找到了 r,且 p_r=x。将 [l,r] 看做段。

设这样段数为 c。容易发现答案其实就是 2n-c。

我们设 f(l,c) 表示考虑长度为 l 的一个排列,段数为 c 的数量。

使用排列 DP 经典套路,我们让这里的排列等价于实际整个排列的一部分取相对大小后的结果。

考虑转移。

假设在第一个位置取:

所以 f(l,c)=f(l-1,c-1)+(l-1)f(l-1,c)。

\gdef \so { \implies } \gdef \P { \mathbb{P} } \gdef \id { \mathrm{id} } \gdef \lcm { \mathrm{lcm} } \gdef \or { \mathbin{ \hbox{ \rm or } } } \gdef \xor { \mathbin{ \hbox{ \rm xor } } } \gdef \and { \mathbin{ \hbox{ \rm and } } } \gdef \inv #1{ { #1 ^ {-1} } } \gdef \abs #1{ \left \lvert #1 \right \rvert } \gdef \paren #1{ \left \lparen #1 \right \rparen } \gdef \brack [#1]{ \left \lbrack #1 \right \rbrack } \gdef \brace #1{ \left \lbrace #1 \right \rbrace } \gdef \ceil #1{ \left \lceil #1 \right \rceil } \gdef \floor #1{ \left \lfloor #1 \right \rfloor } \gdef \Vert #1{ \left \lVert #1 \right \rVert } \gdef \vector #1#2{ \begin{bmatrix} #1 \\ #2 \end{bmatrix} } \gdef \stiro #1#2{ \begin{bmatrix} #1 \\ #2 \end{bmatrix} } \gdef \stirt #1#2{ \begin{Bmatrix} #1 \\ #2 \end{Bmatrix} } \gdef \zc #1#2{\lfloor\frac{#1}{#2}\rfloor} \gdef \dzc #1#2{\lfloor\dfrac{#1}{#2}\rfloor} \gdef \eps {\varepsilon} \gdef \ph {\varphi} \gdef \ba {\begin{aligned}} \gdef \ea {\end{aligned}} \gdef \lcm {\mathrm{lcm}} \gdef \d {\cdot} \gdef \etc {\cdots} \gdef \Etc {\cdots\cdots} \gdef \O {\mathcal{O}}

这就是第一类斯特林数!

好的,我们用通项公式……额,没有?! 考虑直接使用[求一列斯特林数的做法](https://oi-wiki.org/math/combinatorics/stirling/#%E5%90%8C%E4%B8%80%E5%88%97%E7%AC%AC%E4%B8%80%E7%B1%BB%E6%96%AF%E7%89%B9%E6%9E%97%E6%95%B0%E7%9A%84%E8%AE%A1%E7%AE%97)。这样就是 $O(n\log n)$。 *** ?我被卡常了! 好吧,是我实现太【数据删除】了。 稍微优化可以通过。 *** 求斯特林数的部分 oiwiki 有。求完了就输出 $\dfrac{\stiro n{2n-k}}{n!}$。 :::info[我的【数据删除】多项式] ```cpp #define int long long namespace Poly_NTT{ constexpr int maxn=2.1e6+10,mod=998244353,g=3,inv_g=332748118,inv_2=499122177; using poly=vector<int>; poly one=poly(1,1); int qpow(int a,int b,int Mod){ if(a==1||b==0)return 1; if(b==1)return a; int h=qpow(a,b>>1,Mod); return h*h%Mod*(b&1?a:1)%Mod; } int inv(int x){return qpow(x,mod-2,mod);} inline void NTT(poly &a,int n,int d){ int s=n,t=1; vector<int> rev(n); rev[0]=0; for(int L=1;L<=(n>>1);L<<=1){//记 k 为递归层数,则分成奇偶两组的过程中,右侧奇组元素为偶组相应元素加上 2^{k-1} s>>=1;//长度 L 从 1 开始增加,2^{递归层数} 就/2 for(int i=0;i<L;i++){//i是左边(偶) rev[t]=rev[i]+s;//t:右边的点(奇) if(t<rev[t])swap(a[t],a[rev[t]]); t++; } } for(int L=2;L<=n;L<<=1){ int Wn=qpow((d==1?g:inv_g),(mod-1)/L,mod); for(int i=0;i<n;i+=L){ int Wk=1; for(int j=i;j<i+(L>>1);j++){ int u=a[j],v=a[j+(L>>1)]*Wk%mod; a[j]=(u+v)%mod; a[j+(L>>1)]=((u-v)+mod)%mod; Wk=Wk*Wn%mod; } } } } inline poly operator+(poly f,poly g){ int n=max(f.size(),g.size()); f.resize(n),g.resize(n); for(int i=0;i<n;i++){ f[i]+=g[i]; f[i]%=mod; } return f; } inline poly operator-(poly f,poly g){ int n=max(f.size(),g.size()); f.resize(n),g.resize(n); for(int i=0;i<n;i++){ f[i]-=g[i]; f[i]=(f[i]+mod)%mod; } return f; } inline poly operator%(poly f,int x){ f.resize(x,0); return f; } inline poly operator*(poly f,poly g){ int n=f.size()-1,m=g.size()-1,N=1,inv_N; poly h; while(N<n+m+1)N<<=1; f.resize(N),g.resize(N); NTT(f,N,1); NTT(g,N,1); for(int i=0;i<N;i++)f[i]=f[i]*g[i]%mod; NTT(f,N,-1); inv_N=qpow(N,mod-2,mod); for(int i=0;i<=n+m;i++)h.push_back((f[i]*inv_N)%mod); return h; } inline poly multi(poly f,poly g){ int n=f.size()-1,m=g.size()-1,N=1,inv_N; poly h; while(N<n+m*2+1)N<<=1; f.resize(N),g.resize(N); NTT(f,N,1); NTT(g,N,1); for(int i=0;i<N;i++)f[i]=f[i]*g[i]%mod*g[i]%mod; NTT(f,N,-1); inv_N=qpow(N,mod-2,mod); for(int i=0;i<=n+m*2;i++)h.push_back((f[i]*inv_N)%mod); return h; } inline poly operator*(poly f,int x){ for(auto &i:f){i*=x,i%=mod;} return f; } inline poly inv(poly f){ int n=f.size(); poly g(1); g[0]=inv(f[0]); int L=1; while(L<n){ L<<=1; g=(g+g-multi(f%L,g)%L)%L; } return g%n; } inline poly inv_mod(poly f,int len){ int n=f.size(); poly g(1); g[0]=inv(f[0]); int L=1; while(L<n){ L<<=1; g=(g+g-multi(f%L,g)%L)%min(L,len); } return g%n; } inline poly diff(poly f){//求导 poly g(f.size()-1,0); for(int i=1;i<f.size();i++){ g[i-1]=i*f[i]%mod; } return g; } inline poly integ(poly f){//积分 poly g(f.size()+1,0); for(int i=1;i<f.size();i++)g[i]=qpow(i,mod-2,mod)*f[i-1]%mod; return g; } inline poly ln(poly f){ int n=f.size(); return integ(inv_mod(f,n)*diff(f)%n)%n; } inline poly exp(poly f){ int n=f.size(); poly g(1); g[0]=1; int L=1; while(L<n){ L<<=1; g=g*((one-ln(g)+f%L)%L); g=g%(L<<1); } return g%n; } inline poly operator>>(poly f,int k){ int n=f.size(); poly g(n-k); for(int i=k;i<f.size();i++)g[i-k]=f[i]; return g; } inline poly operator<<(poly f,int k){ int n=f.size(); poly g(n+k,0); for(int i=0;i<f.size();i++)g[i+k]=f[i]; return g; } } using namespace Poly_NTT; ``` :::