-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpow.cpp
More file actions
27 lines (21 loc) · 755 Bytes
/
pow.cpp
File metadata and controls
27 lines (21 loc) · 755 Bytes
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
long recursive(long x,long n, long d){
long ret ;
if ( x == 0)
return 0 ;
else if ( n == 0 )
return 1 ;
else if ( n == 1 )
ret = x ;
else if ( n%2 ){ // odd
ret = ( x * recursive ( ( x%d * x%d ) %d , n/2 , d ) ) % d;
}
else ret = recursive ( (x%d * x%d ) %d , n/2 , d ) % d ;
return ret < 0 ? ret + d : ret ;
}
int Solution::pow(int x, int n, int d) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
return (int) recursive ( x , n , d ) ;
}