LY1156 [ 20230320 CQYC省选模拟赛 T3 ] 集结
题意
平面上 \(n\) 个点,每个点按照曼哈顿距离移动。
要求在 \(m\) 时刻后,所有点都处于同一位置。
求方案数。
Sol
平凡地,考虑曼哈顿距离转切比雪夫距离。
这样 \(x\) 和 \(y\) 就完全独立了。
考虑先算 \(x\) 的贡献,再算 \(y\) 的贡献。
判断一下是否能到当前的 \(x\) 或 \(y\) 就做完了。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define ll long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
#define fi first
#define se second
const int N = 1e3 + 5, M = 1e5 + 5, mod = 998244353;
int pow_(int x, int k, int p) {
int ans = 1;
while (k) {
if (k & 1) ans = 1ll * ans * x % p;
x = 1ll * x * x % p;
k >>= 1;
}
return ans;
}
array <int, M> fac, inv;
void init(int n = 1e5 + 2) {
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = 1ll * fac[i - 1] * i % mod;
inv[n] = pow_(fac[n], mod - 2, mod);
for (int i = n; i; i--)
inv[i - 1] = 1ll * inv[i] * i % mod;
}
int C(int n, int m) {
if (n < m) return 0;
return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
array <pii, N> qrl;
void Mod(int &x) {
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
signed main() {
/* freopen("assemble.in", "r", stdin); */
/* freopen("assemble.out", "w", stdout); */
int n = read(), m = read();
for (int i = 1, x, y; i <= n; i++)
x = read(), y = read(), qrl[i] = make_pair(x + y, x - y);
init();
sort(qrl.begin() + 1, qrl.begin() + 1 + n);
int tp0 = 0, tp1 = 0;
for (int i = qrl[1].fi - m; i <= qrl[n].fi + m; i++) {
bool flg = 0;
for (int j = 1; j <= n; j++)
if (abs(qrl[j].fi - i) > m || ((m - abs(qrl[j].fi - i)) & 1))
flg = 1;
if (flg) continue;
int sum = 1;
for (int j = 1; j <= n; j++)
sum = 1ll * sum * C(m, (m - abs(qrl[j].fi - i)) / 2) % mod;
tp0 += sum, Mod(tp0);
}
sort(qrl.begin() + 1, qrl.begin() + 1 + n, [](pii x, pii y) {
return x.se < y.se;
});
for (int i = qrl[1].se - m; i <= qrl[n].se + m; i++) {
bool flg = 0;
for (int j = 1; j <= n; j++)
if (abs(qrl[j].se - i) > m || ((m - abs(qrl[j].se - i)) & 1))
flg = 1;
if (flg) continue;
int sum = 1;
for (int j = 1; j <= n; j++)
sum = 1ll * sum * C(m, (m - abs(qrl[j].se - i)) / 2) % mod;
tp1 += sum, Mod(tp1);
}
write(1ll * tp0 * tp1 % mod), puts("");
return 0;
}