-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower Of Numbers.java
More file actions
87 lines (66 loc) · 1.54 KB
/
Power Of Numbers.java
File metadata and controls
87 lines (66 loc) · 1.54 KB
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
//{ Driver Code Starts
//Initial Template for Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class Main {
// compute reverse of a number
public static long rev(long n)
{
long rev_num = 0;
while(n > 0)
{
rev_num = rev_num*10 + n%10;
n = n/10;
}
return rev_num;
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
//testcases
int T=sc.nextInt();
while(T-->0)
{
Solution obj=new Solution();
int N;
//input N
N=sc.nextInt();
int R=0;
// reverse the given number n
R=(int)rev(N);
//power of the number to it's reverse
long ans=(long)obj.power(N,R);
System.out.println(ans);
}
}
}
// } Driver Code Ends
//User function Template for Java
/*
BASIC IDEA
for 2^10 we can do 2^5 * 2^5 so for even we are squaring the half one's result
but for odd
2^5 we can do 2 * 2^4 so here 2^4 follows the same 2^2 * 2^2 --> 2^1 * 2^1 ---> 2 * 2^0
*/
class Solution
{
long power(int N,int R)
{
//Your code here
int mod=1000000007;
long ans=0;
if(R==0)
return 1;
if(R%2==0)
{
ans=power(N,R/2);
ans=(ans*ans)%mod;
}
else
{
ans=N%mod;
ans=(ans*power(N,R-1)%mod)%mod;
}
return (ans)%mod;
}
}