This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);
#define what(x) cerr << #x << " is " << x << '\n';
#define kill(x) {cout << x << '\n'; return 0;}
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ll long long
#define F first
#define S second
const ll inf = 1e18, mod = 1e9 + 7, delta = 1e9 + 9, SQ = 450, P = 6065621; //998244353
using namespace std;
const ll N = 5e5 + 10, LG = 20;
ll n, dis[N], ps0[N][20], ps1[N][20], psr[N][20];
queue<ll> q;
bool mark[N];
int main() {
fast_io;
string x, y;
cin >> n >> x >> y;
ll a = 0, b = 0;
for (int i = n - 1, j = 0; i >= 0; i--, j++)
a += (x[i] == '1') * (1 << j);
for (int i = n - 1, j = 0; i >= 0; i--, j++)
b += (y[i] == '1') * (1 << j);
q.push(a);
dis[a] = 0;
mark[a] = true;
if (a == b) kill(0);
for (int mask = 0; mask < (1 << n); mask++) {
for (int i = n - 1; i >= 0; i--) {
ps0[mask][i] = ps0[mask][i + 1] + (!(mask & (1 << i))) * (1 << i);
ps1[mask][i] = ps1[mask][i + 1] + (mask & (1 << i));
}
}
while (q.size()) {
int v = q.front();
q.pop();
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int x = v;
x -= (ps1[v][i] - ps1[v][j + 1]);
if (!mark[x]) {
mark[x] = true;
q.push(x);
dis[x] = dis[v] + 1;
if (x == b) goto Hell;
}
x = v;
x += ps0[v][i] - ps0[v][j + 1];
if (!mark[x]) {
mark[x] = true;
q.push(x);
dis[x] = dis[v] + 1;
if (x == b) goto Hell;
}
x = v;
x += ps0[v][i] - ps0[v][j + 1];
x -= (ps1[v][i] - ps1[v][j + 1]);
if (!mark[x]) {
mark[x] = true;
q.push(x);
dis[x] = dis[v] + 1;
if (x == b) goto Hell;
}
}
}
}
Hell:;
cout << dis[b] << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |