Advertisement
RaFiN_

nth fibonacci number

Sep 8th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.36 KB | None | 0 0
  1. /* nth fibonacci number */
  2. void fib(ll n, ll&x, ll&y){
  3.     if(n==0){
  4.         x = 0;
  5.         y = 1;
  6.         return ;
  7.     }
  8.      
  9.     if(n&1){
  10.         fib(n-1, y, x);
  11.         y=(y+x)%mod;
  12.     }else{
  13.         ll a, b;
  14.         fib(n>>1, a, b);
  15.         y = (a*a+b*b)%mod;
  16.         x = (a*b + a*(b-a+mod))%mod;
  17.     }
  18. }
  19. //answer in x. no map, time — logN.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement