E - Adjacent Xor Game Editorial /

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 25002500

問題文

NN 個の非負整数 A1,A2,,ANA_1,A_2,\cdots,A_N が与えられます. 各 k=1,2,,Nk=1,2,\cdots,N について,以下の問題を解いてください.

Alice と Bob が以下のようなゲームをします.

  • Alice が A1,A2,,ANA_1,A_2,\cdots,A_N の中から kk 個の数を選ぶ. 選んだ数を x1,x2,,xkx_1,x_2,\cdots,x_k (順不同)とおく.

  • Bob が長さ 2k2k の非負整数列 y=(y1,y2,,y2k)y=(y_1,y_2,\cdots,y_{2k}) を作る. ここで,yy は以下の条件を満たす必要がある.

    • 0y1y2y2k0 \leq y_1 \leq y_2 \leq \cdots \leq y_{2k}
    • zi=y2i1y2iz_i=y_{2i-1} \oplus y_{2i} と定義する. このとき,{z1,z2,,zk}\{z_1,z_2,\cdots,z_k\} は多重集合として {x1,x2,,xk}\{x_1,x_2,\cdots,x_k\} と一致する.

なお,\oplus はビット単位 XOR\mathrm{XOR} 演算を表します.

このゲームのスコアy2ky_{2k} の値とします. Bob はスコアを最小化するように行動します. その上で Alice はスコアを最大化するように行動します. このとき,スコアはいくつになるでしょうか?

ビット単位 XOR\mathrm{XOR} 演算とは

非負整数 A,BA, B のビット単位 XOR\mathrm{XOR}ABA \oplus B は、以下のように定義されます。

  • ABA \oplus B を二進表記した際の 2k2^k (k0k \geq 0) の位の数は、A,BA, B を二進表記した際の 2k2^k の位の数のうち一方のみが 11 であれば 11、そうでなければ 00 である。
