Official

A - 暗号化リレー / Encryption Relay Editorial by kyopro_friends Editorial for printing


初心者の方へ


問題文の指示通りに \(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)

posted:
last update: