Advertisement
adriweb

Untitled

Sep 24th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4.  
  5. std::string intToBase(int x, int base)
  6. {
  7.     if (x < base) {
  8.         return std::to_string(x);
  9.     }
  10.     return intToBase(x/base, base) + std::to_string(x % base);
  11. }
  12.  
  13. int main()
  14. {
  15.     int n;
  16.     std::cin >> n; std::cin.get();
  17.  
  18.     for (int i=0; i<n; i++)
  19.     {
  20.         int b, l, r;
  21.         std::cin >> b >> l >> r; std::cin.get();
  22.  
  23.         int goodCount = 0;
  24.         for (int num = l; num <= r; num++)
  25.         {
  26.             bool numIsGood = true;
  27.             std::unordered_map<char, int> counts;
  28.  
  29.             for (const char digit : intToBase(num, b))
  30.             {
  31.                 if (counts.find(digit) == counts.end()) {
  32.                     counts[digit] = 0;
  33.                 }
  34.                 counts[digit]++;
  35.             }
  36.  
  37.             for (const auto& key_val : counts)
  38.             {
  39.                 if ((key_val.second % 2) != 0) {
  40.                     numIsGood = false;
  41.                     break;
  42.                 }
  43.             }
  44.             if (numIsGood) { goodCount++; }
  45.         }
  46.  
  47.         std::cout << goodCount << std::endl;
  48.     }
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement