|
| 1 | +//Codechef//Easy Tiling |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +public class FIBONACCI_logN { |
| 5 | + |
| 6 | + static long MOD = 1000000007; |
| 7 | + public static void main(String[] args)throws IOException { |
| 8 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + |
| 10 | + int t = Integer.parseInt(br.readLine()); |
| 11 | + while(t-- > 0) { |
| 12 | + long n = Long.parseLong(br.readLine()); |
| 13 | + |
| 14 | + if(n == 1) System.out.println(1); |
| 15 | + else if(n == 2) System.out.println(2); |
| 16 | + else{ |
| 17 | + n--; |
| 18 | + long[][] M = {{1,1},{1,0}}; |
| 19 | + long[][] Identity = {{1,0},{0,1}}; |
| 20 | + long[] f = {1,1}; |
| 21 | + while(n > 0){ |
| 22 | + if((n & 1) == 1){ |
| 23 | + Identity = multiply(Identity,M); |
| 24 | + } |
| 25 | + M = multiply(M,M); |
| 26 | + n = n >> 1; |
| 27 | + } |
| 28 | + |
| 29 | + //HERE F(0) = 1; F(1) = 1 THATS WHY F(2) = 2 AND SO ON.. |
| 30 | + long ans = (Identity[0][0] + Identity[0][1])%MOD; // Fibonacci(N) |
| 31 | + System.out.println(ans); |
| 32 | +/* |
| 33 | + long ans = (Identity[1][0] + Identity[1][1])%MOD; // Fibonacci(N - 1) |
| 34 | + System.out.println(ans);*/ |
| 35 | + |
| 36 | + //FOR F(0) = 0; F(1) = 1; |
| 37 | + /*long ans = (Identity[0][0])%MOD; // Fibonacci(N) |
| 38 | + System.out.println(ans); |
| 39 | +
|
| 40 | + long ans = (Identity[1][0])%MOD; // Fibonacci(N - 1) |
| 41 | + System.out.println(ans);*/ |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private static long[][] multiply(long[][] A,long[][] B){ |
| 48 | + long a = (A[0][0]*B[0][0] + A[0][1]*B[1][0])%MOD; |
| 49 | + long b = (A[0][0]*B[0][1] + A[0][1]*B[1][1])%MOD; |
| 50 | + long c = (A[1][0]*B[0][0] + A[1][1]*B[1][0])%MOD; |
| 51 | + long d = (A[1][0]*B[0][1] + A[1][1]*B[1][1])%MOD; |
| 52 | + |
| 53 | + A[0][0] = a; |
| 54 | + A[0][1] = b; |
| 55 | + A[1][0] = c; |
| 56 | + A[1][1] = d; |
| 57 | + |
| 58 | + return A; |
| 59 | + } |
| 60 | +} |
0 commit comments