Divide Without Slash

Divide Without Slash

Hard PHP PHP Operators 33 views
Explanation Complexity

Problem Statement

Input a b (a>=0, b>0). Print quotient and remainder without using / or %.

Input Format

One line: a b.

Output Format

Two integers: q r.

Example

17 5
3 2

Constraints

0

Input / Output Format

Input Format
One line: a b.
Output Format
Two integers: q r.
Constraints
0

Examples

Input:
17 5
Output:
3 2

Example Solution (Public)

PHP
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$a,$b]=array_map('intval',preg_split('/\\s+/', $inputText));
$q=0; $r=$a;
for($k=30;$k>=0;$k--){
  $step=$b<<$k;
  if($step<=0) continue;
  if($step<=$r){
    $r-=$step;
    $q |= (1<<$k);
  }
}
echo $q.' '.$r;
?>

Official Solution Code

<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$a,$b]=array_map('intval',preg_split('/\\s+/', $inputText));
$q=0; $r=$a;
for($k=30;$k>=0;$k--){
  $step=$b<<$k;
  if($step<=0) continue;
  if($step<=$r){
    $r-=$step;
    $q |= (1<<$k);
  }
}
echo $q.' '.$r;
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.