例えば、35=63 \oplus 5 = 6 となります (二進表記すると: 011101=110011 \oplus 101 = 110)。
一般に kk 個の非負整数 p1,p2,p3,,pkp_1, p_2, p_3, \dots, p_k のビット単位 XOR\mathrm{XOR}(((p1p2)p3)pk)(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p1,p2,p3,,pkp_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

  • 1N2500001 \leq N \leq 250000
  • 1Ai1091 \leq A_i \leq 10^9
  • 入力される値はすべて整数.

入力

入力は以下の形式で標準入力から与えられる.

NN
A1A_1 A2A_2 \cdots ANA_N

出力

答えを出力せよ.


入力例 1Copy

Copy
3
1 2 3

出力例 1Copy

Copy
2
6
6

k=1k=1 の場合を考えます.

  • Alice が x1=1x_1=1 を選んだ場合: Bob は y=(0,1)y=(0,1) を作り,スコアは 11 になります.
  • Alice が x1=2x_1=2 を選んだ場合: Bob は y=(0,2)y=(0,2) を作り,スコアは 22 になります.
  • Alice が x1=3x_1=3 を選んだ場合: Bob は y=(1,2)y=(1,2) を作り,スコアは 22 になります.

よって Alice は x1=2x_1=2 または x1=3x_1=3 を選び, スコアは 22 になります.

k=2k=2 の場合を考えます. Alice が (x1,x2)=(2,3)(x_1,x_2)=(2,3) を選び,Bob が y=(1,2,4,6)y=(1,2,4,6) を作ると,スコアは 66 になります. これは両者の最適な行動の一例であり,求める答えは 66 です.

k=3k=3 の場合を考えます. Alice が (x1,x2,x3)=(1,2,3)(x_1,x_2,x_3)=(1,2,3) を選び,Bob が y=(0,1,1,2,4,6)y=(0,1,1,2,4,6) を作ると,スコアは 66 になります. これは両者の最適な行動の一例であり,求める答えは 66 です.


入力例 2Copy

Copy
5
1 1 1 1 1

出力例 2Copy

Copy
1
3
5
7
9

入力例 3Copy

Copy
5
1 2 4 8 16

出力例 3Copy

Copy
16
24
28
30
31

入力例 4Copy

Copy
20
167660508 377125547 866003430 419036363 234113368 201296307 408194538 249252693 207212853 190504619 306027011 652875643 335898069 545795899 204268068 274532279 342039277 975300308 901270584 360957186

出力例 4Copy

Copy
536870912
1610612736
2684354560
3758096384
4831838208
4831838208
4831838208
4831838208
4831838208
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664

Score : 25002500 points

Problem Statement

You are given NN non-negative integers A1,A2,,ANA_1,A_2,\cdots,A_N. For each k=1,2,,Nk=1,2,\cdots,N, solve the following problem.

Alice and Bob play the following game.

  • Alice chooses kk numbers from A1,A2,,ANA_1,A_2,\cdots,A_N. Let x1,x2,,xkx_1,x_2,\cdots,x_k be the chosen numbers (in arbitrary order).

  • Bob makes a sequence of 2k2k non-negative integers y=(y1,y2,,y2k)y=(y_1,y_2,\cdots,y_{2k}), which must satisfy the following conditions.

    • 0y1y2y2k0 \leq y_1 \leq y_2 \leq \cdots \leq y_{2k}.
    • Let zi=y2i1y2iz_i=y_{2i-1} \oplus y_{2i}. Then, {z1,z2,,zk}\{z_1,z_2,\cdots,z_k\} coincides with {x1,x2,,xk}\{x_1,x_2,\cdots,x_k\} as a multiset.

Here, \oplus denotes bitwise XOR\mathrm{XOR}.

Let the score of the game be the value of y2ky_{2k}. Bob plays to minimize the score. With this in mind, Alice plays to maximize the score. What will the score be?

What is bitwise XOR\mathrm{XOR}?

The bitwise XOR\mathrm{XOR} of non-negative integers AA and BB, A XOR BA\ \mathrm{XOR}\ B, is defined as follows.

  • When A XOR BA\ \mathrm{XOR}\ B is written in binary, the kk-th lowest bit (k0k \geq 0) is 11 if exactly one of the kk-th lowest bits of AA and BB is 11, and 00 otherwise.
For instance, 3 XOR 5=63\ \mathrm{XOR}\ 5 = 6 (in binary: 011 XOR 101=110011\ \mathrm{XOR}\ 101 = 110).
Generally, the bitwise XOR\mathrm{XOR} of kk non-negative integers p1,p2,p3,,pkp_1, p_2, p_3, \dots, p_k is defined to be (((p1 XOR p2) XOR p3) XOR  XOR pk)(\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k), which one can prove to be independent of the order of p1,p2,p3,pkp_1, p_2, p_3, \dots p_k.

Constraints

  • 1N2500001 \leq N \leq 250000
  • 1Ai1091 \leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 \cdots ANA_N

Output

Print the answer.


Sample Input 1Copy

Copy
3
1 2 3

Sample Output 1Copy

Copy
2
6
6

Consider the case k=1k=1.

  • If Alice chooses x1=1x_1=1: Bob makes y=(0,1)y=(0,1), for the score of 11.
  • If Alice chooses x1=2x_1=2: Bob makes y=(0,2)y=(0,2), for the score of 22.
  • If Alice chooses x1=3x_1=3: Bob makes y=(1,2)y=(1,2), for the score of 22.

Thus, Alice will choose x1=2x_1=2 or x1=3x_1=3, for the score of 22.

Consider the case k=2k=2. If Alice chooses (x1,x2)=(2,3)(x_1,x_2)=(2,3) and Bob makes y=(1,2,4,6)y=(1,2,4,6), the score will be 66. This is an example of optimal play for both players, and the answer is 66.

Consider the case k=3k=3. If Alice chooses (x1,x2,x3)=(1,2,3)(x_1,x_2,x_3)=(1,2,3) and Bob makes y=(0,1,1,2,4,6)y=(0,1,1,2,4,6), the score will be 66. This is an example of optimal play for both players, and the answer is 66.


Sample Input 2Copy

Copy
5
1 1 1 1 1

Sample Output 2Copy

Copy
1
3
5
7
9

Sample Input 3Copy

Copy
5
1 2 4 8 16

Sample Output 3Copy

Copy
16
24
28
30
31

Sample Input 4Copy

Copy
20
167660508 377125547 866003430 419036363 234113368 201296307 408194538 249252693 207212853 190504619 306027011 652875643 335898069 545795899 204268068 274532279 342039277 975300308 901270584 360957186

Sample Output 4Copy

Copy
536870912
1610612736
2684354560
3758096384
4831838208
4831838208
4831838208
4831838208
4831838208
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664
5100273664


2025-03-31 (Mon)
10:31:32 +00:00