Advertisement
adriweb

Untitled

Sep 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2.  
  3. $n = (int)fgets(STDIN);
  4.  
  5. for ($i=0; $i<$n; $i++)
  6. {
  7.     preg_match('/(\d+) (\d+) (\d+)/', fgets(STDIN), $matches);
  8.     $b = (int)$matches[1];
  9.     $l = (int)$matches[2];
  10.     $r = (int)$matches[3];
  11.     $goodCount = 0;
  12.  
  13.     for ($num = $l; $num <= $r; $num++)
  14.     {
  15.         $numIsGood = true;
  16.  
  17.         $conv = base_convert($num, 10, $b);
  18.         $counts = [];
  19.         foreach (str_split($conv) as $digit)
  20.         {
  21.             if (!isset($counts[$digit])) { $counts[$digit] = 0; }
  22.             $counts[$digit]++;
  23.         }
  24.  
  25.         foreach ($counts as $count)
  26.         {
  27.             if (($count % 2) !== 0) {
  28.                 $numIsGood = false;
  29.                 break;
  30.             }
  31.         }
  32.         if ($numIsGood) { $goodCount++; }
  33.     }
  34.     echo $goodCount . "\n";
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement