1133. Fibonacci Sequence
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
is an infinite sequence of integers that satisfies to Fibonacci condition
F
i + 2 =
F
i + 1 +
Fi for any integer
i. Write a program, which calculates the value of
Fn for the given values of
Fi and
Fj.
Input
The input contains five integers in the following order:
i,
Fi,
j,
Fj,
n.
−1000 ≤ i, j, n ≤ 1000, i ≠ j,
−2·10 9 ≤ Fk ≤ 2·10 9 ( k = min( i, j, n), …, max( i, j, n)).
−1000 ≤ i, j, n ≤ 1000, i ≠ j,
−2·10 9 ≤ Fk ≤ 2·10 9 ( k = min( i, j, n), …, max( i, j, n)).
Output
The output consists of a single integer, which is the value of
Fn.
Sample
input | output |
---|---|
3 5 -1 4 5 | 12 |
Notes
In the example you are given:
F
3 = 5,
F
−1 = 4; you asked to find the value of
F
5. The following Fibonacci sequence can be reconstructed using known values:
…,
F
−1 = 4,
F
0 = −1,
F
1 = 3,
F
2 = 2,
F
3 = 5,
F
4 = 7,
F
5 = 12, …
Thus, the answer is:
F
5 = 12.
Problem Source: Quarterfinal, Central region of Russia, Rybinsk, October 17-18 2001
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Task().solve();
}
}
class Task {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
void solve() {
int i = in.nextInt() ;
BigInteger fi = in.nextBigInteger() ;
int j = in.nextInt() ;
BigInteger fj = in.nextBigInteger() ;
int n = in.nextInt() ;
if(i > j){
int t = i ;
i = j ;
j = t ;
BigInteger ts = fi ;
fi = fj ;
fj = ts ;
}
BigInteger[][] a = {{BigInteger.ONE , BigInteger.ONE},{BigInteger.ONE,BigInteger.ZERO}} ;
for(int k = 1 ; k < j - i ; k++){
BigInteger[][] c = new BigInteger[2][2] ;
c[0][0] = a[0][0].add(a[0][1]) ;
c[0][1] = a[0][0] ;
c[1][0] = a[1][0].add(a[1][1]) ;
c[1][1] = a[1][0] ;
a = c ;
}
BigInteger fi_1 = (fj.subtract(a[0][0].multiply(fi) )).divide(a[0][1]) ;
BigInteger fn = BigInteger.valueOf(-1) ;
if(i-1 == n){
fn = fi_1 ;
}
else if(i == n){
fn = fi ;
}
else if(i < n){
BigInteger f = fi_1 ;
BigInteger s = fi ;
for(int k = i+1 ; k <= n ; k++){
BigInteger fk = f.add(s) ;
f = s ;
s = fk ;
}
fn = s ;
}
else{
BigInteger f = fi ;
BigInteger s = fi_1 ;
for(int k = i-2 ; k >= n ; k--){
BigInteger fk = f.subtract(s) ;
f = s ;
s = fk ;
}
fn = s ;
}
out.println(fn) ;
out.flush();
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = new StringTokenizer("");
}
private void eat(String s) {
tokenizer = new StringTokenizer(s);
}
public String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextInts(int n){
int[] nums = new int[n] ;
for(int i = 0 ; i < n ; i++){
nums[i] = nextInt() ;
}
return nums ;
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}