結果

問題 No.1296 OR or NOR
ユーザー 沙耶花沙耶花
提出日時 2020-06-26 15:25:19
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 953 ms / 3,000 ms
コード長 1,190 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 198,404 KB
最終ジャッジ日時 2025-01-11 10:36:06
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:44:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |         for(int i=0;i<N;i++)scanf("%lld",&a[i]);
      |                             ~~~~~^~~~~~~~~~~~~~
main.cpp:70:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |                 scanf("%lld",&b);
      |                 ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000

int check(vector<long long> &base,long long t,char last){
	long long now = 0LL;
	string S = "1";
	for(int i=0;i<base.size();i++){
		for(int j=0;j<60;j++){
			if((base[i]>>j)&1){
				if((t>>j)&1){
					now ^= base[i];
					S += '1';
				}
				else{
					S += '0';
				}
				break;
			}
		}
	}
	
	if(now!=t)return 100;
	
	int ret = 0;
	S += last;
	for(int i=1;i<S.size();i++){
		if(S[i]!=S[i-1])ret++;
	}
	
	return ret;
}

int main(){
	
	long long X = (1LL<<60)-1;
	
	int N;
	cin>>N;
	
	vector<long long> a(N);
	for(int i=0;i<N;i++)scanf("%lld",&a[i]);
	
	long long now = X;
	vector<long long> base;
	
	for(int i=N-1;i>=1;i--){
		long long t = now&a[i];
		for(int j=59;j>=0;j--){
			if((t>>j)&1){
				base.push_back(t);
				break;
			}
		}
		
		now &= a[i]^X;
	}
	
	reverse(base.begin(),base.end());
	
	long long t = now&a[0];
	
	int Q;
	cin>>Q;
	
	for(int i=0;i<Q;i++){
		long long b;
		scanf("%lld",&b);
		int ans = min(check(base,b^t,'1'),check(base,b^X^t,'0'));
		if(ans==100)printf("-1\n");
		else printf("%d\n",ans);
	}
	
	return 0;
}
0