Josif_tepe

Untitled

Jul 1st, 2025
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. string s;
  6. const int INF = 1e9;
  7. bool check_if_power_of_5(string num) {
  8.     if(num[0] == '0') {
  9.         return false;
  10.     }
  11.     ll power_of_two = 1;
  12.     ll res = 0;
  13.    
  14.     for(int i = (int) num.size() - 1; i >= 0; i--) {
  15.         if(num[i] == '1') {
  16.             res += power_of_two;
  17.         }
  18.         power_of_two *= 2;
  19.     }
  20.    
  21.     bool is_power_of_5 = true;
  22.     while(res > 1) {
  23.         if(res % 5 != 0) {
  24.             is_power_of_5 = false;
  25.             break;
  26.         }
  27.         res /= 5;
  28.     }
  29.     return is_power_of_5;
  30. }
  31. int  dp[55];
  32. int rec(int at) {
  33.     if(at >= (int) s.size()) {
  34.         return 0;
  35.     }
  36.     if(dp[at] != -1) {
  37.         return dp[at];
  38.     }
  39.    
  40.     string tmp = "";
  41.     int res = INF;
  42.     for(int i = at; i < (int) s.size(); i++) {
  43.         tmp += s[i];
  44.         if(check_if_power_of_5(tmp)) {
  45.             res = min(res, rec(i + 1) + 1);
  46.         }
  47.     }
  48.    
  49.     dp[at] = res;
  50.     return res;
  51.    
  52. }
  53. int main()
  54. {
  55.     memset(dp, -1, sizeof dp);
  56.     cin >> s;
  57.    
  58.     int res = rec(0);
  59.     if(res >= INF) {
  60.         res = -1;
  61.     }
  62.     cout << res << endl;
  63.    
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment