感觉挺高妙的……
为了方便,不妨把横纵坐标都整体减 \(1\)。
如果单独考虑上下移动,方案数是 \(\binom{2n}{n}\)。发现两个人上下总共移动 \(n\) 次后一定会在同一行,设这行编号为 \(x\),那么最后带个 \(\binom{n}{x}^2\) 的系数,并且除掉上下移动后方案本质相同。考虑只计算在第 \(n\) 行相遇的情况,最后乘上系数 \(\binom{2n}{n}\)。
设 \(f_i\) 表示两个人在 \((n, i)\) 第一次相遇的方案数。那么:
\[ans = \binom{2n}{n} \sum\limits_{i=0}^m f_i f_{m-i} \]限制第一次不好算,考虑容斥。设 \(g_i\) 表示两个人在 \((n, i)\) 相遇的方案数(不一定是第一次)。那么 \(g_i\) 是容易求的,\(g_i = \binom{2i + n}{i, i, n}\)。设 \(h_i\) 为卡特兰数第 \(i\) 项,那么:
\[f_i = g_i - \sum\limits_{j=0}^{i-1} 2 g_j h_{i-j-1} \]表示枚举 \((n, j)\) 是最后一次相遇的坐标,之后选一个人往前走一步,然后两个人往前走,后走的那个人不能和先走的那个人重合。这个相当于合法括号序列计数,方案数就是卡特兰数的第 \(i - j - 1\) 项。
直接计算是 \(O(n + m^2)\) 的。观察 \(f_i\) 转移式,发现是卷积形式,直接做一遍乘法卷积,复杂度降至 \(O(n + m \log m)\)。
code
// Problem: F - Chance Meeting
// Contest: AtCoder - AtCoder Regular Contest 124
// URL: https://atcoder.jp/contests/arc124/tasks/arc124_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 2000100;
const int N = 2000000;
const ll mod = 998244353, G = 3;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
ll n, m, fac[maxn], ifac[maxn], r[maxn], f[maxn], g[maxn], h[maxn];
inline ll C(ll n, ll m) {
if (n < m || n < 0 || m < 0) {
return 0;
} else {
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
}
void init() {
fac[0] = 1;
for (int i = 1; i <= N; ++i) {
fac[i] = fac[i - 1] * i % mod;
}
ifac[N] = qpow(fac[N], mod - 2);
for (int i = N - 1; ~i; --i) {
ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
for (int i = 0; i <= N / 2; ++i) {
h[i] = (C(i * 2, i) - C(i * 2, i + 1) + mod) % mod;
}
}
typedef vector<ll> poly;
inline poly NTT(poly a, int op) {
int n = (int)a.size();
for (int i = 0; i < n; ++i) {
if (i < r[i]) {
swap(a[i], a[r[i]]);
}
}
for (int k = 1; k < n; k <<= 1) {
ll wn = qpow(op == 1 ? G : qpow(G, mod - 2), (mod - 1) / (k << 1));
for (int i = 0; i < n; i += (k << 1)) {
ll w = 1;
for (int j = 0; j < k; ++j, w = w * wn % mod) {
ll x = a[i + j], y = w * a[i + j + k] % mod;
a[i + j] = (x + y) % mod;
a[i + j + k] = (x - y + mod) % mod;
}
}
}
return a;
}
inline poly operator * (poly a, poly b) {
a = NTT(a, 1);
b = NTT(b, 1);
int n = (int)a.size();
for (int i = 0; i < n; ++i) {
a[i] = a[i] * b[i] % mod;
}
a = NTT(a, -1);
ll inv = qpow(n, mod - 2);
for (int i = 0; i < n; ++i) {
a[i] = a[i] * inv % mod;
}
return a;
}
void solve() {
scanf("%lld%lld", &n, &m);
--n;
--m;
for (int i = 0; i <= m; ++i) {
g[i] = C(i + n, i) * C(i * 2 + n, i) % mod;
}
poly A, B;
int k = 0;
while ((1 << k) <= m * 2) {
++k;
}
for (int i = 1; i < (1 << k); ++i) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (k - 1));
}
for (int i = 0; i < (1 << k); ++i) {
A.pb(g[i]);
B.pb(i < m ? h[i] : 0);
}
poly res = A * B;
for (int i = 0; i <= m; ++i) {
f[i] = (g[i] - (i ? res[i - 1] * 2 % mod : 0) + mod) % mod;
}
ll ans = 0;
for (int i = 0; i <= m; ++i) {
ans = (ans + f[i] * f[m - i] % mod) % mod;
}
ans = ans * C(n * 2, n) % mod;
printf("%lld\n", ans);
}
int main() {
init();
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}