Official
A - 暗号化リレー / Encryption Relay Editorial
by
A - 暗号化リレー / Encryption Relay Editorial
by
kyopro_friends
初心者の方へ
- AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
- また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
問題文の指示通りに \(B_i, D_i\) を順に求めればよいです。
多くのプログラミング言語では、ビットごとの排他的論理和は ^ 演算子により行うことができます。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, x;
cin >> n >> x;
vector<int>a(n);
for(int i=0; i<n; i++) cin >> a[i];
vector<int>b;
for(int i=0; i<n; i++){
if(i != 0 && i != n-1 && a[i-1] == a[i+1] && a[i-1] != a[i]){
b.push_back(0);
}else{
b.push_back(a[i]);
}
}
int d = x;
for(int i=0; i<n; i++){
d ^= b[i];
}
cout << d << endl;
}
実装例 (Python)
N, X = map(int, input().split())
A = list(map(int, input().split()))
B = []
for i in range(N):
if i != 0 and i != N-1 and A[i-1] == A[i+1] and A[i-1] != A[i]:
B.append(0)
else:
B.append(A[i])
D = X
for b in B:
D ^= b
print(D)
