1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <bits/stdc++.h> using namespace std; #define int long long
int check(string x) { int t = x.size() / 3; if(t == 1) { int p = x[0] + x[1] + x[2] - 3 * '0'; if(p >= 2) return 1; else return 0; } int x1 = check(x.substr(0, t)); int x2 = check(x.substr(t, t)); int x3 = check(x.substr(t + t)); int p = x1 + x2 + x3; if(p >= 2) return 1; else return 0; }
signed main(){ int n; cin >> n; string s; cin >> s; int f = check(s); auto dfs = [&](auto&& dfs, string x)->int { int t = x.size() / 3; if(t == 1) { int p = x[0] + x[1] + x[2] - 3 * '0'; if(p >= 2) p = 1; else p = 0; if(p != f) return 0; if(x[0] == x[1] && x[1] == x[2]) return 2; return 1; } int x1 = dfs(dfs, x.substr(0, t)); int x2 = dfs(dfs, x.substr(t, t)); int x3 = dfs(dfs, x.substr(t + t)); int p1 = check(x.substr(0, t)); int p2 = check(x.substr(t, t)); int p3 = check(x.substr(t + t)); if(p1 == p2 && p2 == p3) { return min({x1 + x2, x1 + x3, x2 + x3}); }else{ if(p1 == p2) return min(x1, x2); if(p2 == p3) return min(x2, x3); if(p1 == p3) return min(x1, x3); } };
cout << dfs(dfs, s) << endl; return 0; }
|