Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- long long N {}, K {};
- bool isThereLargerDigit(const std::vector<short int> &combination, const short int k);
- long long getNumberFromCombination(const std::vector<short int> &combination);
- long long howManyNumbers(std::vector<short int> combination);
- void changeCombination(std::vector<short int> &combination, const short int k);
- void sortCombination(std::vector<short int> &combination, const short int start = 0);
- std::vector<short int> generateNextCombination(std::vector<short int> result);
- std::vector<short int> generateVectorOfDigits();
- int main()
- {
- std::cin >> N >> K;
- std::cout << howManyNumbers(generateVectorOfDigits());
- return 0;
- }
- bool isThereLargerDigit(const std::vector<short int> &combination, const short int k) {
- const auto size {static_cast<short int>(combination.size())};
- for (auto i = k + 1; i < size; ++i) {
- if (combination[i] > combination[k]) {
- return true;
- }
- }
- return false;
- }
- long long getNumberFromCombination(const std::vector<short int> &combination) {
- long long result {}, multiplier {1};
- const auto size {static_cast<short int>(combination.size())};
- for (short int i = 0; i < size; ++i) {
- result += (multiplier * combination[i]);
- multiplier *= 10;
- }
- return result;
- }
- long long howManyNumbers(std::vector<short int> combination) {
- long long result {};
- while (combination.size() > 0) {
- if (getNumberFromCombination(combination) % K == 0) {
- ++result;
- }
- combination = generateNextCombination(combination);
- }
- return result;
- }
- void changeCombination(std::vector<short int> &combination, const short int k) {
- // na miejsce k wstawia najmniejszą z większych liczb stojących za k
- // pozostale liczby stojące za k sortuje rosnąco
- const auto size {static_cast<short int>(combination.size())};
- auto base {k + 1};
- auto minimum {10};
- short int index {};
- for (auto i = base; i < size; ++i) {
- if (combination[i] > combination[k]) {
- if (combination[i] < minimum) {
- minimum = combination[i];
- index = i;
- }
- }
- }
- std::swap(combination[k], combination[index]);
- sortCombination(combination, base);
- }
- void sortCombination(std::vector<short int> &combination, const short int start) {
- const auto size {static_cast<short int>(combination.size() - 1)};
- for (auto i = start; i < size; ++i) {
- for (auto j = start; j < size - i + start; ++j) {
- if (combination[j] > combination[j + 1]) {
- std::swap(combination[j], combination[j + 1]);
- }
- }
- }
- }
- std::vector<short int> generateNextCombination(std::vector<short int> result) {
- auto k {static_cast<short int>(result.size() - 2)};
- while (k >= 0) {
- if (isThereLargerDigit(result, k)) {
- changeCombination(result, k);
- return result;
- } else {
- --k;
- }
- }
- return std::vector<short int>{};
- }
- std::vector<short int> generateVectorOfDigits() {
- std::vector<short int> result {};
- while (N > 0) {
- result.push_back(N % 10);
- N /= 10;
- }
- sortCombination(result);
- return result;
- }
Add Comment
Please, Sign In to add comment