Given a number N and its reverse R. The task is to find the number obtained when the number is raised to the power of its own reverse. The answer can be very large, return the result modulo 109+7.
Examples:
Input : N = 2, R = 2
Output: 4
Explanation: Number 2 raised to the power of its reverse 2 gives 4 which gives 4 as a result after performing modulo 109+7
Input : N = 57, R = 75
Output: 262042770
Explanation: 5775 modulo 109+7 gives us the result as 262042770
Naive Approach:
The easiest way to solve this problem could be to traverse a loop from 1 to R(reverse) and multiply our answer with N in each iteration.
Follow the steps mentioned below to implement the idea:
- Create a variable (say ans) and initialize it to 1 to store the final result.
- Then, start a loop from 1 and it goes till R.
- Multiply the variable ans with N.
- Return the result ans with modulo of 1e9+7.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int PowerOfNum( int N, int R)
{
long long ans = 1, mod = 1e9 + 7;
for ( int i = 1; i <= R; i++) {
ans *= N;
ans %= mod;
}
return ans;
}
int main()
{
int N = 57, R = 75;
cout << PowerOfNum(N, R);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static long PowerOfNum( int N, int R)
{
long ans = 1 ;
long mod = 1000000000 + 7 ;
for ( int i = 1 ; i <= R; i++) {
ans *= N;
ans %= mod;
}
return ans;
}
public static void main(String[] args)
{
int N = 57 , R = 75 ;
System.out.println(PowerOfNum(N, R));
}
}
|
Python3
def PowerOfNum(N, R):
ans = 1
mod = 1e9 + 7
for i in range ( 1 ,R + 1 ):
ans * = N
ans % = mod
return ans
N = 57
R = 75
print ( int (PowerOfNum(N, R)))
|
C#
using System;
public class GFG{
public static long PowerOfNum( int N, int R)
{
long ans = 1;
long mod = 1000000000 + 7;
for ( int i = 1; i <= R; i++) {
ans *= N;
ans %= mod;
}
return ans;
}
static public void Main ()
{
int N = 57, R = 75;
Console.WriteLine(PowerOfNum(N, R));
}
}
|
Javascript
function PowerOfNum(N,R)
{
let ans = 1;
let mod = 1000000000 + 7;
for (let i = 1; i <= R; i++) {
ans *= N;
ans %= mod;
}
return ans;
}
let N = 57, R = 75;
console.log(PowerOfNum(N, R));
|
Time Complexity: O(R)
Auxiliary Space: O(1)
We can use the same approach as above but instead of an iterative loop, we can use recursion for the purpose.
C++
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long power( long long x, long long n)
{
if (n == 0)
return 1;
if (x == 0)
return 0;
return (x * power(x, n - 1)) % mod;
}
int main()
{
long long x = 57;
long long n = 75;
cout << (power(x, n));
}
|
C
#include <stdio.h>
long long power( long long x, long long n) {
if (n == 0)
return 1;
if (x == 0)
return 0;
return (x * power(x, n - 1)) % 1000000007;
}
int main() {
long long x = 57;
long long n = 75;
printf ( "%lld\n" , power(x, n));
return 0;
}
|
Java
public class Main {
static final long MOD = 1000000007 ;
static long power( long x, long n) {
if (n == 0 )
return 1 ;
if (x == 0 )
return 0 ;
return (x * power(x, n - 1 )) % MOD;
}
public static void main(String[] args) {
long x = 57 ;
long n = 75 ;
System.out.println(power(x, n));
}
}
|
Python3
def power(x, n):
if n = = 0 :
return 1
if x = = 0 :
return 0
return (x * power(x, n - 1 )) % 1000000007
x = 57
n = 75
print (power(x, n))
|
C#
using System;
public class Program {
const long MOD = 1000000007;
static long Power( long x, long n) {
if (n == 0)
return 1;
if (x == 0)
return 0;
return (x * Power(x, n - 1)) % MOD;
}
public static void Main( string [] args) {
long x = 57;
long n = 75;
Console.WriteLine(Power(x, n));
}
}
|
Javascript
function power(x, n) {
if (n == 0)
return 1;
if (x == 0)
return 0;
return (x * power(x, n - 1)) % 1000000007;
}
var x = 57;
var n = 75;
console.log(power(x, n));
|
Time Complexity: O(R)
Auxiliary Space: O(R)
The efficient way of solving this problem could be bit manipulation, just break the problem into small parts and solve them here the concept of binary exponentiation method will be used.
- Every number can be written as the sum of powers of 2
- Traverse through all the bits of a number from LSB (Least Significant Bit) to MSB (Most Significant Bit) in O(log N) time.
Follow the steps mentioned below to implement the idea:
- First, create a variable (say ans) and initialize it to 1 to store the result.
- Then, check if the given reverse number is odd or not.
- If yes, then multiply the answer with pow ans = (ans * pow)%mod.
- Then, multiply the pow with pow i.e., pow = (pow*pow).
- Start shifting right by R = R/2.
- Finally, return the ans as result.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
long long power( int num, int rev)
{
long long ans = 1;
long long mod = 1e9 + 7;
long long pow = num * 1LL;
while (rev > 0) {
if (rev & 1) {
ans = (ans * pow ) % mod;
}
pow = ( pow * pow ) % mod;
rev >>= 1;
}
return ans;
}
int main()
{
int N = 57, R = 75;
cout << power(N, R) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 57 , R = 75 ;
System.out.println(power(N, R));
}
static long power( int num, int rev)
{
long ans = 1 ;
long mod = 1000000007 , pow = num * 1L;
while (rev > 0 ) {
if (rev % 2 == 1 ) {
ans = (ans * pow) % mod;
}
pow = (pow * pow) % mod;
rev >>= 1 ;
}
return ans;
}
}
|
Python3
def power(num, rev):
ans = 1
mod = 1000000007
pow = num * 1
while (rev > 0 ) :
if (rev % 2 = = 1 ) :
ans = (ans * pow ) % mod
pow = ( pow * pow ) % mod
rev >> = 1
return ans
if __name__ = = "__main__" :
N = 57
R = 75
print (power(N, R))
|
C#
using System;
public class GFG {
static public void Main()
{
int N = 57, R = 75;
Console.WriteLine(power(N, R));
}
static long power( int num, int rev)
{
long ans = 1;
long mod = 1000000007, pow = num * 1L;
while (rev > 0) {
if (rev % 2 == 1) {
ans = (ans * pow) % mod;
}
pow = (pow * pow) % mod;
rev >>= 1;
}
return ans;
}
}
|
Javascript
const mod = 1000000007;
function power(num, rev) {
let ans = 1;
let pow = num * 1;
while (rev > 0) {
if (rev % 2 == 1) {
ans = (ans * pow) % mod;
}
pow = (pow * pow) % mod;
rev >>= 1;
}
return ans;
}
const N = 57, R = 75;
console.log(power(N, R));
|
Time Complexity: O(log R)
Auxiliary Space: O(1)