-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay52.java
61 lines (45 loc) · 1.47 KB
/
Day52.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
/* Nth Fibonacci Number using heap */
public class Day52 {
static int[][] multiply(int[][] a, int[][] b, int m) {
int rowA = a.length;
int colA = a[0].length;
int colB = b[0].length;
int[][] result = new int[rowA][colB];
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colB; j++) {
for (int k = 0; k < colA; k++) {
result[i][j] = (result[i][j] + (int) ((long) a[i][k] * b[k][j] % m)) % m;
}
}
}
return result;
}
static int[][] power(int[][] matrix, int n, int m) {
int size = matrix.length;
int[][] result = new int[size][size];
for (int i = 0; i < size; i++) {
result[i][i] = 1;
}
while (n > 0) {
if (n % 2 == 1) {
result = multiply(result, matrix, m);
}
matrix = multiply(matrix, matrix, m);
n /= 2;
}
return result;
}
static int nthFibonacci(int n) {
if (n <= 0) {
return 0;
}
int[][] baseMatrix = {{1, 1}, {1, 0}};
int[][] resultMatrix = power(baseMatrix, n - 1, 1000000007);
return resultMatrix[0][0];
}
public static void main(String[] args) {
int n = 10; // nth Fibonacci numbers
int nthFib = nthFibonacci(n);
System.out.println("The " + n + "th Fibonacci number modulo 1000000007 is: " + nthFib);
}
}