Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- typedef long long ll;
- string s;
- const int INF = 1e9;
- bool check_if_power_of_5(string num) {
- if(num[0] == '0') {
- return false;
- }
- ll power_of_two = 1;
- ll res = 0;
- for(int i = (int) num.size() - 1; i >= 0; i--) {
- if(num[i] == '1') {
- res += power_of_two;
- }
- power_of_two *= 2;
- }
- bool is_power_of_5 = true;
- while(res > 1) {
- if(res % 5 != 0) {
- is_power_of_5 = false;
- break;
- }
- res /= 5;
- }
- return is_power_of_5;
- }
- int dp[55];
- int rec(int at) {
- if(at >= (int) s.size()) {
- return 0;
- }
- if(dp[at] != -1) {
- return dp[at];
- }
- string tmp = "";
- int res = INF;
- for(int i = at; i < (int) s.size(); i++) {
- tmp += s[i];
- if(check_if_power_of_5(tmp)) {
- res = min(res, rec(i + 1) + 1);
- }
- }
- dp[at] = res;
- return res;
- }
- int main()
- {
- memset(dp, -1, sizeof dp);
- cin >> s;
- int res = rec(0);
- if(res >= INF) {
- res = -1;
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment