#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
218 ms |
89684 KB |
Output is correct |
9 |
Correct |
210 ms |
89628 KB |
Output is correct |
10 |
Correct |
94 ms |
48088 KB |
Output is correct |
11 |
Correct |
95 ms |
48212 KB |
Output is correct |
12 |
Correct |
41 ms |
88912 KB |
Output is correct |
13 |
Correct |
33 ms |
48332 KB |
Output is correct |
14 |
Correct |
45 ms |
89172 KB |
Output is correct |
15 |
Correct |
37 ms |
48216 KB |
Output is correct |
16 |
Correct |
60 ms |
89684 KB |
Output is correct |
17 |
Correct |
46 ms |
48212 KB |
Output is correct |
18 |
Correct |
49 ms |
89208 KB |
Output is correct |
19 |
Correct |
23 ms |
47904 KB |
Output is correct |
20 |
Correct |
69 ms |
89684 KB |
Output is correct |
21 |
Correct |
22 ms |
47700 KB |
Output is correct |
22 |
Correct |
43 ms |
88916 KB |
Output is correct |
23 |
Correct |
28 ms |
47964 KB |
Output is correct |
24 |
Correct |
42 ms |
88780 KB |
Output is correct |
25 |
Correct |
22 ms |
47708 KB |
Output is correct |
26 |
Correct |
49 ms |
89424 KB |
Output is correct |
27 |
Correct |
26 ms |
47952 KB |
Output is correct |
28 |
Correct |
14 ms |
25180 KB |
Output is correct |
29 |
Correct |
87 ms |
89704 KB |
Output is correct |
30 |
Correct |
46 ms |
48208 KB |
Output is correct |
31 |
Correct |
11 ms |
25176 KB |
Output is correct |
32 |
Correct |
42 ms |
88968 KB |
Output is correct |
33 |
Correct |
40 ms |
48256 KB |
Output is correct |
34 |
Correct |
21 ms |
25432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
218 ms |
89684 KB |
Output is correct |
9 |
Correct |
210 ms |
89628 KB |
Output is correct |
10 |
Correct |
94 ms |
48088 KB |
Output is correct |
11 |
Correct |
95 ms |
48212 KB |
Output is correct |
12 |
Correct |
41 ms |
88912 KB |
Output is correct |
13 |
Correct |
33 ms |
48332 KB |
Output is correct |
14 |
Correct |
45 ms |
89172 KB |
Output is correct |
15 |
Correct |
37 ms |
48216 KB |
Output is correct |
16 |
Correct |
60 ms |
89684 KB |
Output is correct |
17 |
Correct |
46 ms |
48212 KB |
Output is correct |
18 |
Correct |
49 ms |
89208 KB |
Output is correct |
19 |
Correct |
23 ms |
47904 KB |
Output is correct |
20 |
Correct |
69 ms |
89684 KB |
Output is correct |
21 |
Correct |
22 ms |
47700 KB |
Output is correct |
22 |
Correct |
43 ms |
88916 KB |
Output is correct |
23 |
Correct |
28 ms |
47964 KB |
Output is correct |
24 |
Correct |
42 ms |
88780 KB |
Output is correct |
25 |
Correct |
22 ms |
47708 KB |
Output is correct |
26 |
Correct |
49 ms |
89424 KB |
Output is correct |
27 |
Correct |
26 ms |
47952 KB |
Output is correct |
28 |
Correct |
14 ms |
25180 KB |
Output is correct |
29 |
Correct |
87 ms |
89704 KB |
Output is correct |
30 |
Correct |
46 ms |
48208 KB |
Output is correct |
31 |
Correct |
11 ms |
25176 KB |
Output is correct |
32 |
Correct |
42 ms |
88968 KB |
Output is correct |
33 |
Correct |
40 ms |
48256 KB |
Output is correct |
34 |
Correct |
21 ms |
25432 KB |
Output is correct |
35 |
Runtime error |
356 ms |
54572 KB |
Execution killed with signal 11 |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
8 ms |
6572 KB |
Output is correct |
8 |
Runtime error |
35 ms |
52164 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
218 ms |
89684 KB |
Output is correct |
9 |
Correct |
210 ms |
89628 KB |
Output is correct |
10 |
Correct |
94 ms |
48088 KB |
Output is correct |
11 |
Correct |
95 ms |
48212 KB |
Output is correct |
12 |
Correct |
41 ms |
88912 KB |
Output is correct |
13 |
Correct |
33 ms |
48332 KB |
Output is correct |
14 |
Correct |
45 ms |
89172 KB |
Output is correct |
15 |
Correct |
37 ms |
48216 KB |
Output is correct |
16 |
Correct |
60 ms |
89684 KB |
Output is correct |
17 |
Correct |
46 ms |
48212 KB |
Output is correct |
18 |
Correct |
49 ms |
89208 KB |
Output is correct |
19 |
Correct |
23 ms |
47904 KB |
Output is correct |
20 |
Correct |
69 ms |
89684 KB |
Output is correct |
21 |
Correct |
22 ms |
47700 KB |
Output is correct |
22 |
Correct |
43 ms |
88916 KB |
Output is correct |
23 |
Correct |
28 ms |
47964 KB |
Output is correct |
24 |
Correct |
42 ms |
88780 KB |
Output is correct |
25 |
Correct |
22 ms |
47708 KB |
Output is correct |
26 |
Correct |
49 ms |
89424 KB |
Output is correct |
27 |
Correct |
26 ms |
47952 KB |
Output is correct |
28 |
Correct |
14 ms |
25180 KB |
Output is correct |
29 |
Correct |
87 ms |
89704 KB |
Output is correct |
30 |
Correct |
46 ms |
48208 KB |
Output is correct |
31 |
Correct |
11 ms |
25176 KB |
Output is correct |
32 |
Correct |
42 ms |
88968 KB |
Output is correct |
33 |
Correct |
40 ms |
48256 KB |
Output is correct |
34 |
Correct |
21 ms |
25432 KB |
Output is correct |
35 |
Runtime error |
356 ms |
54572 KB |
Execution killed with signal 11 |
36 |
Halted |
0 ms |
0 KB |
- |