Live2D

Solution -「51nod 1514」美妙的序列

\(\mathcal{Description}\)

  Link.

  称排列 \(\{p_n\}\) 美妙,当且仅当 \((\forall i\in[1,n))(\max_{j\in[1,i]}\{p_i\}>\min_{j\in(i,n]}\{p_j\})\)。求长度为 \(n\) 的美妙排列个数。多测。

  \(n\le10^5\)

\(\mathcal{Solution}\)

  讨论这道题的时候——打表,然后发现了 A003319!/xyx

  显然 \(f(0)=0,f(1)=1\),然后 A003319 给出了长度为 \(n\) 的美妙排列个数 \(f(n)\) 的递推式:

\[f(n)=n!-\sum_{i=1}^{n-1}i!f(n-i) \]

  先证明这个递推。等式相当于用所有方案 \(n!\) 减去了所有不美妙的序列方案并保证其不重复。考虑当求和的 \(i\) 等于某个数 \(k\) 时,构造序列:

\[\overbrace{p_1~~~~p_2~~~~\cdots~~~~p_k}^{\text{a permutation from 1 to k}}~~~~\overbrace{p_{k+1}~~~~p_{k+2}~~~~\cdots~~~~p_n}^{\text{a permutation from k+1 to n}} \]

  其中,后一个排列由合法的 \(f(n-k)\) 整体 \(+k\) 形成,显然它是合法的。但当分隔点在 \(k\) 时,前缀最大为 \(k\),后缀最小为 \(k+1\),可见整个排列不合法。这样计算是不会算重的——非法排列仅会在分隔点在 \(k\) 处时被算一次,否则将任意一个数 \(t\in[1,k]\) 加入后面合法的排列,都会使排列不合法,不满足 \(f(n-k)\) 的定义。

  接下来着手计算。移项:

\[\sum_{i=0}^{n}i!f(n-i)=n! \]

  那么 \(f\)\(\text{OGF}\) 满足:

\[1+F(x)P(x)=P(x) \]

  \(+1\) 是因为左式的 \(f_0\) 被定义为 \(0\),而 \(0!=1\),所以常数项 \(+1\)。最后移项得到 \(F(x)\) 的表达式:

\[F(x)=1-P^{-1}(x) \]

  多项式求逆算出 \(F\) 即可。复杂度 \(\mathcal O(n\log n)-\mathcal O(1)\)

\(\mathcal{Code}\)

#include <cmath>
#include <cstdio>

const int MAXN = 1 << 18, MOD = 998244353;
int fac[MAXN + 5], F[MAXN + 5];

inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int sub ( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int mul ( long long a, const int b ) { return ( a *= b ) < MOD ? a : a % MOD; }

inline int qkpow ( int a, int b, const int p = MOD ) {
	int ret = 1;
	for ( ; b; a = 1ll * a * a % p, b >>= 1 ) ret = 1ll * ret * ( b & 1 ? a : 1 ) % p;
	return ret;
}

namespace Poly {

const int G = 3;

inline int adjust ( const int n ) {
	int ret = 0;
	for ( int l = 1; l < n; l <<= 1, ++ ret );
	return ret;
}

inline void NTT ( const int n, int* A, const int tp ) {
	static int lstn = -1, rev[MAXN + 5] {};
	if ( lstn ^ n ) {
		int lgn = log ( n ) / log ( 2 ) + 0.5;
		for ( int i = 0; i < n; ++ i ) rev[i] = ( rev[i >> 1] >> 1 ) | ( ( i & 1 ) << lgn >> 1 );
		lstn = n;
	}
	for ( int i = 0; i < n; ++ i ) if ( i < rev[i] ) A[i] ^= A[rev[i]] ^= A[i] ^= A[rev[i]];
	for ( int i = 2, stp = 1; i <= n; i <<= 1, stp <<= 1 ) {
		int w = qkpow ( G, ( MOD - 1 ) / i );
		if ( ! ~ tp ) w = qkpow ( w, MOD - 2 );
		for ( int j = 0; j < n; j += i ) {
			for ( int k = j, r = 1; k < j + stp; ++ k, r = mul ( r, w ) ) {
				int ev = A[k], ov = mul ( r, A[k + stp] );
				A[k] = add ( ev, ov ), A[k + stp] = sub ( ev, ov );
			}
		}
	}
	if ( ! ~ tp ) {
		int invn = qkpow ( n, MOD - 2 );
		for ( int i = 0; i < n; ++ i ) A[i] = mul ( A[i], invn );
	}
}

inline void polyInv ( const int n, const int* A, int* R ) {
	static int tmp[MAXN + 5] {};
	if ( n == 1 ) return void ( R[0] = qkpow ( A[0], MOD - 2 ) );
	int len = 1 << adjust ( n << 1 );
	polyInv ( n + 1 >> 1, A, R );
	for ( int i = 0; i < n; ++ i ) tmp[i] = A[i];
	NTT ( len, tmp, 1 ), NTT ( len, R, 1 );
	for ( int i = 0; i < len; ++ i ) R[i] = mul ( sub ( 2, mul ( tmp[i], R[i] ) ), R[i] ), tmp[i] = 0;
	NTT ( len, R, -1 );
	for ( int i = n; i < len; ++ i ) R[i] = 0;
}

} // namespace Poly.

int main () {
	int T, n = 1e5;
	fac[0] = 1;
	for ( int i = 1; i <= n; ++ i ) fac[i] = mul ( fac[i - 1], i );
	Poly::polyInv ( n + 1, fac, F );
	F[0] = ( 1 - F[0] + MOD ) % MOD;
	for ( int i = 1; i <= n; ++ i ) F[i] = ( MOD - F[i] ) % MOD;
	for ( scanf ( "%d", &T ); T --; ) {
		scanf ( "%d", &n );
		printf ( "%d\n", F[n] );
	}
	return 0;
}
posted @ 2020-08-10 21:59  Rainybunny  阅读(144)  评论(0编辑  收藏  举报