【省内训练2018-11-23】Bishop

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/84449909

【思路要点】

  • 先考虑一个子问题,在 NNN*N 棋盘的主对角线及其右下方放置 KK 个不能互相攻击的车,求方案数 f(N,k)f(N,k)。考虑最后一行的放置情况,有递推式 f(N,k)=f(N1,k)+(Nk+1)f(N1,k1)f(N,k)=f(N-1,k)+(N-k+1)*f(N-1,k-1) 。观察该递推式的形式,不难发现其实际上等于第二类斯特林数 S(N+1,N+1k)S(N+1,N+1-k)
  • 在本题中,首先,象的移动方式不会改变坐标 (x,y)(x,y) 对应的 x+yx+y 的奇偶性,我们可以将这两类坐标分开处理,再将结果卷积起来,得到答案。
  • 考虑 x+yx+y 为偶数的坐标,我们将棋盘旋转 4545 度,我们得到了一个类似于上述子问题的问题形式,每一行可以放入车的方格数为 1,1,3,3,5,5,7,7,...1,1,3,3,5,5,7,7,... ,换而言之,偶数行的主对角线上是不允许放入棋子的。
  • 考虑对这条新加入的限制进行容斥,记 M=N2M=\lfloor\frac{N}{2}\rfloor ,新问题的方案数为 g(N,K)g(N,K) 。枚举强制放入棋子的方格数 ii ,则有 g(N,k)=i=0M(1)i(Mi)S(Ni+1,Nk+1)g(N,k)=\sum_{i=0}^{M}(-1)^i\binom{M}{i}S(N-i+1,N-k+1)
  • 展开斯特林数,有 g(N,k)=i=0M(1)i(Mi)1(Nk1)!j=0Nk+1(1)Nk+1j(Nk+1j)jNi+1g(N,k)=\sum_{i=0}^{M}(-1)^i\binom{M}{i}\frac{1}{(N-k-1)!}\sum_{j=0}^{N-k+1}(-1)^{N-k+1-j}\binom{N-k+1}{j}j^{N-i+1}
  • 交换求和顺序,有 g(N,k)=1(Nk1)!j=0Nk+1(1)Nk+1j(Nk+1j)i=0M(1)i(Mi)jNi+1g(N,k)=\frac{1}{(N-k-1)!}\sum_{j=0}^{N-k+1}(-1)^{N-k+1-j}\binom{N-k+1}{j}\sum_{i=0}^{M}(-1)^{i}\binom{M}{i}j^{N-i+1}
  • 由二项式定理,有 g(N,k)=1(Nk1)!j=0Nk+1(1)Nk+1j(Nk+1j)(j1)MjNM+1g(N,k)=\frac{1}{(N-k-1)!}\sum_{j=0}^{N-k+1}(-1)^{N-k+1-j}\binom{N-k+1}{j}(j-1)^Mj^{N-M+1}
  • NTTNTT 对其卷积即可,时间复杂度 O(NLogN)O(NLogN)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 262144;
const int P = 998244353;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
namespace NTT {
	const int MAXN = 262144;
	const int P = 998244353;
	const int G = 3;
	int power(int x, int y) {
		if (y == 0) return 1;
		int tmp = power(x, y / 2);
		if (y % 2 == 0) return 1ll * tmp * tmp % P;
		else return 1ll * tmp * tmp % P * x % P;
	}
	int N, Log, home[MAXN];
	void NTTinit() {
		for (int i = 0; i < N; i++) {
			int ans = 0, tmp = i;
			for (int j = 1; j <= Log; j++) {
				ans <<= 1;
				ans += tmp & 1;
				tmp >>= 1;
			}
			home[i] = ans;
		}
	}
	void NTT(int *a, int mode) {
		for (int i = 0; i < N; i++)
			if (home[i] < i) swap(a[i], a[home[i]]);
		for (int len = 2; len <= N; len <<= 1) {
			int delta;
			if (mode == 1) delta = power(G, (P - 1) / len);
			else delta = power(G, P - 1 - (P - 1) / len);
			for (int i = 0; i < N; i += len) {
				int now = 1;
				for (int j = i, k = i + len / 2; k < i + len; j++, k++) {
					int tmp = a[j];
					int tnp = 1ll * a[k] * now % P;
					a[j] = (tmp + tnp) % P;
					a[k] = (tmp - tnp + P) % P;
					now = 1ll * now * delta % P;
				}
			}
		}
		if (mode == -1) {
			int inv = power(N, P - 2);
			for (int i = 0; i < N; i++)
				a[i] = 1ll * a[i] * inv % P;
		}
	}
	void times(int *a, int *b, int *c, int limit) {
		N = 1, Log = 0;
		while (N < 2 * limit) {
			N <<= 1;
			Log++;
		}
		for (int i = limit; i < N; i++)
			a[i] = b[i] = 0;
		NTTinit();
		NTT(a, 1);
		NTT(b, 1);
		for (int i = 0; i < N; i++)
			c[i] = 1ll * a[i] * b[i] % P;
		NTT(c, -1);
	}
}
int power(int x, int y) {
	if (y == 0) return 1;
	int tmp = power(x, y / 2);
	if (y % 2 == 0) return 1ll * tmp * tmp % P;
	else return 1ll * tmp * tmp % P * x % P;
}
int fac[MAXN], inv[MAXN];
int odd[MAXN], even[MAXN], res[MAXN];
void update(int &x, int y) {
	x += y;
	if (x >= P) x -= P;
}
void init(int n) {
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = 1ll * fac[i - 1] * i % P;
	inv[n] = power(fac[n], P - 2);
	for (int i = n - 1; i >= 0; i--)
		inv[i] = inv[i + 1] * (i + 1ll) % P;
}
void getans(int n, int *res) {
	static int a[MAXN], b[MAXN];
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	int m = n / 2;
	for (int i = 0; i <= n + 1; i++) {
		a[i] = 1ll * inv[i] * power(i, n - m + 1) % P * power((i - 1 + P) % P, m) % P;
		b[i] = 1ll * inv[i] * power(P - 1, i) % P;
	}
	static int c[MAXN];
	NTT :: times(a, b, c, n + 2);
	for (int i = 0; i <= n; i++)
		res[i] = c[n - i + 1];
}
int main() {
	int n; read(n);
	init(n + 1);
	getans(n, odd);
	getans(n - 1, even);
	for (int i = n; i >= 1; i--)
		update(even[i], 1ll * even[i - 1] * (n - i) % P);
	NTT :: times(odd, even, res, n + 1);
	for (int i = 1; i <= 2 * n - 1; i++)
		printf("%d ", res[i]);
	return 0;
}
阅读更多

没有更多推荐了,返回首页