Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Question 1
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int dp[100009];
- char s[100009];
- int n;
- bool power(ll num)
- {
- ll copy = num;
- while(copy>1)
- {
- if(copy % 4 == 0)
- copy /= 4;
- else
- break;
- }
- if(copy == 1)
- return true;
- copy = num;
- while(copy>1)
- {
- if(copy % 6 == 0)
- copy /= 6;
- else
- return false;
- }
- return true;
- }
- int solve(int ind)
- {
- if(ind == n)
- return 0;
- if(s[ind] == '0')
- return 1e9;
- if(dp[ind] != -1)
- return dp[ind];
- long long num = 0;
- int ret = 1e9;
- for(int i=ind; i<n; i++)
- {
- num = num*2 + (s[i] == '1');
- if(power(num))
- ret = min(ret, solve(i+1) +1);
- }
- return dp[ind] = ret;
- }
- int main()
- {
- memset(dp, -1, sizeof(dp));
- cin>>s;
- n = strlen(s);
- int ans = solve(0);
- if(ans == 1e9)
- ans = -1;
- printf("%d\n", ans);
- }
Add Comment
Please, Sign In to add comment