Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, cnt[2050];
- char a[1000][2050], b[2050], x[2050];
- void f (int i) {
- for (int j = 0; j < n; ++j) {
- a[j][i]++;
- for (int k = i; k < 2040 && a[j][k] == 2; ++k) {
- a[j][k] = 0;
- a[j][k + 1]++;
- }
- }
- x[i] += 1;
- for (int k = i; k < 2040 && x[k] == 2; ++k) {
- x[k] = 0;
- x[k + 1]++;
- }
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- freopen("input.txt", "r", stdin);
- cin >> n;
- if (n == 1) {
- string a, b;
- cin >> a >> b;
- int x = 0, y = 0;
- for (int i = 0; i < a.size(); ++i)
- x = x*2 + a[i] - 48;
- for (int i = 0; i < b.size(); ++i)
- y = y*2 + b[i] - 48;
- y -= x;
- if (y < 0) {
- cout << -1;
- return 0;
- }
- string ans;
- while (y) {
- ans = ans + char(y%2 + 48);
- y /= 2;
- }
- if (ans == "")
- cout << 0;
- else
- cout << ans;
- return 0;
- }
- string A;
- for (int i = 0; i < n; ++i) {
- cin >> A;
- reverse(A.begin(), A.end());
- for (int j = 0; j < A.size(); ++j)
- a[i][j] = A[j] - 48;
- }
- cin >> A;
- reverse(A.begin(), A.end());
- for (int j = 0; j < A.size(); ++j)
- b[j] = A[j] - 48;
- for (int i = 0; i < 2050; ++i) {
- for (int j = 0; j < n; ++j) {
- cnt[i] += a[j][i];
- }
- if (cnt[i]%2 && !b[i])
- if (n%2 == 0) {
- if (!i || b[i - 1] && (n - cnt[i - 1])%2 == 0 || !b[i - 1] && (n - cnt[i - 1])%2) {
- cout << -1;
- return 0;
- }
- else {
- f(i - 1);
- cnt[i] = 0;
- for (int j = 0; j < n; ++j) {
- cnt[i] += a[j][i];
- }
- if (cnt[i]%2) {
- cout << -1;
- return 0;
- }
- }
- }
- else {
- f(i);
- cnt[i] = n - cnt[i];
- }
- else
- if (cnt[i]%2 == 0 && b[i])
- if (n%2 == 0) {
- if (!i || b[i - 1] && (n - cnt[i - 1])%2 == 0 || !b[i - 1] && (n - cnt[i - 1])%2) {
- cout << -1;
- return 0;
- }
- else {
- f(i - 1);
- cnt[i] = 0;
- for (int j = 0; j < n; ++j) {
- cnt[i] += a[j][i];
- }
- if (cnt[i]%2 == 0) {
- cout << -1;
- return 0;
- }
- }
- }
- else {
- f(i);
- cnt[i] = n - cnt[i];
- }
- }
- int i = 2040;
- for (; i && !x[i]; --i);
- if (i > 1999) {
- cout << -1;
- return 0;
- }
- for (; i + 1; --i)
- cout << int(x[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement