Use this to learn the idea, then write your own version.
1class Solution {2 public:3 string smallestNumber(string num, long long t) {4 const auto [primeCount, isDivisible] = getPrimeCount(t);5 if (!isDivisible)6 return "-1";7 8 const unordered_map<int, int> factorCount = getFactorCount(primeCount);9 if (sumValues(factorCount) > num.length())10 return consturct(factorCount);11 12 unordered_map<int, int> primeCountPrefix = getPrimeCount(num);13 int firstZeroIndex = num.find('0');14 if (firstZeroIndex == string::npos) {15 firstZeroIndex = num.length();16 if (isSubset(primeCount, primeCountPrefix))17 return num;18 }19 20 for (int i = num.length() - 1; i >= 0; --i) {21 const int d = num[i] - '0';22 23 primeCountPrefix = substract(primeCountPrefix, kFactorCounts.at(d));24 const int spaceAfterThisDigit = num.length() - 1 - i;25 if (i > firstZeroIndex)26 continue;27 for (int biggerDigit = d + 1; biggerDigit < 10; ++biggerDigit) {28 29 const unordered_map<int, int> factorsAfterReplacement =30 getFactorCount(substract(substract(primeCount, primeCountPrefix),31 kFactorCounts.at(biggerDigit)));32 33 if (sumValues(factorsAfterReplacement) <= spaceAfterThisDigit) {34 35 const int fillOnes =36 spaceAfterThisDigit - sumValues(factorsAfterReplacement);37 return num.substr(0, i) + 38 to_string(biggerDigit) + 39 string(fillOnes, '1') + 40 consturct(factorsAfterReplacement);41 }42 }43 }44 45 46 47 const unordered_map<int, int> factorsAfterExtension =48 getFactorCount(primeCount);49 return string(num.length() + 1 - sumValues(factorsAfterExtension), '1') +50 consturct(factorsAfterExtension);51 }52 53 private:54 constexpr static unordered_map<int, unordered_map<int, int>> kFactorCounts = {55 {0, {}}, {1, {}}, {2, {{2, 1}}}, {3, {{3, 1}}},56 {4, {{2, 2}}}, {5, {{5, 1}}}, {6, {{2, 1}, {3, 1}}}, {7, {{7, 1}}},57 {8, {{2, 3}}}, {9, {{3, 2}}}};58 59 60 pair<unordered_map<int, int>, bool> getPrimeCount(long t) {61 unordered_map<int, int> count{{2, 0}, {3, 0}, {5, 0}, {7, 0}};62 for (const int prime : {2, 3, 5, 7}) {63 while (t % prime == 0) {64 t /= prime;65 ++count[prime];66 }67 }68 return {count, t == 1};69 }70 71 72 unordered_map<int, int> getPrimeCount(const string& num) {73 unordered_map<int, int> count{{2, 0}, {3, 0}, {5, 0}, {7, 0}};74 for (const char d : num)75 for (const auto& [prime, freq] : kFactorCounts.at(d - '0'))76 count[prime] += freq;77 return count;78 }79 80 unordered_map<int, int> getFactorCount(const unordered_map<int, int>& count) {81 unordered_map<int, int> res;82 83 const int count8 = count.at(2) / 3;84 const int remaining2 = count.at(2) % 3;85 86 const int count9 = count.at(3) / 2;87 int count3 = count.at(3) % 2;88 89 int count4 = remaining2 / 2;90 int count2 = remaining2 % 2;91 92 int count6 = 0;93 if (count2 == 1 && count3 == 1) {94 count2 = 0;95 count3 = 0;96 count6 = 1;97 }98 99 if (count3 == 1 && count4 == 1) {100 count2 = 1;101 count6 = 1;102 count3 = 0;103 count4 = 0;104 }105 return unordered_map<int, int>{106 {2, count2}, {3, count3}, {4, count4}, {5, count.at(5)},107 {6, count6}, {7, count.at(7)}, {8, count8}, {9, count9}};108 }109 110 string consturct(const unordered_map<int, int>& factors) {111 string res;112 for (int digit = 2; digit < 10; ++digit)113 res += string(factors.at(digit), '0' + digit);114 return res;115 }116 117 118 bool isSubset(const unordered_map<int, int>& a,119 const unordered_map<int, int>& b) {120 for (const auto& [key, value] : a)121 if (b.at(key) < value)122 return false;123 return true;124 }125 126 127 unordered_map<int, int> substract(unordered_map<int, int> a,128 const unordered_map<int, int>& b) {129 for (const auto& [key, value] : b)130 a[key] = max(0, a[key] - value);131 return a;132 }133 134 135 int sumValues(const unordered_map<int, int>& count) {136 return accumulate(137 count.begin(), count.end(), 0,138 [](int acc, const pair<int, int>& p) { return acc + p.second; });139 }140};141