-
Notifications
You must be signed in to change notification settings - Fork 3
/
My Fair Coin.java
79 lines (52 loc) · 1.47 KB
/
My Fair Coin.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Akash Yadav
// @Sohar, Oman
// 26th December
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class TestClass{
//https://www.codechef.com/problems/CSUMD
public static Scanner scn = new Scanner(System.in);
public static long MOD = 1000000007L;
public static void main (String[] args) throws java.lang.Exception{
int T = scn.nextInt();
while(T-- > 0){
long N = scn.nextLong();
long ans = 0;
if(N == 1)
ans = 1;
else if(N == 2)
ans = 3;
else
ans = (fun(N-1) + fun(N-2))%MOD;
System.out.println(ans);
}
}
public static long fun(long N){
if(N == 1) return 2;
if(N == 2) return 6;
long[][] mat = {{2,2},{1,0}};
long[][] m = {{2,2},{1,0}};
long pow = N;
long ans = power(mat, pow, m);
return ans;
}
public static long power(long[][] mat, long pow, long[][] m){
if(pow == 1)
return ((6 * mat[0][0])%MOD + (2 * mat[0][1])%MOD)%MOD;;
power(mat, pow/2, m);
multiply(mat, mat);
if(pow % 2 == 1)
multiply(mat, m);
return mat[0][0];
}
public static void multiply(long[][] a, long[][] b){
long[][] c = new long[2][2];
c[0][0] = ((a[0][0] * b[0][0])%MOD + (a[0][1] * b[1][0])%MOD)%MOD;
c[0][1] = ((a[0][0] * b[0][1])%MOD + (a[0][1] * b[1][1])%MOD)%MOD;
c[1][0] = ((a[1][0] * b[0][0])%MOD + (a[1][1] * b[1][0])%MOD)%MOD;
c[1][1] = ((a[1][0] * b[0][1])%MOD + (a[1][1] * b[1][1])%MOD)%MOD;
a[0][0] = c[0][0]; a[0][1] = c[0][1]; a[1][0] = c[1][0]; a[1][1] = c[1][1];
}
}