Fast Power Mod
PHP
Medium
4 views
Problem Description
Input a b m. Print (a^b) mod m.
Input Format
One line: a b m.
Output Format
One integer.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$a,$b,$m]=array_map('intval',preg_split('/\\s+/', $inputText));
$res=1%$m; $base=$a%$m; $e=$b;
while($e>0){
if($e&1) $res=($res*$base)%$m;
$base=($base*$base)%$m;
$e >>= 1;
}
echo $res;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!