Advertisement
Oibek

Fibonacci - Formulae

Feb 4th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. const ll MOD = 1000000007;
  7. map<ll, ll> ma;
  8.  
  9. ll fib(ll n)
  10. {
  11.     if (n==0 or n==1) return 1;
  12.  
  13.     if (ma.find(n) != ma.end()) return ma[n];
  14.     ma[n] = (fib((n+1)/2) * fib(n/2) + fib((n-1)/2) * fib((n-2)/2))%MOD;
  15.     return ma[n];
  16. }
  17.  
  18. int main() {
  19.     ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
  20.  
  21.     while (1)
  22.     {
  23.        ll n;
  24.        cin >> n;
  25.        if (n==0) cout << 0 << endl;
  26.        else cout << fib(n-1) << endl;
  27.     }
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement