#P14037. [PAIO 2025] XOR multiset

[PAIO 2025] XOR multiset

题目背景

DO NOT include xor.h. Submit using C++ >=17.

题目描述

You are given an integer nn and n1n-1 non-negative integers a1,a2,,an1a_1, a_2, \dots, a_{n-1}.

Find a multiset SS of integers from {1,2,,n1}\{1, 2, \dots, n-1\} such that:

  • xSx0(modn)\sum_{x \in S} x \equiv 0 \pmod n
  • xSax\bigoplus_{x \in S} a_x is maximized, where \bigoplus denotes the bitwise XOR operation. The bitwise XOR operator works on the binary representation of two numbers and performs the logical exclusive OR operation on each pair of corresponding bits; so 5 (binary representation 0101) XOR 3 (binary representation 0011) gives 6 (binary representation 0110). The operator is ^ in C++, Java, and Python.

If there are multiple such multisets, you can return any of them.

Implementation Details

You need to implement the following function:

(int64, int32[]) find_multiset(int32 n, int64[] a)
  • nn: the modulus value
  • aa: array of length n1n-1, where a[i]a[i] corresponds to ai+1a_{i+1}
  • The function should return a pair with:
    • First element: an integer representing the optimal value of xSax\bigoplus_{x \in S} a_x among all valid multisets SS
    • Second element: a vector representing any optimal multiset SS. The elements of the vector should be integers from 11 to n1n-1, and the size of SS has to be at most 2n2n.

提示

Examples

The following find_multiset(3, {5, 10}) call should return {15, {1, 2}}

  • We have n=3n=3 and a={5,10}a=\{5, 10\} (corresponding to a1=5,a2=10a_1=5, a_2=10).
  • We need to find a multiset S{1,2}S \subseteq \{1, 2\} such that xSx0(mod3)\sum_{x \in S} x \equiv 0 \pmod 3.
  • Valid multisets include: \varnothing (sum = 0), {1,2}\{1, 2\} (sum = 3 \equiv 0), {1,1,1}\{1, 1, 1\} (sum = 3 \equiv 0), {2,2,2}\{2, 2, 2\} (sum = 6 \equiv 0), etc.
  • For S={1,2}S = \{1, 2\}: XOR is a1a2=510=15a_1 \oplus a_2 = 5 \oplus 10 = 15.
  • For S={1,1,1}S = \{1, 1, 1\}: XOR is $a_1 \oplus a_1 \oplus a_1 = 5 \oplus 5 \oplus 5 = 5$.
  • The maximum XOR value is 15, achieved by S={1,2}S = \{1, 2\}.

The following find_multiset(4, {8, 12, 6}) call should return {14, {1, 3}}

  • We have n=4n=4 and a={8,12,6}a=\{8, 12, 6\} (corresponding to a1=8,a2=12,a3=6a_1=8, a_2=12, a_3=6).
  • We need to find a multiset S{1,2,3}S \subseteq \{1, 2, 3\} such that xSx0(mod4)\sum_{x \in S} x \equiv 0 \pmod 4.
  • For S={1,3}S = \{1, 3\}: XOR is a1a3=86=14a_1 \oplus a_3 = 8 \oplus 6 = 14.
  • For S={2,2}S = \{2, 2\}: XOR is a2a2=1212=0a_2 \oplus a_2 = 12 \oplus 12 = 0.
  • The maximum XOR value is 14, achieved by S={1,3}S = \{1, 3\}.

Sample Grader

The sample grader reads the input in the following format:

  • Line 1: One integer nn
  • Line 2: n1n-1 integers a1,a2,,an1a_1, a_2, \dots, a_{n-1}

The sample grader calls find_multiset(n, a) and prints the returned multiset in the following format:

  • First line: the value returned as the first element of the pair
  • Second line: the size of the multiset
  • Third line: the elements of the multiset (if any), separated by spaces

Note: The sample grader provided with this problem is just for testing your solution locally. The actual grader used during the contest may be different.

Constraints

  • 1n1051 \le n \le 10^5
  • 0ai<2320 \le a_i < 2^{32} for each i=1,2,,n1i = 1, 2, \dots, n-1

Scoring

  • Subtask 1 (20 points): n10n \le 10
  • Subtask 2 (40 points): nn is odd
  • Subtask 3 (40 points): No additional constraints

In each subtask, you can obtain a partial score if your program determines the optimal value of xSax\bigoplus_{x \in S} a_x among all valid multisets SS. More precisely, you get the whole score of a subtask if in all of its test cases, the first element of the pair returned by find_multiset is exactly the same as the first element of the pair returned by the official grader and the second element is a valid multiset (i.e. it satisfies the conditions above) that achieves this optimal value. You get 60% of the score of a subtask if in all of its test cases, the first element of the pair returned by find_multiset is exactly the same as the first element of the pair returned by the official grader (regardless of the second element) and you get 0% of the score of a subtask otherwise.