Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, cnt[2050];
  5.  
  6. char a[1000][2050], b[2050], x[2050];
  7.  
  8. void f (int i) {
  9.     for (int j = 0; j < n; ++j) {
  10.         a[j][i]++;
  11.         for (int k = i; k < 2040 && a[j][k] == 2; ++k) {
  12.             a[j][k] = 0;
  13.             a[j][k + 1]++;
  14.         }
  15.     }
  16.     x[i] += 1;
  17.     for (int k = i; k < 2040 && x[k] == 2; ++k) {
  18.         x[k] = 0;
  19.         x[k + 1]++;
  20.     }
  21. }
  22.  
  23. int main() {
  24.     ios_base::sync_with_stdio(0);
  25.     cin.tie(0);
  26.     freopen("input.txt", "r", stdin);
  27.  
  28.     cin >> n;
  29.     if (n == 1) {
  30.         string a, b;
  31.         cin >> a >> b;
  32.         int x = 0, y = 0;
  33.         for (int i = 0; i < a.size(); ++i)
  34.             x = x*2 + a[i] - 48;
  35.         for (int i = 0; i < b.size(); ++i)
  36.             y = y*2 + b[i] - 48;
  37.         y -= x;
  38.         if (y < 0) {
  39.             cout << -1;
  40.             return 0;
  41.         }
  42.         string ans;
  43.         while (y) {
  44.             ans = ans + char(y%2 + 48);
  45.             y /= 2;
  46.         }
  47.         if (ans == "")
  48.             cout << 0;
  49.         else
  50.             cout << ans;
  51.         return 0;
  52.     }
  53.     string A;
  54.     for (int i = 0; i < n; ++i) {
  55.         cin >> A;
  56.         reverse(A.begin(), A.end());
  57.         for (int j = 0; j < A.size(); ++j)
  58.             a[i][j] = A[j] - 48;
  59.     }
  60.     cin >> A;
  61.     reverse(A.begin(), A.end());
  62.     for (int j = 0; j < A.size(); ++j)
  63.         b[j] = A[j] - 48;
  64.     for (int i = 0; i < 2050; ++i) {
  65.         for (int j = 0; j < n; ++j) {
  66.             cnt[i] += a[j][i];
  67.         }
  68.         if (cnt[i]%2 && !b[i])
  69.             if (n%2 == 0) {
  70.                 if (!i || b[i - 1] && (n - cnt[i - 1])%2 == 0 || !b[i - 1] && (n - cnt[i - 1])%2) {
  71.                     cout << -1;
  72.                     return 0;
  73.                 }
  74.                 else {
  75.                     f(i - 1);
  76.                     cnt[i] = 0;
  77.                     for (int j = 0; j < n; ++j) {
  78.                         cnt[i] += a[j][i];
  79.                     }
  80.                     if (cnt[i]%2) {
  81.                         cout << -1;
  82.                         return 0;
  83.                     }
  84.                 }
  85.             }
  86.             else {
  87.                 f(i);
  88.                 cnt[i] = n - cnt[i];
  89.             }
  90.         else
  91.             if (cnt[i]%2 == 0 && b[i])
  92.                 if (n%2 == 0) {
  93.                     if (!i || b[i - 1] && (n - cnt[i - 1])%2 == 0 || !b[i - 1] && (n - cnt[i - 1])%2) {
  94.                         cout << -1;
  95.                         return 0;
  96.                     }
  97.                     else {
  98.                         f(i - 1);
  99.                         cnt[i] = 0;
  100.                         for (int j = 0; j < n; ++j) {
  101.                             cnt[i] += a[j][i];
  102.                         }
  103.                         if (cnt[i]%2 == 0) {
  104.                             cout << -1;
  105.                             return 0;
  106.                         }
  107.                     }
  108.                 }
  109.                 else {
  110.                     f(i);
  111.                     cnt[i] = n - cnt[i];
  112.                 }
  113.     }
  114.     int i = 2040;
  115.     for (; i && !x[i]; --i);
  116.     if (i > 1999) {
  117.         cout << -1;
  118.         return 0;
  119.     }
  120.     for (; i + 1; --i)
  121.         cout << int(x[i]);
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement