Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 965 Bytes

power-of-numbers.md

File metadata and controls

40 lines (34 loc) · 965 Bytes

Power Of Numbers

Problem Link

Given a number and its reverse. Find that number raised to the power of its own reverse.

Note: As answers can be very large, print the result modulo 109 + 7.

Sample Input

2

Sample Output

4

Solution

class Solution {
    public:
    long long MOD = 1e9+7;
    long long power(long long N, long long R)
    {
        N %= MOD;
        long long ans = 1;
        while (R > 0) {
            if (R & 1) {
                ans = (ans * N) % MOD;
            }
            N = (N * N) % MOD;
            R >>= 1;
        }
        return ans;
    }
};

Accepted

image