結果

問題 No.1355 AND OR GAME
ユーザー 👑 emthrmemthrm
提出日時 2021-04-02 03:52:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 3,475 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 206,312 KB
最終ジャッジ日時 2025-01-20 07:37:50
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 95
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  constexpr int B = 60;
  int n; ll x, y; cin >> n >> x >> y;
  auto out = [&](string &ans) {
    while (ans.size() < n) ans += '3';
    reverse(ALL(ans));
    REP(i, n) cout << ans[i] << " \n"[i + 1 == n];
    exit(0);
  };
  vector<ll> a(n); REP(i, n) cin >> a[i];
  vector<int> d(B);
  REP(i, B) d[i] = y >> i & 1;
  vector<pair<vector<int>, string>> que;
  {
    vector<int> bit(B);
    iota(ALL(bit), 0);
    que.emplace_back(bit, "");
  }
  auto f = [&](auto &&f, int i, vector<int> &bit, string &ans) -> void {
    if (i == -1) {
      bool sw = true;
      for (int b : bit) {
        if (d[b] != (x >> b & 1)) {
          sw = false;
          break;
        }
      }
      if (sw) out(ans);
      return;
    }
    bool sw = true;
    for (int b : bit) {
      if (d[b] != (a[i] >> b & 1)) {
        sw = false;
        break;
      }
    }
    if (sw) out(ans);
    bool an = false, o = false;
    for (int b : bit) {
      if ((a[i] >> b & 1) && d[b] == 0) an = true;
      if (!(a[i] >> b & 1) && d[b] == 1) o = true;
    }
    if (an && o) return;
    if (an) {
      ans += '1';
      vector<int> nx_bit;
      for (int b : bit) {
        if (!(!(a[i] >> b & 1) && d[b] == 0)) nx_bit.emplace_back(b);
      }
      if (nx_bit.empty()) out(ans);
      f(f, i - 1, nx_bit, ans);
      ans.pop_back();
    } else if (o) {
      ans += '2';
      vector<int> nx_bit;
      for (int b : bit) {
        if (!((a[i] >> b & 1) && d[b] == 1)) nx_bit.emplace_back(b);
      }
      if (nx_bit.empty()) out(ans);
      f(f, i - 1, nx_bit, ans);
      ans.pop_back();
    } else {
      int cnt[2] = {};
      for (int b : bit) ++cnt[d[b]];
      if (cnt[0] == bit.size()) {
        ans += '1';
        out(ans);
        ans.pop_back();
      } else if (cnt[1] == bit.size()) {
        ans += '2';
        out(ans);
        ans.pop_back();
      } else {
        {
          vector<int> nx_bit;
          for (int b : bit) {
            if (!(!(a[i] >> b & 1) && d[b] == 0)) nx_bit.emplace_back(b);
          }
          assert(!nx_bit.empty());
          ans += '1';
          f(f, i - 1, nx_bit, ans);
          ans.pop_back();
        }
        {
          vector<int> nx_bit;
          for (int b : bit) {
            if (!((a[i] >> b & 1) && d[b] == 1)) nx_bit.emplace_back(b);
          }
          assert(!nx_bit.empty());
          ans += '2';
          f(f, i - 1, nx_bit, ans);
          ans.pop_back();
        }
      }
    }
  };
  vector<int> bit(B);
  iota(ALL(bit), 0);
  string ans = "";
  f(f, n - 1, bit, ans);
  cout << "-1\n";
  return 0;
}
0