[SHOI2013]超级跳马

传送门

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=12if[i2k+1][j1]+f[i2k+1][j]+f[i2k+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][j1]+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);
}
    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/quan_tum/article/details/84789290
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