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


    (mathcal{Description})

      Link.

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

      (nle10^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}^{ ext{a permutation from 1 to k}}~~~~overbrace{p_{k+1}~~~~p_{k+2}~~~~cdots~~~~p_n}^{ ext{a permutation from k+1 to n}} ]

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

      接下来着手计算。移项:

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

      那么 (f)( ext{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(nlog 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
    ", F[n] );
    	}
    	return 0;
    }
    
  • 相关阅读:
    处理i18n国际电话区号的代码实践
    iOS 万能跳转界面方法 (runtime实用篇一)
    URI跳转方式地图导航的代码实践
    iOS @IBDesignable和@IBInspectable
    我的iOS高效编程秘诀—坚持编程习惯
    如何手动解析CrashLog
    小心别让圆角成了你列表的帧数杀手
    iOS开发之使用Runtime给Model类赋值
    POP介绍与使用实践(快速上手动画)
    NSString&NSMutableString常用操作梳理
  • 原文地址:https://www.cnblogs.com/rainybunny/p/13472432.html
Copyright © 2020-2023  润新知