ural 1133. Fibonacci Sequence math

1133. Fibonacci Sequence

Time limit: 1.0 second
Memory limit: 64 MB
Problem illustration
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:  iFijFjn.
−1000 ≤  ijn ≤ 1000,  i ≠  j,
−2·10 9 ≤  Fk ≤ 2·10 9 ( k = min( ijn), …, max( ijn)).

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());
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值