Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя Abd_Codeforce

Автор Abd_Codeforce, история, 6 месяцев назад, По-английски

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.

  • Проголосовать: нравится
  • -26
  • Проголосовать: не нравится

»
6 месяцев назад, # |
  Проголосовать: нравится +28 Проголосовать: не нравится
if you are someone above newbie(Your upvote or comment of appreciation will really make me happy).