we can answer query in logarithmic time using any standard data structure for range queries but the thing is getting i th fiboncci number as i can be as big as 10^9… we should be able to calculate nth fibonacci number quickly.To do that we can use Matrix Exponentiation
just look at the above matrix where n th fibonacci number is corresponding entry in right side matrix.
let’s indicate . according to identity
so we are almost done we need to calculate Q^n-1 and take the first element of first line…
but how do we compute Q^n-1 here comes the Matrix Exponentiation
the above pic says it all…To calculate M^n we need to calculate M^n/2 and multiply it by itself and so on…it is very obvious that tree height is log n
I appreciate your logic and even I submitted with same logic but no avail of it . Because ith fibonacci number where i<=10^9 is itself very large and can’t fit even in long long integer in C++ . There will be overflow …
Its a question from samsung research institute, banglore’s hiring challenge currently being held on hackerearth . Somebody remove this blog from discussion forum.
Questions like these kill the fairness of the contests. A lot of time is spent in making a problem and these discussions throw open all the ideas behind it!
I don’t understand why people cannot wait for the contest to get over!