c++ - Fibonacci Sequence ,binary search -
i trying solve following problem:
f infinite sequence of integers satisfies fibonacci condition f(i + 2) = f(i + 1) + f(i) integer i. write program, calculates value of f(n) given values of f(i) , f(j).
input: input contains 5 integers in following order: i, f(i), j, f(j), n. −1000 ≤ i, j, n ≤ 1000, ≠ j, −2·10^9 ≤ f(k) ≤ 2·10^9 (k = min(i, j, n), …, max(i, j, n)).
output: output consists of single integer, value of f(n).
i trying solve problem finding f(min(i,j)+1) using 2 neighbors find f(n). told can done implementing binary search on interval (-2*10^9,2*10^9) ,but don't understand how use binary search here.could give me hint or explain algorithm in short manner.
one way can think of that: lets < j , f(i) < f(j) want find f(i+1). know f(i) < f(i+1) < f(j) can binary search between [f(i),f(j)] - each time guess f(i+1) , checks if fits (no easy way around think) until correct value.
complexity - each iteration can take 2000 steps (worst case), , @ worst it's going take log(4*10^9) iterations 32 seems reasonable.
Comments
Post a Comment