forked from bhaveshlohana/HacktoberFest2020-Contributions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix exp.cpp
62 lines (62 loc) · 1.09 KB
/
matrix exp.cpp
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
#include<bits/stdc++.h>
using namespace std;
void multiply(long long int a[2][2],long long int b[2][2])
{
long long int m[2][2];
int i,j,k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
m[i][j]=0;
for(k=0;k<2;k++)
{
m[i][j]=(m[i][j]+a[i][k]*b[k][j]);
}
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=m[i][j];
}
}
}
void exp(long long int a[2][2],long long int n)
{
int i,j;
long long int res[2][2]={1,0,0,1};
while(n>0)
{
if(n%2==1)
{
multiply(res,a);
}
multiply(a,a);
n=n/2;
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=res[i][j];
}
}
}
int main()
{
int i,j;
long long int a[2][2]={0,1,1,1};
long long int n;
cin>>n;
exp(a,n);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}