Live2D

Solution -「CF 1342E」Placing Rooks

\(\mathcal{Description}\)

  Link.

  在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足:

  • 所有格子都可以被攻击到。
  • 恰好存在 \(k\) 对车可以互相攻击。

  的摆放方案数,对 \(998244353\) 取模。

  \(n\le2\times10^5\)

\(\mathcal{Solution}\)

  这道《蓝题》嗷,看来兔是个傻子。

  从第一个条件入手,所有格子可被攻击,那就有「每行都有车」或「每列都有车」成立。不妨设每行有车,则第二个条件中的“互相攻击”仅能由同列的车满足,可以得出有车的列数为 \(n-k\)

  \(n\) 个不同行棋子放入 \(n-k\) 个不同列,方案数:

\[A_n^{n-k}{n \brace n-k} \]

  若 \(k\not=0\),明显沿对角线对称摆放所有棋子得到新方案,故答案 \(\times2\)

  复杂度 \(\mathcal O(n)\)

\(\mathcal{Code}\)

/* Clearink */

#include <cstdio>

const int MAXN = 2e5, MOD = 998244353;
int n, m, fac[MAXN + 5], ifac[MAXN + 5];

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

inline int qkpow ( int a, int b ) {
	int ret = 1;
	for ( ; b; a = mul ( a, a ), b >>= 1 ) ret = mul ( ret, b & 1 ? a : 1 );
	return ret;
}

inline void init () {
	fac[0] = 1;
	for ( int i = 1; i <= n; ++i ) fac[i] = mul ( i, fac[i - 1] );
	ifac[n] = qkpow ( fac[n], MOD - 2 );
	for ( int i = n - 1; ~i; --i ) ifac[i] = mul ( i + 1, ifac[i + 1] );
}

inline int comb ( const int n, const int m ) {
	return n < m ? 0 : mul ( fac[n], mul ( ifac[m], ifac[n - m] ) );
}

inline int stir ( const int n, const int m ) {
	int ret = 0;
	for ( int i = 0; i <= m; ++i ) {
		ret = ( i & 1 ? sub : add )( ret,
			mul ( comb ( m, i ), qkpow ( m - i, n ) ) );
	}
	return mul ( ret, ifac[m] );
}

int main () {
	scanf ( "%d %d", &n, &m );
	if ( m > n - 1 ) return puts ( "0" ), 0;
	init ();
	int ans = mul ( mul ( fac[n], ifac[m] ), stir ( n, n - m ) );
	if ( m ) ans = add ( ans, ans );
	printf ( "%d\n", ans );
	return 0;
}
posted @ 2020-12-07 17:15  Rainybunny  阅读(99)  评论(0编辑  收藏  举报