結果

問題 No.1296 OR or NOR
ユーザー 👑 NachiaNachia
提出日時 2020-11-20 22:26:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 577 ms / 3,000 ms
コード長 1,409 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 205,812 KB
実行使用メモリ 4,916 KB
最終ジャッジ日時 2023-09-30 19:33:53
合計ジャッジ時間 22,189 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 424 ms
4,616 KB
testcase_03 AC 422 ms
4,572 KB
testcase_04 AC 466 ms
4,592 KB
testcase_05 AC 466 ms
4,592 KB
testcase_06 AC 575 ms
4,900 KB
testcase_07 AC 563 ms
4,644 KB
testcase_08 AC 564 ms
4,616 KB
testcase_09 AC 498 ms
4,740 KB
testcase_10 AC 507 ms
4,664 KB
testcase_11 AC 442 ms
4,668 KB
testcase_12 AC 433 ms
4,916 KB
testcase_13 AC 465 ms
4,780 KB
testcase_14 AC 577 ms
4,644 KB
testcase_15 AC 482 ms
4,628 KB
testcase_16 AC 448 ms
4,620 KB
testcase_17 AC 571 ms
4,572 KB
testcase_18 AC 568 ms
4,748 KB
testcase_19 AC 575 ms
4,592 KB
testcase_20 AC 385 ms
4,380 KB
testcase_21 AC 387 ms
4,380 KB
testcase_22 AC 390 ms
4,376 KB
testcase_23 AC 381 ms
4,376 KB
testcase_24 AC 390 ms
4,380 KB
testcase_25 AC 397 ms
4,376 KB
testcase_26 AC 390 ms
4,376 KB
testcase_27 AC 388 ms
4,376 KB
testcase_28 AC 400 ms
4,380 KB
testcase_29 AC 390 ms
4,380 KB
testcase_30 AC 513 ms
4,616 KB
testcase_31 AC 515 ms
4,704 KB
testcase_32 AC 519 ms
4,744 KB
testcase_33 AC 454 ms
4,612 KB
testcase_34 AC 454 ms
4,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0;i<(n);i++)

int N,Q;
vector<ULL> A;

int main(){
  cin>>N;
  A.resize(N);
  rep(i,N) cin>>A[i];

  vector<ULL> G;
  ULL no;
  bool cannot_fix=false;
  {
    int dcrA[60];
    int rA[60]; rep(i,60) rA[i]=-1;
    rep(i,N) rep(d,60) if((A[i]>>d)&1) rA[d]=i;

    pair<int,int> S[61];
    S[0] = {-1,-1};
    rep(i,60){ S[i+1] = {rA[i],i}; }
    sort(S,S+61);
    int p=-1;
    rep(i,60){
      if(S[i+1].first!=S[i].first) p++;
      dcrA[S[i+1].second]=p;
    }
    G.resize(p+1);
    no=0;
    rep(i,60){
      if(dcrA[i]==-1) no |= 1ull<<i;
      else G[dcrA[i]] |= 1ull<<i;
    }

    rep(i,61) if(S[i].first==0) cannot_fix=true;
  }


  cin>>Q;
  while(Q--){
    ULL b; cin>>b;
    bool enable = true;

    int no_flipped=0;
    if((no&b) == 0){ no_flipped=0; }
    else if((no&b) == no){ no_flipped=1; }
    else enable=false;

    for(ULL g:G) if(((g&b) != g) && ((g&b) != 0)) enable=false;
    
    int flipped = 0;
    int ans=0;
    for(int i=G.size()-1; i>=0 && enable; i--){
      ULL g = G[i];
      int flip=1; if(g&b) flip=0;
      if(flipped != flip) ans++;
      flipped = flip;
    }
    
    if(no) if(ans%2 != no_flipped) {
      ans++;
      if(cannot_fix){ enable=false; }
    }

    if(!enable){ cout<<-1<<endl; continue; }
    cout<<ans<<endl;
  }

  return 0;
}

0