-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneralised Fibonacci numbers
68 lines (53 loc) · 1.74 KB
/
Generalised Fibonacci numbers
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
package gfg;
//Generalised Fibonacci numbers
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String S[] = read.readLine().split(" ");
long a = Long.parseLong(S[0]);
long b = Long.parseLong(S[1]);
long c = Long.parseLong(S[2]);
long n = Long.parseLong(S[3]);
long m = Long.parseLong(S[4]);
Solution ob = new Solution();
System.out.println(ob.genFibNum(a,b,c,n,m));
}
}
}
class Solution {
static long[][] multiply(long[][] A, long[][] B, long m) {
int size = A.length;
long[][] res = new long[size][size];
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
for(int k = 0; k < size; k++) {
res[i][j] = (res[i][j] + (A[i][k] % m * B[k][j] % m) % m) % m;
}
}
}
return res;
}
static long[][] power(long[][] mat, long R, long m) {
if(R == 1) {
return mat;
}
long[][] ans = power(mat, R / 2, m);
ans = multiply(ans, ans, m);
if((R % 2) == 1) {
ans = multiply(ans, mat, m);
}
return ans;
}
static long genFibNum(Long a, Long b, Long c, long n, long m) {
if(n <= 2) {
return (1 % m);
}
long[][] mat = {{a, b, 1}, {1, 0, 0}, {0, 0, 1}};
long[][] ans = power(mat, n - 2, m);
return (ans[0][0] + ans[0][1] + ans[0][2] * c) % m;
}
};