Advertisement
Josif_tepe

Untitled

Feb 6th, 2023
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10.     ios_base::sync_with_stdio(false);
  11.     string s;
  12.     cin >> s;
  13.    
  14.     vector<bool> digits(10, false);
  15.     for(int i = 0; i < (int) s.size(); i++) {
  16.         digits[s[i] - '0'] = true;
  17.     }
  18.     int n;
  19.     cin >> n;
  20.    
  21.     vector<int> dp(5005, 1e9);
  22.    
  23.     for(int i = 0; i <= 5000; i++) {
  24.         int tmp = i;
  25.         bool ok = true;
  26.         int cnt = 0;
  27.         while(tmp > 0) {
  28.             int digit = tmp % 10;
  29.             if(!digits[digit]) {
  30.                 ok = false;
  31.                 break;
  32.             }
  33.             tmp /= 10;
  34.             cnt++;
  35.         }
  36.         if(ok) {
  37.             dp[i] = cnt;
  38.         }
  39.     }
  40.    
  41.  
  42.    
  43.     for(int i = 0; i <= 5000; i++) {
  44.         for(int j = 0; j <= 5000; j++) {
  45.                 if(dp[i] != 1e9 and dp[j] != 1e9) {
  46.                     dp[min(i * j, 5000)] = min(dp[min(i * j, 5000)], dp[i] + dp[j] + 1);
  47.                 }
  48.            
  49.         }
  50.     }
  51.     for(int i = 0; i <= 5000; i++) {
  52.         for(int j = 0; j <= 5000; j++) {
  53.        
  54.             if(dp[i] != 1e9 and dp[j] != 1e9) {
  55.                 dp[min(i + j, 5000)] = min(dp[min(i + j, 5000)], dp[i] + dp[j] + 1);
  56.             }
  57.         }
  58.     }
  59.     cout << dp[n] << endl;
  60.  
  61.     return 0;
  62. }
  63. /*
  64.  5 2 2
  65.  0 1 1 0 1
  66.  0 1
  67. 1 1
  68.  
  69.  13 4 3
  70.  1 1 3 2 0 1 2 0 0 0 0 3 1
  71.  0 2
  72.  2 1
  73.  1 2
  74.  
  75.  5 3 1
  76.  1 2 0 1 2
  77.  0 2
  78.  **/
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement