This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.java
64 lines (51 loc) · 1.59 KB
/
fibonacci.java
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
import java.util.*;
public class fibonacci {
private static final long MOD = (long) 1e9;
public static void main(String[] args) {
Matrix base = new Matrix(new long[][] { { 1, 1 }, { 1, 0 } });
int i = 19; // read this from stdin
// result should be 4181
System.out.println(base.exp(i - 1).mat[0][0]);
}
private static class Matrix {
private long[][] mat;
int m, n;
public Matrix(int m, int n) {
mat = new long[m][n];
this.m = m;
this.n = n;
}
public Matrix(long[][] mat) {
this.mat = mat;
this.m = mat.length;
this.n = mat[0].length;
}
public Matrix mult(Matrix m2) {
int x = this.m;
int y = this.n;
int z = m2.n;
Matrix res = new Matrix(x, z);
for (int i = 0; i < x; i++) {
for (int j = 0; j < z; j++) {
for (int k = 0; k < y; k++) {
res.mat[i][j] += (this.mat[i][k] * m2.mat[k][j]) % MOD;
res.mat[i][j] %= MOD;
}
}
}
return res;
}
public Matrix exp(long p) {
Matrix m = this;
if (p == 0)
return new Matrix(new long[][] { { 1, 0 }, { 0, 1 } });
else if (p == 1)
return m;
else if (p % 2 == 1)
return m.mult(m.exp(p - 1));
else {
return m.mult(m).exp(p / 2);
}
}
}
}