【LOJ3160】「NOI2019」斗主地

【题目链接】

  • 点击打开链接

【思路要点】

  • 打表可得,若各个位置 i i i 上权值的期望是关于 i i i 的一 / 二次函数,则经过一次洗牌后,各个位置 i i i 上权值的期望依然是关于 i i i 的一 / 二次函数。
  • 暴力计算牌堆底的三项,然后插值即可。
  • 时间复杂度 O ( M + Q ) O(M+Q) O(M+Q)

【代码】

#include
using namespace std;
const int MAXN = 5;
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("");
}
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;
}
namespace Lagrange {
	const int MAXN = 5;
	int n, x[MAXN], y[MAXN], a[MAXN];
	int p[MAXN], q[MAXN];
	void work() {
		memset(p, 0, sizeof(p));
		memset(q, 0, sizeof(q));
		memset(a, 0, sizeof(a)); p[0] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = i - 1; j >= 0; j--) {
				p[j + 1] = (p[j + 1] + p[j]) % P;
				p[j] = (P - 1ll * p[j] * x[i] % P) % P;
			}
		}
		for (int i = 1; i <= n; i++) {
			memset(q, 0, sizeof(q));
			for (int j = n - 1; j >= 0; j--)
				q[j] = (p[j + 1] + 1ll * q[j + 1] * x[i]) % P;
			int now = 1;
			for (int j = 1; j <= n; j++)
				if (j != i) now = 1ll * now * (x[i] - x[j]) % P;
			now = power((P + now) % P, P - 2);
			for (int j = 0; j <= n; j++)
				q[j] = 1ll * q[j] * now % P;
			for (int j = 0; j <= n; j++)
				a[j] = (a[j] + 1ll * q[j] * y[i]) % P;
		}
	}
	int get(int x) {
		int ans = 0, now = 1;
		for (int i = 0; i <= n; i++) {
			ans = (ans + 1ll * now * a[i]) % P;
			now = 1ll * now * x % P;
		}
		return ans;
	}
}
void update(int &x, int y) {
	x += y;
	if (x >= P) x -= P;
}
int n, m, type, mid, inv[MAXN];
void work(int x, int y, int now, int p) {
	if (now >= 4) return;
	int px = 1ll * x * inv[now] % P;
	int py = 1ll * (y - mid) * inv[now] % P;
	if (px) {
		update(Lagrange :: y[now], 1ll * p * px % P * Lagrange :: get(x) % P);
		work(x - 1, y, now + 1, 1ll * p * px % P);
	}
	if (py) {
		update(Lagrange :: y[now], 1ll * p * py % P * Lagrange :: get(y) % P);
		work(x, y - 1, now + 1, 1ll * p * py % P);
	}
}
int main() {
	freopen("landlords.in", "r", stdin);
	freopen("landlords.out", "w", stdout);
	read(n), read(m), read(type);
	Lagrange :: n = 3;
	Lagrange :: a[type] = 1;
	for (int i = 1; i <= 3; i++) {
		Lagrange :: x[i] = n - i + 1;
		inv[i] = power(n - i + 1, P - 2);
		Lagrange :: y[i] = 0;
	}
	for (int i = 1; i <= m; i++) {
		read(mid);
		work(mid, n, 1, 1);
		Lagrange :: work();
		for (int i = 1; i <= 3; i++)
			Lagrange :: y[i] = 0;
	}
	int q; read(q);
	while (q--) {
		int x; read(x);
		writeln(Lagrange :: get(x));
	}
	return 0;
}

你可能感兴趣的:(【OJ】LOJ,【类型】做题记录,【算法】找规律,【算法】拉格朗日插值法)