[COGS 2287][HZOI 2015]疯狂的机器人

Description

题库链接

现在在二维平面内原点上有一只机器人,他每次可以选择向右走,向左走,向下走,向上走和不走(每次如果走只能走一格)。机器人不能走到横坐标是负数或者纵坐标是负数的点上。

给定操作次数 $n$ ,求有多少种不同的操作序列使得机器人在操作后会回到原点,输出答案模 $998244353$ 后的结果。

$1\leq n\leq 100000$

Solution

应该不难想吧...

显然我们先考虑前四种走法...不走的情况可以组合数求出来。

对于一类操作(向上向下或向左向右),显然是成组出现的。更具体地,这就是 $Catalan$ 数。

记 $Catalan$ 数的第 $i$ 项为 $C_i$ 。

记多项式

$$A(x)=\sum_{i=0}^\infty \frac{[2\mid i]\cdot C_{\left\lfloor\frac{i}{2}\right\rfloor}}{i!}x^i$$

那么答案就是 $\sum_{i=0}^n [2\mid i]\cdot i!\cdot A^2(i)\cdot {n\choose i}$ 。 $\text{NTT}$ 优化即可 。

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 4*100000, yzh = 998244353;

int n, inv[N+5], fac[N+5], ifac[N+5], a[N+5], len, R[N+5], L;

int quick_pow(int a, int b) {
    int ans = 1;
    while (b) {
    if (b&1) ans = 1ll*ans*a%yzh;
    b >>= 1, a = 1ll*a*a%yzh;
    }
    return ans;
}
void NTT(int *A, int o) {
    for (int i = 0; i < len; i++) if (i < R[i]) swap(A[i], A[R[i]]);
    for (int i = 1; i < len; i <<= 1) {
    int gn = quick_pow(3, (yzh-1)/(i<<1)), x, y;
    if (o == -1) gn = quick_pow(gn, yzh-2);
    for (int j = 0; j < len; j += (i<<1)) {
        int g = 1;
        for (int k = 0; k < i; k++, g = 1ll*g*gn%yzh) {
        x = A[j+k], y = 1ll*g*A[j+k+i]%yzh;
        A[j+k] = (x+y)%yzh, A[j+k+i] = (x-y)%yzh;
        }
    }
    }
}
int C(int n, int m) {return 1ll*fac[n]*ifac[m]%yzh*ifac[n-m]%yzh; }
void work() {
    scanf("%d", &n); inv[0] = inv[1] = fac[0] = ifac[0] = 1;
    for (int i = 1; i <= n; i++) fac[i] = 1ll*i*fac[i-1]%yzh;
    for (int i = 2; i <= n+1; i++) inv[i] = -1ll*yzh/i*inv[yzh%i]%yzh;
    for (int i = 1; i <= n; i++) ifac[i] = 1ll*inv[i]*ifac[i-1]%yzh;
    for (int i = 0; i <= n; i += 2) a[i] = 1ll*C(i, i/2)*inv[i/2+1]%yzh*ifac[i]%yzh;
    for (len = 1; len <= (n<<1); len <<= 1) ++L;
    for (int i = 0; i < len; i++) R[i] = (R[i>>1]>>1)|((i&1)<<(L-1));
    NTT(a, 1);
    for (int i = 0; i < len; i++) a[i] = 1ll*a[i]*a[i]%yzh;
    NTT(a, -1);
    for (int i = 0, inv = quick_pow(len, yzh-2); i < len; i++)
    a[i] = 1ll*a[i]*inv%yzh*fac[i]%yzh;
    int ans = 0;
    for (int i = 0; i <= n; i += 2)
    (ans += 1ll*a[i]*C(n, i)%yzh) %= yzh;
    printf("%d\n", (ans+yzh)%yzh);
}
int main() {work(); return 0; }
posted @ 2018-04-06 15:24  NaVi_Awson  阅读(274)  评论(0编辑  收藏  举报