Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- map<int, int>mpp;
- int recurse(int n, int x, int anss = 0)
- {
- string str = to_string(x);
- if ((int)str.size() == n)return anss;
- if (mpp.find(x) != mpp.end())return mpp[x];
- set<int>mp2;
- for0(i, (int)str.size())
- {
- mp2.insert(str[i] - '0');
- }
- int ans = 1e9;
- for (auto it : mp2)
- {
- if (it == 0 || it == 1)continue;
- ans = min(ans, recurse(n, x * it, anss + 1));
- }
- return mpp[x] = ans;
- }
- void solve()
- {
- int n, x;
- cin >> n >> x;
- if (n == 1) {
- string str = to_string(x);
- if (str.size() > 1) {
- int z = count(all(str), '0');
- if (z) {cout << 1 << endl; return;}
- }
- }
- int ans = recurse(n, x);
- if (ans == 1e9) {cout << -1 << endl;}
- else cout << ans << endl;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement