Q:

How can I reduce the time complexity of the following loop?

for(i=0;i<N-2;i++)
       count=(count*10)%M;

Here, N can be upto 10^18 and M is (10^9 +7). Since this loop takes O(n) time to execute, I get TLE in my code. Any way to reduce the time complexity?

A:

The question is basically:

(count*a^b)%mod = ((count%mod)*((a^b)%mod))%mod

a = 10, b = 10^18
You can find ((a^b)%mod) using:
long long power(long long x, long long y, long long p)
{
    long long res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or 
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;  
    }
    return res;
}

Time Complexity of the power function is O(log y).
In your case count is a 1-digit number so we can simply multiply this with (count%mod), and finally take mod of the result. If count is a big number too, and can cause overflow then we can do:
long long mulmod(long long a, long long b, long long mod)
{
    long long res = 0; // Initialize result
    a = a % mod;
    while (b > 0)
    {
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;

        // Multiply 'a' with 2
        a = (a * 2) % mod;

        // Divide b by 2
        b /= 2;
    }

    // Return result
    return res % mod;
}

