ProblemFind (a^b)%M, wherea = Nth non-fibonacci numberb = Nth fibonacci number modulo MM = 1000000007Consider fibonacci series 1,1,2,3,.....INPUTFirst line contains T , the number of test cases.Each next T lines contains a  number N.OUTPUTPrint T lines of output where each line corresponds to the required answer. EXAMPLEInput:3321Output:4964Constraints1<=T<=1000001<=N<=10^7Here is my code for the problem link#include <cstdio>#include <unordered_map>using namespace std;typedef long long int ll;ll M = 1000000007;ll mulmod(ll a, ll b)       //modular multiplication{    ll x = 0;    ll y = a%M;    while(b>0)    {        if(b%2)            x = (x+y)%M;        y = (y+y)%M;        b/=2;    }    return x%M;}ll modulo(ll a, ll b)       //modular exponentiation{    ll x = 1;    ll y = a;    while(b)    {        if(b%2)            x = mulmod(x,y);        y = mulmod(y,y);        b/=2;    }    return x%M;}unordered_map<ll,ll> Fib;ll fibo(ll n)       //n+1 th fibonacci number{    if(n<2)        return 1;    if(Fib.find(n) != Fib.end())        return Fib[n];    Fib[n] = (fibo((n+1) / 2)*fibo(n/2) + fibo((n-1) / 2)*fibo((n-2) / 2)) % M;    return Fib[n];}ll nonfibo(ll n)        //nth non-fibonacci number{    ll a = 1, b = 2, c = 3;    while(n>0)    {        a = b;        b = c;        c = a+b;        n-=(c-b-1);    }    n+=(c-b-1);    return n + b;}int main(){    ll t;    scanf(%lld,&t);    while(t--)    {        ll n;        scanf(%lld,&n);        printf(%lld\n,modulo(nonfibo(n),fibo(n-1)));    }    return 0;}It exceeds the time limit, how do I improve the code?
SPOJ - POWFIB (Fibo and non fibo) Time Limit Exceeds
c++;programming challenge;time limit exceeded;fibonacci sequence
Here, you are using modular exponentiation and modular multiplication, which are fast enough.Your nonfibo function is also taking only O(lg(n)) time.But your method for computing nth fibonacci number is not much efficient. Try using this Linear Recurrence Solving Method. 