-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathPower_of_numbers.cpp
99 lines (84 loc) · 1.87 KB
/
Power_of_numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Description :
Given a number. Find and its reverse that number raised to the power of its own reverse.
As answers can be very large, print the result modulo 1000000007.
*/
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
class Solution
{
public:
//function to find the power with respect to mod
long long modular_func(long long n, long long R)
{
if (n == 0)
{
return 0;
}
if (R == 0)
{
return 1;
}
long long y;
// If we find R is even
if (R % 2 == 0)
{
y = modular_func(n, R / 2);
y = (y * y) % mod;
}
// If we find R is odd
else
{
y = n % mod;
y = (y * modular_func(n, R - 1) % mod) % mod;
}
// finally return the answer
return ((y + mod) % mod);
}
long long power(int N, int R)
{
//using recursion
return modular_func(N, R) % mod;
}
};
//fucntion is used to reverse the number
long long rev(long long n)
{
long long rev_num = 0;
while (n > 0)
{
rev_num = rev_num * 10 + n % 10;
n = n / 10;
}
return rev_num;
}
int main()
{
// user input number
//using long long variables and function to avoid integer overflow
long long num;
cout << "Enter the number : " << endl;
cin >> num;
// R is used to hold reverse of the original number
long long R = 0;
R = rev(num);
Solution obj;
// calling function
long long ans = obj.power(num, R);
cout << "Number raised to the power of its own reverse is : " << endl;
cout << ans << endl;
}
/*
Time complexity : O(log N)
Space complexity : O(log N)
*/
/*
Test Case :
Input ;
Enter the number :
12
Output ;
Number raised to the power of its own reverse is :
864354781
*/