Fast power (pow)
Java
Hard
6 views
Problem Description
Task: compute a^b using fast exponentiation (b >= 0).
Output Format
Return value
Constraints
Use long to reduce overflow risk.
Official Solution
static long pow(long a,int b){long res=1;long base=a;int e=b;while(e>0){if((e&1)==1) res*=base;base*=base;e>>=1;}return res;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!