File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+ final static int mod = 1000000007 ;
7+ static HashMap<Long , Long > map;
8+
9+ public static void main (String [] args ) throws Exception {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ long N = Long . parseLong(br. readLine());
12+ map = new HashMap<> ();
13+
14+ map. put((long ) 1 , (long ) 1 );
15+ map. put((long ) 2 , (long ) 1 );
16+
17+ long answer = fibo(N );
18+
19+ System . out. println(answer);
20+ }
21+
22+ static long fibo (long N ) {
23+ if (map. containsKey(N )) return map. get(N );
24+ long a, b, c;
25+ if (N % 2 == 1 ) {
26+ a = fibo(N / 2 + 1 );
27+ b = fibo(N / 2 );
28+ map. put(N , ((a% mod)* (a% mod)% mod+ (b% mod)* (b% mod)% mod)% mod);
29+ } else {
30+ a = fibo(N / 2 + 1 );
31+ b = fibo(N / 2 );
32+ c = fibo(N / 2 - 1 );
33+ map. put(N , ((a% mod)* (b% mod)% mod+ (b% mod)* (c% mod)% mod)% mod);
34+ }
35+
36+ return map. get(N );
37+ }
38+ }
39+ ```
You can’t perform that action at this time.
0 commit comments