矩阵快速幂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 }
时间: 2024-08-28 10:55:19

矩阵快速幂3 k*n铺方格的相关文章

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

瓷砖铺放 (状压DP+矩阵快速幂)

未加矩阵快速幂 50分 1 const dx:array[1..8,1..3] of longint= 2 ((-1,0,0),(-1,0,0),(1,0,0),(0,1,0),(-1,0,0),(-1,1,0),(0,1,0),(-1,0,1)); 3 dy:array[1..8,1..3] of longint= 4 ((0,1,0),(0,-1,0),(0,-1,0),(1,0,0),(0,1,-1),(0,0,-1),(1,0,-1),(0,1,0)); 5 mo=65521; 6 va

hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output对应每组数据,输出Tr(A^k)%9973. Sample Input22 21 00 13 999999991 2 34

hdu 2157 从a点走到b点刚好k步的方案数是多少 (矩阵快速幂)

n个点 m条路 询问T次 从a点走到b点刚好k步的方案数是多少 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值把 给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j.令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就 等于从点i到点j恰好经过2条边的路径数(枚举k为中转点).类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数 Sample Input4 4 // n m0 10 21 32 32 //T0 3 2

poj 3613 经过k条边最短路 floyd+矩阵快速幂

http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路,跑k-1次"floyd"即可(使用矩阵快速幂的思想). 把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j.令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点).类似地,C*A的第i行第j列就

POJ3420 Quad Tiling DP + 矩阵快速幂

题目大意是用1*2的骨牌堆积成4*N的矩形,一共有多少种方法,N不超过10^9. 这题和曾经在庞果网上做过的一道木块砌墙几乎一样.因为骨牌我们可以横着放,竖着放,我们假设以4为列,N为行这样去看,并且在骨牌覆盖的位置上置1,所以一共最多有16种状态.我们在第M行放骨牌的时候,第M+1行的状态也是有可能被改变的,设S(i,j)表示某一行状态为i时,将其铺满后下一行状态为j的方案书.考虑下如果我们让矩阵S和S相乘会有什么意义,考虑一下会发现S*S的意义当某行状态为i,接着其后面第2行的状态为j的可行

HDU6185 Covering (递推+矩阵快速幂)

大致题意:让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案对1000000007取模 递推得f(n)=f(n-1)+5*f(n-2)+f(n-3)-f(n-4),因为n很大,所以接下来用矩阵快速幂搞搞就可以了. #include <iostream> #include <cstring> #include <cstdio> using namespace std; long long n; long long

HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

Covering Bob's school has a big playground, boys and girls always play games here after school. To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets. Meanwh

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr