MeehoweCK

Untitled

Nov 24th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. long long N {}, K {};
  5.  
  6. bool isThereLargerDigit(const std::vector<short int> &combination, const short int k);
  7. long long getNumberFromCombination(const std::vector<short int> &combination);
  8. long long howManyNumbers(std::vector<short int> combination);
  9. void changeCombination(std::vector<short int> &combination, const short int k);
  10. void sortCombination(std::vector<short int> &combination, const short int start = 0);
  11. std::vector<short int> generateNextCombination(std::vector<short int> result);
  12. std::vector<short int> generateVectorOfDigits();
  13.  
  14. int main()
  15. {
  16.     std::cin >> N >> K;
  17.     std::cout << howManyNumbers(generateVectorOfDigits());
  18.     return 0;
  19. }
  20.  
  21. bool isThereLargerDigit(const std::vector<short int> &combination, const short int k) {
  22.     const auto size {static_cast<short int>(combination.size())};
  23.     for (auto i = k + 1; i < size; ++i) {
  24.         if (combination[i] > combination[k]) {
  25.             return true;
  26.         }
  27.     }
  28.     return false;
  29. }
  30.  
  31. long long getNumberFromCombination(const std::vector<short int> &combination) {
  32.     long long result {}, multiplier {1};
  33.     const auto size {static_cast<short int>(combination.size())};
  34.     for (short int i = 0; i < size; ++i) {
  35.         result += (multiplier * combination[i]);
  36.         multiplier *= 10;
  37.     }
  38.     return result;
  39. }
  40.  
  41. long long howManyNumbers(std::vector<short int> combination) {
  42.     long long result {};
  43.     while (combination.size() > 0) {
  44.         if (getNumberFromCombination(combination) % K == 0) {
  45.             ++result;
  46.         }
  47.         combination = generateNextCombination(combination);
  48.     }
  49.     return result;
  50. }
  51.  
  52. void changeCombination(std::vector<short int> &combination, const short int k) {
  53.     // na miejsce k wstawia najmniejszą z większych liczb stojących za k
  54.     // pozostale liczby stojące za k sortuje rosnąco
  55.     const auto size {static_cast<short int>(combination.size())};
  56.     auto base {k + 1};
  57.     auto minimum {10};
  58.     short int index {};
  59.     for (auto i = base; i < size; ++i) {
  60.         if (combination[i] > combination[k]) {
  61.             if (combination[i] < minimum) {
  62.                 minimum = combination[i];
  63.                 index = i;
  64.             }
  65.         }
  66.     }
  67.     std::swap(combination[k], combination[index]);
  68.     sortCombination(combination, base);
  69. }
  70.  
  71. void sortCombination(std::vector<short int> &combination, const short int start) {
  72.     const auto size {static_cast<short int>(combination.size() - 1)};
  73.     for (auto i = start; i < size; ++i) {
  74.         for (auto j = start; j < size - i + start; ++j) {
  75.             if (combination[j] > combination[j + 1]) {
  76.                 std::swap(combination[j], combination[j + 1]);
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. std::vector<short int> generateNextCombination(std::vector<short int> result) {
  83.     auto k {static_cast<short int>(result.size() - 2)};
  84.     while (k >= 0) {
  85.         if (isThereLargerDigit(result, k)) {
  86.             changeCombination(result, k);
  87.             return result;
  88.         } else {
  89.             --k;
  90.         }
  91.     }
  92.     return std::vector<short int>{};
  93. }
  94.  
  95. std::vector<short int> generateVectorOfDigits() {
  96.     std::vector<short int> result {};
  97.     while (N > 0) {
  98.         result.push_back(N % 10);
  99.         N /= 10;
  100.     }
  101.     sortCombination(result);
  102.     return result;
  103. }
Add Comment
Please, Sign In to add comment