# [bzoj4417] [Shoi2013] 超级跳马

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #define M 110
5 #define MOD 30011
6 using namespace std;
7 struct matrix{
8     int a[M][M],n,m;
9     matrix(){memset(a,0,sizeof(a)); n=m=0;}
10     matrix(int nn,int mm){memset(a,0,sizeof(a)); n=nn; m=mm;}
11     friend matrix operator *(matrix a,matrix b){
12         matrix c=matrix(a.n,b.m);
13         for(int i=1;i<=a.n;i++)
14         for(int j=1;j<=b.m;j++)
15         for(int k=1;k<=a.m;k++)
16         c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%MOD;
17         return c;
18     }
19     friend matrix operator ^(matrix a,int p){
20         matrix c=a; p--;
21         if(p<0) {memset(c.a,0,sizeof(c.a)); if(p==-1) c.a[1][1]=1; return c;}
22         while(p){
23             if(p&1) c=c*a;
24             p>>=1; a=a*a;
25         }
26         return c;
27     }
28 }f,f1,f2,ans1,ans2;
29 int n,m;
30 int main(){
31     scanf("%d%d",&n,&m);
32     f=matrix(n<<1,n<<1); ans1=ans2=matrix(1,n<<1);
33     ans1.a[1][1]=ans2.a[1][1]=1;
34     for(int i=1;i<=n;i++) f.a[i][i-1]=f.a[i][i]=f.a[i][i+1]=1;
35     f.a[1][0]=f.a[n][n+1]=0;
36     for(int i=1;i<=n;i++) f.a[i][n+i]=f.a[n+i][i]=1;
37     f1=f^(m-1); f2=f^(m-3);
38     ans1=ans1*f1; ans2=ans2*f2;
39     printf("%d\n",(ans1.a[1][n]-ans2.a[1][n]+MOD)%MOD);
40 }

posted @ 2017-10-26 10:40  AlphaInf  阅读(426)  评论(0编辑  收藏  举报