-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA10229.cpp
More file actions
40 lines (40 loc) · 803 Bytes
/
UVA10229.cpp
File metadata and controls
40 lines (40 loc) · 803 Bytes
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
#include<iostream>
#include<cmath>
using namespace std;
void cpyMatrix(long long a[][2], long long b[][2]) {
static int i, j;
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
a[i][j] = b[i][j];
}
}
}
void mulMatrix(long long a[][2], long long b[][2], int c) {
static int i, j, z;
long long temp[2][2] = { 0, 0, 0, 0 };
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
for (z = 0; z < 2; ++z) {
temp[j][i] += a[j][z] * b[i][z];
temp[j][i] &= (1 << c) - 1;//mod
}
}
}
cpyMatrix(a, temp);
}
long long fibon(int n, int m) {
long long a[2][2] = { 1,0,0,1 }, b[2][2] = { 1,1,1,0 };
while (n) {
if (n & 1)
mulMatrix(a, b, m);
mulMatrix(b, b, m);
n /= 2;
}
return a[1][0];
}
int main() {
int n, m;
while (cin >> n >> m) {
cout << fibon(n, m) << endl;
}
}