Editorial for CPC '21 Contest 1 P2 - AQT and Multiset


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Tzak

For the first subtask, it suffices to try possible values of c less than 215. The resulting multisets can be compared with sorting in O(NlogN).

Time complexity: O(maxANlogN)

For the second subtask, we observe that if a value for c exists, there is some Ai such that Aic=B1, meaning c=AiB1. With this, we have 2N+1 possible candidates for c: A1B1,A2B1,,A2N+1B1.

Time complexity: O(N2logN)

For the final subtask, we will use the fact that 2N+1 is odd. Let's begin by assuming that we have found a value of c which makes the multisets equal. This implies that:

A1cA2cA2N+1c=B1B2B2N+1

Let's XOR both sides by A1A2A2N+1:

ccc=B1B2B2N+1A1A2A2N+1

The left hand side has an odd number of copies of c, so we have:

c=B1B2B2N+1A1A2A2N+1

We have proven if a solution exists, then it is unique and given by the expression above. All that remains is to check if this value of c works.

Time complexity: O(NlogN)


Comments

There are no comments at the moment.