MY FIRST BLOG( FIBONACCI NUMBERS !!! )

Revision en2, by Abd_Codeforce, 2024-01-20 19:29:38

Hello Everyone. Finally I reached 1000 on Codeforces so I decided to post my first Blog. But rather than just talking I wanted to post something helpful for the community in my first blog hence I came up with something for you all(Specifically Newbie's because I am also one).For all the people rated more than me I would appreciate your support (pls don't say newbie opinions don't matter)

So a lot of the newbie don't know how to calculate the nth term of fibonacci series modulo 10^9 + 7 when n is very large like N<=10^18 because it require computation in logN time and also some basic modular Arithmetic so here I am sharing the code to calculate that U can use it as a snippet and make a file of it so that u can import it anytime.

Here it is

define mod 1e9+7

define ll long long

ll modAdd(ll a,ll b){ ll p = (a%mod + b%mod)%mod; return p; }

ll modSub(ll a,ll b){ ll p = (a%mod — b%mod)%mod; if(p<0) p+=mod; return p; }

ll modMul(ll a,ll b){ ll p = (a%mod * b%mod)%mod; return p; }

pair<ll, ll> fib (ll n) { if (n == 0) return {0, 1};

auto p = fib(n >> 1);
ll c = modMul(p.first,modSub(modMul(2,p.second),p.first));
ll d = modAdd(modMul(p.first,p.first),modMul(p.second,p.second));
if (n & 1)
    return {d, modAdd(c,d)};
else
    return {c, d};

}

just call the fib function with N as parameter and it will return a pair of integers {Fn,Fn+1} and u can say fib(N).first to get the value.

If u want the explanation of the code pls comment below. And if you want to make my day special pls upvote this blog.

P.S if you are someone above newbie(Your upvote or comment of appreciation will really make me happy).

Tags fibonacci, first post, 1000, newbie

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English Abd_Codeforce 2024-01-21 11:39:44 104
en6 English Abd_Codeforce 2024-01-20 19:34:28 21 Reverted to en2
en5 English Abd_Codeforce 2024-01-20 19:34:20 8 Reverted to en3
en4 English Abd_Codeforce 2024-01-20 19:32:52 8
en3 English Abd_Codeforce 2024-01-20 19:32:05 21
en2 English Abd_Codeforce 2024-01-20 19:29:38 17
en1 English Abd_Codeforce 2024-01-20 19:27:39 1786 Initial revision (published)