设 f [ i ] [ j ] f[i][j] f[i][j] 表示跳到 ( i , j ) (i,j) (i,j) 的方案数,那么
f [ i ] [ j ] = ∑ k = 1 i 2 f [ i − 2 k + 1 ] [ j − 1 ] + f [ i − 2 k + 1 ] [ j ] + f [ i − 2 k + 1 ] [ j + 1 ] f[i][j]=\sum_{k=1}^{\frac{i}{2}} f[i-2k+1][j-1]+f[i-2k+1][j]+f[i-2k+1][j+1] f[i][j]=k=1∑2if[i−2k+1][j−1]+f[i−2k+1][j]+f[i−2k+1][j+1]
维护与当前列相差为偶数和相差为奇数的的两个前缀和
前缀和的转移:
s 1 [ i + 1 ] [ j ] = s 2 [ i ] [ j ] + s 1 [ i ] [ j − 1 ] + s 1 [ i ] [ j ] + s 1 [ i ] [ j + 1 ] , s 2 [ i + 1 ] [ j ] = s 1 [ i ] [ j ] s1[i+1][j]=s2[i][j]+s1[i][j-1]+s1[i][j]+s1[i][j+1],s2[i+1][j]=s1[i][j] s1[i+1][j]=s2[i][j]+s1[i][j−1]+s1[i][j]+s1[i][j+1],s2[i+1][j]=s1[i][j]
发现可以用矩乘优化
最后两前缀相减
#include<bits/stdc++.h>
using namespace std;
const int mo=30011;int n,m;
struct M{
int w[105][105];
M(){memset(w,0,sizeof(w));}
int *operator[](int a){return w[a];}
M operator*(M &a){
M c;for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) for(int k=1;k<=n;++k)
c[i][j]=(c[i][j]+w[i][k]*a[k][j])%mo;return c;
}
}p,x,y;
M pow(M a,int b){
M c;for(int i=1;i<=n;++i) c[i][i]=1;
for(;b;b>>=1,a=a*a) if(b&1) c=c*a;return c;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) p[i][i]=p[i+n][i]=p[i][i+n]=1;
for(int i=1;i<n;++i) p[i+1][i]=p[i][i+1]=1;n<<=1,x=pow(p,m-2),y=x*p;
return !printf("%d",(y[1][n>>1]-x[1][n]+mo)%mo);
}