看起来比较舒服的矩阵乘法模板

转载源页面
poj 3070

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
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>

#define SIZE 4
#define mod 10000

using namespace std;

struct MATRIX
{
int mt[SIZE][SIZE];
int x,y;
}ans,def;

int n;

inline MATRIX operator *(MATRIX a,MATRIX b)
{
MATRIX c;
memset(c.mt,0,sizeof c.mt);
c.x=a.x; c.y=b.y;
for(int i=1;i<=a.x;i++)
for(int j=1;j<=b.y;j++)
for(int k=1;k<=a.y;k++)
c.mt[i][j]=(c.mt[i][j]+(a.mt[i][k]%mod)*(b.mt[k][j]%mod))%mod;
return c;
}

inline MATRIX operator +(MATRIX a,MATRIX b)
{
MATRIX c;
memset(c.mt,0,sizeof c.mt);
c.x=a.x; c.y=a.y;
for(int i=1;i<=c.x;i++)
for(int j=1;j<=c.y;j++)
c.mt[i][j]=(a.mt[i][j]+b.mt[i][j])%mod;
return c;
}

inline bool prt(MATRIX &c)
{
for(int i=1;i<=c.x;i++)
{
for(int j=1;j<=c.y;j++) printf("%d ",c.mt[i][j]);
puts("");
}
}

void go()
{
n-=2;
def.mt[1][1]=def.mt[1][2]=def.mt[2][1]=1;
def.mt[2][2]=0; def.x=def.y=2;
ans.mt[1][1]=ans.mt[1][2]=ans.mt[2][1]=1; ans.mt[2][2]=0;
ans.x=ans.y=2;

while(n)
{
if(n&1) ans=ans*def;
def=def*def;
n>>=1;
}
printf("%d\n",ans.mt[1][1]);
}

int main()
{
while(scanf("%d",&n))
{
if(n==-1) break;
else if(n==0) puts("0");
else if(n==1) puts("1");
else go();
}
system("pause");
return 0;
}

看起来比较舒服的矩阵乘法模板

https://devblog.citruxonve.net/posts/c8084c88/

Author

Semprathlon / Simfae Dean

Posted on

05/03/2015

Updated on

07/19/2023

Licensed under

Comments