Use this to learn the idea, then write your own version.
123 45class Solution {6private:7 struct TupleHash {8 template <typename... T>9 std::size_t operator()(const std::tuple<T...>& t) const {10 return apply([](const auto&... args) {11 std::size_t seed = 0;12 ((seed ^= std::hash<std::decay_t<decltype(args)>>{}(args) +13 0x9e3779b9 + (seed << 6) + (seed >> 2)), ...);14 return seed;15 }, t);16 }17 };18 19public:20 int countSequences(vector<int>& nums, long long k) {21 static const unordered_map<int, tuple<int, int, int>> LOOKUP = {22 {1, {0, 0, 0}},23 {2, {1, 0, 0}},24 {3, {0, 1, 0}},25 {4, {2, 0, 0}},26 {5, {0, 0, 1}},27 {6, {1, 1, 0}}28 };29 30 const auto& factors = [](int64_t x) {31 int cnt2 = 0;32 while (x % 2 == 0) {33 x /= 2;34 ++cnt2;35 }36 int cnt3 = 0;37 while (x % 3 == 0) {38 x /= 3;39 ++cnt3;40 }41 int cnt5 = 0;42 while (x % 5 == 0) {43 x /= 5;44 ++cnt5;45 }46 return x == 1 ? tuple(cnt2, cnt3, cnt5) : tuple(-1, -1, -1);47 };48 49 const auto& count = [](const auto& nums) {50 unordered_map<tuple<int, int, int>, int, TupleHash> dp;51 dp[{0, 0, 0}] = 1;52 for (const auto& x : nums) {53 unordered_map<tuple<int, int, int>, int, TupleHash> new_dp;54 const auto& [d2, d3, d5] = LOOKUP.at(x);55 for (const auto& [k, c] : dp) {56 const auto& [c2, c3, c5] = k;57 new_dp[{c2, c3, c5}] += c;58 new_dp[{c2 + d2, c3 + d3, c5 + d5}] += c;59 new_dp[{c2 - d2, c3 - d3, c5 - d5}] += c;60 }61 dp = move(new_dp);62 }63 return dp;64 };65 66 const auto& [c2, c3, c5] = factors(k);67 if (c2 == -1) {68 return 0;69 }70 auto left = count(vector<int>(cbegin(nums), cbegin(nums) + (size(nums) / 2)));71 auto right = count(vector<int>(cbegin(nums) + (size(nums) / 2), cend(nums)));72 int result = 0;73 for (const auto& [k, d] : left) {74 const auto& [d2, d3, d5] = k;75 result += d * right[{c2 - d2, c3 - d3, c5 - d5}];76 }77 return result;78 }79};80 81828384class Solution2 {85private:86 struct TupleHash {87 template <typename... T>88 std::size_t operator()(const std::tuple<T...>& t) const {89 return apply([](const auto&... args) {90 std::size_t seed = 0;91 ((seed ^= std::hash<std::decay_t<decltype(args)>>{}(args) +92 0x9e3779b9 + (seed << 6) + (seed >> 2)), ...);93 return seed;94 }, t);95 }96 };97 98public:99 int countSequences(vector<int>& nums, long long k) {100 static const unordered_map<int, tuple<int, int, int>> LOOKUP = {101 {1, {0, 0, 0}},102 {2, {1, 0, 0}},103 {3, {0, 1, 0}},104 {4, {2, 0, 0}},105 {5, {0, 0, 1}},106 {6, {1, 1, 0}}107 };108 109 const auto& factors = [](int64_t x) {110 int cnt2 = 0;111 while (x % 2 == 0) {112 x /= 2;113 ++cnt2;114 }115 int cnt3 = 0;116 while (x % 3 == 0) {117 x /= 3;118 ++cnt3;119 }120 int cnt5 = 0;121 while (x % 5 == 0) {122 x /= 5;123 ++cnt5;124 }125 return x == 1 ? tuple(cnt2, cnt3, cnt5) : tuple(-1, -1, -1);126 };127 128 const auto& count = [](const auto& nums) {129 unordered_map<tuple<int, int, int>, int, TupleHash> dp;130 dp[{0, 0, 0}] = 1;131 for (const auto& x : nums) {132 unordered_map<tuple<int, int, int>, int, TupleHash> new_dp;133 const auto& [d2, d3, d5] = LOOKUP.at(x);134 for (const auto& [k, c] : dp) {135 const auto& [c2, c3, c5] = k;136 new_dp[{c2, c3, c5}] += c;137 new_dp[{c2 + d2, c3 + d3, c5 + d5}] += c;138 new_dp[{c2 - d2, c3 - d3, c5 - d5}] += c;139 }140 dp = move(new_dp);141 }142 return dp;143 };144 145 const auto& [c2, c3, c5] = factors(k);146 if (c2 == -1) {147 return 0;148 }149 auto dp = count(nums);150 return dp[{c2, c3, c5}];151 }152};153