矩阵快速幂3 k*n铺方格

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <queue>
 5 #include <cstdio>
 6 #include <algorithm>
 7 #include <map>
 8 #include <time.h>
 9 #include <ext/pb_ds/assoc_container.hpp>
10 #include <ext/pb_ds/tree_policy.hpp>
11 #define LL long long
12 
13 using namespace std;
14 using namespace __gnu_pbds;
15 
16 
17 const int MOD = 12357;
18 
19 struct Martix
20 {
21     LL martix[260][260];
22     int row,col;
23     Martix(int _row,int _col)
24     {
25         memset(martix,0,sizeof(martix));
26         row = _row;
27         col = _col;
28     }
29     void sets(int _row,int _col)
30     {
31         memset(martix,0,sizeof(martix));
32         row = _row;
33         col = _col;
34     }
35     Martix operator *(const Martix &A)const
36     {
37         Martix C(row, A.col);
38         for(int i = 0; i < row; i++)
39             for(int j = 0; j < A.col; j++)
40                 for(int k = 0; k < col; k++)
41                 {
42                     C.martix[i][j] =  (C.martix[i][j] + martix[i][k] * A.martix[k][j]);
43                     if(C.martix[i][j] >= MOD)
44                         C.martix[i][j]%=MOD;
45                 }
46 
47         return C;
48     }
49 };
50 
51 //
52 //第i行不放置:new_x = x << 1, new_y = (y << 1) + 1; 列数+1
53 //第i行竖放骨牌:new_x = (x << 1) + 1, new_y = y << 1; 列数+1
54 //第i行横向骨牌:new x = (x << 2) + 3, new_y = (y << 2) + 3; 列数+2
55 
56 int k;
57 Martix A(260,260),F(260,260);
58 void dfs(int x,int y,int col)
59 {
60     if(col == k) {A.martix[y][x] = 1; return ;}
61     dfs(x<<1, (y<<1) + 1, col+1);
62     dfs( (x<<1) + 1, y << 1, col + 1);
63     if(col + 2 <= k)
64         dfs( (x << 2)+ 3, (y << 2)+3, col+2);
65 }
66 
67 void solve()
68 {
69     int n;
70     scanf("%d %d",&k,&n);
71     if( (k&1) && (n&1) )
72     {
73         printf("%d\n",0);
74         return ;
75     }
76     A.sets(1<<k,1<<k);
77     F.sets(1<<k,1<<k);
78     dfs(0,0,0);
79     for(int i = 0; i < (1<<k); i++)
80         F.martix[i][i] = 1;
81     while(n > 0)
82     {
83         if(n & 1)
84             F = F*A;
85         A = A*A;
86         n >>= 1;
87     }
88     printf("%lld\n",F.martix[ (1<<k)-1 ][ (1<<k)-1 ]);
89 }
90 
91 int main(void)
92 {
93     solve();
94     return 0;
95 }

 

posted on 2016-08-05 17:08  鱼泪儿  阅读(171)  评论(0编辑  收藏  举报

导航