ural 1133. Fibonacci Sequence math

来源:互联网 发布:程序员离职证明 编辑:程序博客网 时间:2024/05/18 01:13

1133. Fibonacci Sequence

Time limit: 1.0 second
Memory limit: 64 MB
Problem illustration
is an infinite sequence of integers that satisfies to Fibonacci conditionFi + 2 = Fi + 1 + Fi for any integer i. Write a program, which calculates the value ofFn for the given values of Fi and Fj.

Input

The input contains five integers in the following order: iFijFjn.
−1000 ≤ ijn ≤ 1000, i ≠ j,
−2·109 ≤ Fk ≤ 2·109 (k = min(ijn), …, max(ijn)).

Output

The output consists of a single integer, which is the value of Fn.

Sample

inputoutput
3 5 -1 4 5
12

Notes

In the example you are given: F3 = 5, F−1 = 4; you asked to find the value of F5. The following Fibonacci sequence can be reconstructed using known values:
…, F−1 = 4, F0 = −1, F1 = 3, F2 = 2, F3 = 5, F4 = 7, F5 = 12, …
Thus, the answer is: F5 = 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());}}