fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. unordered_map<int, int> mp;
  4. int n;
  5. int mi = INT_MAX;
  6. void dfs(long long num){
  7. string s = to_string(num);
  8.  
  9. if(s.size() >= n){
  10. mi = min(mi, mp[num]);
  11. return;
  12. }
  13. for(int i = 0; i < s.size(); i++){
  14. if(mp[(s[i] - '0') * num] || s[i] - '0' == 1) continue;
  15. mp[(s[i] - '0') * num] = mp[num] + 1;
  16. dfs((s[i] - '0') * num);
  17. }
  18.  
  19. }
  20. signed main(){
  21. long long x;
  22. cin >> n >> x;
  23. dfs(x);
  24. if(mi == INT_MAX){
  25. cout << -1;
  26. }else cout << mi;
  27. }
Success #stdin #stdout 0.01s 5288KB
stdin
13 42
stdout
13