BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]

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

题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数


这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg


其实就是卡特兰数
只不过这里\(C(i)\)是第\(\frac{i}{2}\)项,奇数为0
\(f[n]\)为走n次回到原点方案数,$$
f[n]=\sum_{i=0}{n}C(i)C(n-i)\binom{n}{i}=n!\sum_{i=0}C(i)\frac{1}{i!}C(n-i)\frac{1}{(n-i)!}

\[ 注意阶乘和阶乘逆元别乘错了,别丢东西!!! ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=(1<<18)+5, INF=1e9; const ll P=998244353; inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } ll Pow(ll a, ll b, ll P) { ll ans=1; for(; b; b>>=1, a=a*a%P) if(b&1) ans=ans*a%P; return ans; } namespace NTT{ int n, rev[N], g; void ini(int lim) { g=3; n=1; int k=0; while(n<lim) n<<=1, k++; for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1)); } void dft(ll *a, int flag) { for(int i=0; i<n; i++) if(i<rev[i]) swap(a[i], a[rev[i]]); for(int l=2; l<=n; l<<=1) { int m=l>>1; ll wn=Pow(g, flag==1 ? (P-1)/l : P-1-(P-1)/l, P); for(ll *p=a; p!=a+n; p+=l) { ll w=1; for(int k=0; k<m; k++) { ll t = w*p[k+m]%P; p[k+m] = (p[k] - t + P)%P; p[k] = (p[k] + t)%P; w = w*wn%P; } } } if(flag==-1) { ll inv=Pow(n, P-2, P); for(int i=0; i<n; i++) a[i] = a[i]*inv%P; } } void mul(ll *a, ll *b) { dft(a, 1); for(int i=0; i<n; i++) a[i]=a[i]*a[i]%P; dft(a, -1); } }using NTT::dft; using NTT::ini; using NTT::mul; int n; ll inv[N], fac[N], facInv[N]; ll a[N], b[N]; ll C(int n, int m) {return fac[n]*facInv[m]%P*facInv[n-m]%P;} int main() { //freopen("in","r",stdin); freopen("crazy_robot.in","r",stdin); freopen("crazy_robot.out","w",stdout); n=read(); ini(n+n+1); inv[1]=1; fac[0]=facInv[0]=1; for(int i=1; i<=n; i++) { if(i!=1) inv[i] = (P-P/i)*inv[P%i]%P; fac[i] = fac[i-1]*i%P; facInv[i] = facInv[i-1]*inv[i]%P; } a[0]=b[0]=1; for(int i=2; i<=n; i+=2) a[i]=b[i]= C(i, i>>1) * inv[(i>>1)+1] %P * facInv[i] %P; mul(a, b); for(int i=0; i<=n; i++) a[i]=a[i]*fac[i]%P; ll ans=0; for(int m=0; m<=n; m+=2) (ans += C(n, m) * a[m]%P) %=P; printf("%lld\n", ans); } ```\]

posted @ 2017-03-30 21:48  Candy?  阅读(639)  评论(1编辑  收藏  举报