bhupixb

Fibonnaci numbers

Jul 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. typedef long long int ll;
  4. unordered_map < ll, ll > f;
  5. const ll mod = 1e9 + 7;
  6. unordered_map < ll, ll > :: iterator it;
  7. ll fib(ll n){
  8.  
  9. if(n == 0)return 0;
  10. if(f[n])return f[n];
  11.  
  12. ll k = (n+1)/2;
  13.  
  14. if(n%2 == 0){
  15. return f[n] = ((2 * fib(k-1) % mod + fib(k) % mod) % mod * fib(k) % mod) % mod;
  16. }
  17. else{
  18. return f[n] = (fib(k) * fib(k) % mod + fib(k-1) * fib(k-1) % mod) % mod;
  19. }
  20.  
  21. }
  22. int main(){
  23. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  24. ll n, gcd, num, i;
  25. f[0] = 0;
  26. f[1] = 1;
  27. f[2] = 1;
  28. cin >> n;
  29. cout << fib(n);
  30. return 0;
  31. }
Add Comment
Please, Sign In to add comment