摘要:矩阵优化dp转移

「SHOI2013」超级跳马

题面

题目分析

设$f_{i,j}$为在第$i$列第$j$行,能够转移的方案数的前缀和.

那么可以写出转移:

解释一下,$i$与$i+1$的奇偶性不同,所以可以通过跳马得到转移,$i+1$与$i-1$的奇偶性相同,只能直接累加进前缀和用于以后的转移.

发现转移的形式很优美,可以写成矩阵:

然后矩阵快速幂就可以了,最后$f_{m,n}-f_{m-2,n}$即为答案.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "cstring"
#include "cmath"
#include "cctype"
#include "ctime"
#include "iomanip"
#include "algorithm"
#include "set"
#include "queue"
#include "map"
#include "stack"
#include "deque"
#include "vector"
#define R register
#define INF 0x3f3f3f3f
#define debug(x) printf("debug:%lld\n",x)
#define debugi(x) printf("debug:%d\n",x)
#define debugf(x) printf("debug:%llf\n",x)
#define endl putchar('\n')
typedef long long lxl;
const lxl big=101,p=30011;
lxl n,m;
struct _Matrix
{
lxl a[big][big];
_Matrix(){memset(a,0,sizeof a);}
inline lxl* operator [](const lxl &i){return a[i];}
inline _Matrix operator *(const _Matrix &another)const
{
_Matrix b;
R int i,j,k;
for(i=1;i<=n;++i)
for(k=1;k<=n;++k)
for(j=1;j<=n;++j)
b.a[i][j]=(b.a[i][j]+a[i][k]*another.a[k][j]%p+p)%p;
return b;
}
}T,A,B,I;
inline lxl read()
{
char c(getchar());
lxl f(1),x(0);
for(;!isdigit(c);(c=='-')&&(f=-1),c=getchar());
for(;isdigit(c);x=(x<<1)+(x<<3)+(c^48),c=getchar());
return f*x;
}
inline _Matrix FastPow(_Matrix A,lxl b)
{
_Matrix C=I;
for(;b;b>>=1,A=A*A)if(b&1)C=C*A;
return C;
}
inline void prework()
{
for(R int i(1);i<n;++i)A[i][i+1]=A[i+1][i]=A[i][i]=1;A[n][n]=1;
for(R int i(1);i<=n;++i)A[i][i+n]=A[i+n][i]=1;
n<<=1;
for(R int i(1);i<=n;++i)I[i][i]=1;
T[1][1]=1;
}
int main(void)
{
n=read(),m=read();
prework();
T=T*FastPow(A,m-2),B=T*A;
printf("%lld\n",(B[1][n>>1]-T[1][n]+p)%p);
return 0;
}