Guest User

Untitled

a guest
Jun 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. // Question 1
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define ll long long
  7.  
  8. int dp[100009];
  9. char s[100009];
  10. int n;
  11.  
  12. bool power(ll num)
  13. {
  14. ll copy = num;
  15. while(copy>1)
  16. {
  17. if(copy % 4 == 0)
  18. copy /= 4;
  19. else
  20. break;
  21. }
  22.  
  23. if(copy == 1)
  24. return true;
  25. copy = num;
  26. while(copy>1)
  27. {
  28. if(copy % 6 == 0)
  29. copy /= 6;
  30. else
  31. return false;
  32. }
  33. return true;
  34. }
  35.  
  36. int solve(int ind)
  37. {
  38. if(ind == n)
  39. return 0;
  40. if(s[ind] == '0')
  41. return 1e9;
  42.  
  43. if(dp[ind] != -1)
  44. return dp[ind];
  45.  
  46. long long num = 0;
  47. int ret = 1e9;
  48. for(int i=ind; i<n; i++)
  49. {
  50. num = num*2 + (s[i] == '1');
  51. if(power(num))
  52. ret = min(ret, solve(i+1) +1);
  53. }
  54.  
  55. return dp[ind] = ret;
  56. }
  57.  
  58. int main()
  59. {
  60. memset(dp, -1, sizeof(dp));
  61.  
  62. cin>>s;
  63. n = strlen(s);
  64. int ans = solve(0);
  65. if(ans == 1e9)
  66. ans = -1;
  67. printf("%d\n", ans);
  68. }
Add Comment
Please, Sign In to add comment