ural 1133. Fibonacci Sequence math
来源:互联网 发布:程序员离职证明 编辑:程序博客网 时间:2024/05/18 01:13
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 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: i, Fi, j, Fj, n.
−1000 ≤ i, j, n ≤ 1000, i ≠ j,
−2·109 ≤ Fk ≤ 2·109 (k = min(i, j, n), …, max(i, j, n)).
−1000 ≤ i, j, n ≤ 1000, i ≠ j,
−2·109 ≤ Fk ≤ 2·109 (k = min(i, j, n), …, max(i, j, n)).
Output
The output consists of a single integer, which is the value of Fn.
Sample
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());}}
阅读全文
0 0
- ural 1133. Fibonacci Sequence math
- fibonacci sequence
- Fibonacci Sequence
- Fibonacci sequence
- Fibonacci sequence
- ural 1306. Sequence Median
- ural 1306. Sequence Median
- Ural 1306. Sequence Median
- URAL 1306. Sequence Median
- Ural 1306. Sequence Median
- Ural 1306 Sequence Median
- Sequence - URAL 1528 水题
- URAL 1528 Sequence
- ural Brackets Sequence (dp)
- ural 1306 Sequence Median
- URAL 1528Sequence
- SZ Fibonacci Sequence
- 1-2 Fibonacci sequence
- caffe win10 ssd训练和预测(CPU)
- mfcs100ud.lib(dllmodul.obj) : error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义
- JEESZ RestFul服务介绍
- 逆转字符串
- Eclipse导入已经存在WorkSpace里的工程
- ural 1133. Fibonacci Sequence math
- 线程调用中的一些理解
- PaperWeekly 第38期 | SQuAD综述
- Android关于OOM的解决方案
- js 事件对象
- REST 架构
- 【地图整饰】ArcMap添加地图标题及ArcEngine实现
- Codeforces Gravity Flip
- java判断字符串是正整数