Skip to content

Commit 88420eb

Browse files
authored
Merge pull request #182 from AlgorithmWithGod/suyeun84
[20250226] BOJ / 골드2 / 피보나치 수 6 / 김수연
2 parents a8ed4eb + 84552d4 commit 88420eb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
```

0 commit comments

Comments
 (0)