結果

問題 No.1296 OR or NOR
ユーザー chocorusk
提出日時 2020-11-22 03:38:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,390 ms / 3,000 ms
コード長 1,946 bytes
コンパイル時間 1,407 ms
コンパイル使用メモリ 124,684 KB
最終ジャッジ日時 2025-01-16 04:16:28
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |         scanf("%lld", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
main.cpp:50:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   50 |         scanf("%lld", &b);
      |         ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    int n;
    cin>>n;
    vector<ll> a(n);
    for(int i=0; i<n; i++){
        scanf("%lld", &a[i]);
    }
    ll as[20][200020];
    for(int i=0; i<n; i++) as[0][i]=a[i];
    for(int i=1; (1<<i)<=n; i++){
        for(int j=0; j<n; j++){
            if(j+1-(1<<i)>=0){
                as[i][j]=(as[i-1][j]|as[i-1][j-(1<<(i-1))]);
            }
        }
    }
    int q;
    cin>>q;
    while(q--){
        ll b;
        scanf("%lld", &b);
        int k=n-1;
        ll x=(1ll<<60)-1;
        int ans=0;
        while(k>=0){
            for(int t=17; t>=0; t--){
                if(k+1-(1<<t)<0) continue;
                if((x&as[t][k])==0) k-=(1<<t);
            }
            if(k==-1){
                if(b==0) break;
                else if(x==b){
                    ans++;
                    b=0;
                    break;
                }else{
                    ans=-1;
                    break;
                }
            }
            if(((b|a[k])&x)==b){
                b^=(x&a[k]);
                x^=(x&a[k]);
                k--;
            }else if(((~b|a[k])&x)==(~b&x)){
                ans++;
                b=(~b&x);
                b^=(x&a[k]);
                x^=(x&a[k]);
                k--;
            }else{
                ans=-1;
                break;
            }
        }
        if(b!=0) ans=-1;
        printf("%d\n", ans);
    }
	return 0;
}

0