-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacciModified.java
48 lines (33 loc) · 1.39 KB
/
fibonacciModified.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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the fibonacciModified function below.
static BigInteger fibonacciModified(int t1, int t2, int n) {
BigInteger[] arr = new BigInteger[n]; // using bigInteger because answer becames very big
arr[0]=BigInteger.ZERO; // even though using bigInteger all test cases are not solved (look for this issue) solved with python
arr[1]=BigInteger.ONE;
for(int i=2; i < n; i++)
{
arr[i] = arr[i-2].add(arr[i-1].multiply(arr[i-1]));
}
return arr[n-1];
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] t1T2n = scanner.nextLine().split(" ");
int t1 = Integer.parseInt(t1T2n[0]);
int t2 = Integer.parseInt(t1T2n[1]);
int n = Integer.parseInt(t1T2n[2]);
BigInteger result = fibonacciModified(t1, t2, n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}