Editorial
Represent all possible pairs as a bipartite multigraph.
Create a left vertex for every possible sum , and a right vertex for every possible XOR value . For every possible pair , add an edge between and .
At the beginning of a day, the remaining edges are exactly the pairs consistent with every earlier statement. Dadas knows the pair exactly when the vertex for the observed sum has degree . Mog knows the pair exactly when the vertex for the observed XOR has degree .
Because the statements are simultaneous, all edges incident to a degree- vertex at the beginning of a day must be removed simultaneously. The answer is therefore the peeling round in which the actual edge is removed. An edge that remains in the graph's -core forever has answer .
Assume . Let be the largest power of two not exceeding .
The only sum vertices of initial degree correspond to the following four pairs:
- with sum .
- with sum .
- with sum .
- with sum .
The initial degree- XOR vertices are classified as follows.
- If , every pair with is unique.
- If , only and are unique.
- Otherwise, there is no such pair.
For the proof, fix an XOR value . If , the highest bit is preserved. Since , every occurring XOR value has at least two representations inside the lower block.
If , write . Write the larger number as . The smaller number must then be , where . Only is invalid because it produces .
- If , there is only one possible , so is unique.
- If , exactly leave one valid pair, namely and .
- If , at least two valid values remain after excluding at most one.
Checking the vertices that can lose degree in each round gives the following table.
Condition | Day 1 | Day 2 | Day 3 | Day 4 |
|---|---|---|---|---|
none | none | |||
otherwise | none | none | none |
When , the first round removes every . Consequently, only remains at sum , and only remains at sum . These two edges are removed on day .
When , after day , only remains at XOR . After removing it, only remains at sum . After removing that edge, only remains at XOR .
After all edges listed in the table are removed, every sum or XOR vertex of positive degree has degree at least . This follows by checking the constant number of boundary configurations and, elsewhere, constructing another pair with the same sum or another valid value of with the same XOR. The assumption keeps all required values in range. Therefore, no further peeling occurs.
For , the slack used above is insufficient and extra cascades may occur. There are at most edges, so we directly construct the graph and simulate the peeling rounds.
Finding takes time per test case. The small simulation has constant size. The total time complexity is and the extra space complexity is .
Solution written by GPT5.6