-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
63 lines (54 loc) · 1.36 KB
/
fibonacci.cpp
File metadata and controls
63 lines (54 loc) · 1.36 KB
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
const int MOD = 10000;
void mult(int A[2][2], int B[2][2], int C[2][2]) {
C[0][0] = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % MOD;
C[0][1] = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % MOD;
C[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % MOD;
C[1][1] = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % MOD;
}
void sq(int A[2][2], int B[2][2]) { mult(A, A, B); }
void pow2(int A[2][2], int n, int B[2][2]) {
int E[2][2], F[2][2];
if (n == 0) {
B[0][0] = B[1][1] = 1;
B[0][1] = B[1][0] = 0;
return;
}
if (n % 2 == 0) {
sq(A, E);
pow2(E, n / 2, B);
return;
}
sq(A, E);
pow2(E, n / 2, F);
mult(F, A, B);
}
void solve(long long n) {
if (n == 0)
cout << 0 << endl;
else if (n == 1)
cout << 1 << endl;
else {
int B[2][2];
int A[2][2] = {{1, 1},
{1, 0}};
pow2(A, n - 1, B);
cout << B[0][0] << endl;
}
}
void print(int A[2][2]) {
cout << A[0][0] << " " << A[0][1] << endl;
cout << A[1][0] << " " << A[1][1] << endl;
}
int main() {
for (long long n; cin >> n && ~n;) {
solve(n);
}
return 0;
}