import java util stream IntStream import java util Scanner import 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
80
81
82
83
84
85
86
87
import java.util.stream.IntStream;
import java.util.Scanner;
import java.lang.Math;
import java.math.BigInteger;
public class Factorial {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int pow = sc.nextInt(),i=0,j=0,k=0,count=0;
int FalsePow=pow;
while(FalsePow>0)
{
if((FalsePow & 1)==1)
count++;
FalsePow=FalsePow>>1;
}
int [] pows=new int [count];
for(i=0,count=0,FalsePow=pow;FalsePow>0;i++)
{
if((FalsePow & 1)==1) {
pows[count] = i;
//System.out.print(pows[count]);
count++;
}
FalsePow=FalsePow>>1;
}
BigInteger [][] Matrix=new BigInteger [2][2];
Matrix[0][0]=BigInteger.ONE;
Matrix[1][1]=BigInteger.ONE;
Matrix[0][1]=BigInteger.ZERO;
Matrix[1][0]=BigInteger.ZERO;
for(i=0;i<count;i++)
{
Matrix=MatrixMulty(Matrix,MatrixPow(pows[i]));
}
System.out.println("");
System.out.print(Matrix[0][0]);
}
public static BigInteger[][] MatrixPow(int n) {
int i;
BigInteger [][] answer=new BigInteger [2][2];
answer[0][0]=BigInteger.ONE;
answer[0][1]=BigInteger.ONE;
answer[1][0]=BigInteger.ONE;
answer[1][1]=BigInteger.ZERO;
BigInteger one,two,three,four;
if(n==1)
{
one=answer[0][0];
two=answer[0][1];
three=answer[1][0];
four=answer[1][1];
answer[0][0]=(one.multiply(one)).add((two.multiply(three)));
answer[0][1]=(one.multiply(two)).add((two.multiply(four)));
answer[1][0]=(three.multiply(one)).add((four.multiply(three)));
answer[1][1]=(three.multiply(two)).add((four.multiply(four)));
}
for (i=0;i<Math.log10(n)/Math.log10(2);i++)
{
one=answer[0][0];
two=answer[0][1];
three=answer[1][0];
four=answer[1][1];
answer[0][0]=(one.multiply(one)).add((two.multiply(three)));
answer[0][1]=(one.multiply(two)).add((two.multiply(four)));
answer[1][0]=(three.multiply(one)).add((four.multiply(three)));
answer[1][1]=(three.multiply(two)).add((four.multiply(four)));
}
return answer;
}
public static BigInteger[][] MatrixMulty(BigInteger a[][] , BigInteger b[][] ){
BigInteger one,two,three,four;
one=a[0][0];
two=a[0][1];
three=a[1][0];
four=a[1][1];
a[0][0]=(one.multiply(b[0][0])).add((two.multiply(b[1][0])));
a[0][1]=(one.multiply(b[0][1])).add((two.multiply(b[1][1])));
a[1][0]=(three.multiply(b[0][0])).add((four.multiply(b[1][0])));
a[1][1]=(three.multiply(b[0][1])).add((four.multiply(b[1][1])));
return a;
}
}