-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix_Expo.cpp
91 lines (90 loc) · 2.09 KB
/
Matrix_Expo.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Credit = Araf-Al-Jami
// Complexity = O(log(n))
// Code for UVA-10229 - Modular Fibonacci
#include<bits/stdc++.h>
using namespace std;
int mod=(int)1e9+7;
const int mat_sz = 2;
struct Matrix {
int a[mat_sz][mat_sz];
void clear() {
memset(a, 0, sizeof(a));
}
void one() {
for( int i=0; i<mat_sz; i++ ) {
for( int j=0; j<mat_sz; j++ ) {
a[i][j] = 1;
}
}
a[1][1]=0;
}
void print()
{
for( int i=0; i<mat_sz; i++ ) {
for( int j=0; j<mat_sz; j++ ) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
Matrix operator + (const Matrix &b) const {
Matrix tmp;
tmp.clear();
for (int i = 0; i < mat_sz; i++) {
for (int j = 0; j < mat_sz; j++) {
tmp.a[i][j] = a[i][j] + b.a[i][j];
if (tmp.a[i][j] >= mod) {
tmp.a[i][j] -= mod;
}
}
}
return tmp;
}
Matrix operator * (const Matrix &b) const {
Matrix tmp;
tmp.clear();
for (int i = 0; i < mat_sz; i++) {
for (int j = 0; j < mat_sz; j++) {
for (int k = 0; k < mat_sz; k++) {
tmp.a[i][k] += (long long)a[i][j] * b.a[j][k] % mod;
if (tmp.a[i][k] >= mod) {
tmp.a[i][k] -= mod;
}
}
}
}
return tmp;
}
Matrix pw(int x) {
Matrix ans, num = *this;
ans.one();
while (x > 0) {
if (x & 1) {
ans = ans * num;
}
num = num * num;
x >>= 1;
}
return ans;
}
};
int main()
{
int n,m,cnt=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
mod=(1<<m);
if(m==0)
mod=1;
if(n==0)
{
printf("0\n");
continue;
}
Matrix mat;
mat.one();
Matrix ans=mat.pw(n-1);
long long c=ans.a[1][0];
printf("%lld\n",c%mod);
}
}