XOR on Range
PHP
Hard
5 views
Problem Description
Given L and R, print XOR of all integers in [L,R].
Input Format
One line: L R.
Output Format
One integer.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$L,$R]=array_map('intval',preg_split('/\\s+/', $inputText));
function xorTo($n){
$r=$n & 3;
if($r===0) return $n;
if($r===1) return 1;
if($r===2) return $n+1;
return 0;
}
echo (xorTo($R) ^ xorTo($L-1));
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!