-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 10229 - Modular Fibonacci.cpp
74 lines (59 loc) · 1.43 KB
/
UVA 10229 - Modular Fibonacci.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1000000000
#define ii pair<int, int>
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int, int> >
#define vvi vector< vector<int> >
#define vvl vector< vector<long long> >
#define vvii vector< vector< pair<int, int > > >
int mod;
vvl multiply(vvl a, vvl b)
{
vvl ret(2, vl(2) );
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
ret[i][j] = 0;
for(int k=0; k<2; k++){
ret[i][j] =(ret[i][j] % mod + (a[i][k] * b[k][j]) % mod) % mod;
}
}
}
return ret;
}
vvl mat_pow(vvl a, int e)
{
if(e == 1) return a;
vvl ret = mat_pow(a, e/2);
ret = multiply(ret, ret);
if(e % 2 == 1){
vvl temp(2, vl(2) );
temp[0][0] = temp[0][1] = temp[1][0] = 1;
temp[1][1] = 0;
ret = multiply(ret, temp);
}
return ret;
}
int main()
{
int n, m, f[3] = {0, 1, 1};
while(cin>>n>>m){
mod = 1<<m;
if(n <= 2){
printf("%d\n", f[n] % mod);
continue;
}
vvl a(2, vl(2) );
a[0][0] = a[0][1] = a[1][0] = 1;
a[1][1] = 0;
vvl b = mat_pow(a, n-2);
vl temp(2); temp[0] = 1; temp[1] = 1;
ll ans = 0;
for(int k=0; k<2; k++){
ans = (ans % mod + (b[0][k] * temp[k]) % mod) % mod;
}
printf("%d\n", ans);
}
}