Advertisement
Guest User

Untitled

a guest
May 23rd, 2022
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. map<int, int>mpp;
  2. int recurse(int n, int x, int anss = 0)
  3. {
  4.  
  5.         string str = to_string(x);
  6.         if ((int)str.size() == n)return anss;
  7.  
  8.  
  9.         if (mpp.find(x) != mpp.end())return mpp[x];
  10.         set<int>mp2;
  11.         for0(i, (int)str.size())
  12.         {
  13.                 mp2.insert(str[i] - '0');
  14.  
  15.         }
  16.         int ans = 1e9;
  17.         for (auto it : mp2)
  18.         {
  19.                 if (it == 0 || it == 1)continue;
  20.                 ans = min(ans, recurse(n, x * it, anss + 1));
  21.         }
  22.         return mpp[x] = ans;
  23.  
  24. }
  25. void solve()
  26. {
  27.  
  28.         int n, x;
  29.         cin >> n >> x;
  30.         if (n == 1) {
  31.                 string str = to_string(x);
  32.                 if (str.size() > 1) {
  33.                         int z = count(all(str), '0');
  34.                         if (z) {cout << 1 << endl; return;}
  35.                 }
  36.  
  37.  
  38.         }
  39.         int ans = recurse(n, x);
  40.         if (ans == 1e9) {cout << -1 << endl;}
  41.         else cout << ans << endl;
  42.         return;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement