DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

The Sum-XOR Dialogue

Back to problem

Editorial

Represent all possible pairs as a bipartite multigraph.

Create a left vertex SsS_sSs​ for every possible sum sss, and a right vertex YyY_yYy​ for every possible XOR value yyy. For every possible pair (a,b)(a,b)(a,b), add an edge between Sa+bS_{a+b}Sa+b​ and YaxorbY_{a\mathbin{\mathrm{xor}}b}Yaxorb​.

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 111. Mog knows the pair exactly when the vertex for the observed XOR has degree 111.

Because the statements are simultaneous, all edges incident to a degree-111 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 222-core forever has answer −1-1−1.

Assume X≥10X\ge 10X≥10. Let PPP be the largest power of two not exceeding XXX.

The only sum vertices of initial degree 111 correspond to the following four pairs:

  • (1,2)(1,2)(1,2) with sum 333.
  • (1,3)(1,3)(1,3) with sum 444.
  • (X−2,X)(X-2,X) with sum .

The initial degree-111 XOR vertices are classified as follows.

  • If X=PX=PX=P, every pair (i,P)(i,P)(i,P) with 1≤i<P1\le i<P1≤i<P is unique.
  • If X=P, only and are unique.

For the proof, fix an XOR value yyy. If y<Py<Py<P, the highest bit is preserved. Since P≥8P\ge 8P≥8, every occurring XOR value has at least two representations inside the lower block.

If y≥Py\ge Py≥P, write y=P+zy=P+zy=P+z. Write the larger number as P+tP+tP+t. The smaller number must then be , where . Only is invalid because it produces .

  • If X−P=0X-P=0X−P=0, there is only one possible ttt, so (z,P)(z,P)(z,P) is unique.
  • If X−P=1X-P=1X, exactly leave one valid pair, namely and .

Checking the vertices that can lose degree in each round gives the following table.

Condition

Day 1

Day 2

Day 3

Day 4

X=PX=PX=P

(1,2),(1,3),(i,P)(1,2),(1,3),(i,P)

When X=PX=PX=P, the first round removes every (i,P)(i,P)(i,P). Consequently, only (P−3,P−1)(P-3,P-1)(P−3,P− remains at sum , and only remains at sum . These two edges are removed on day .

(
X
−
2,X)
2X−22X-22X−2
  • (X−1,X)(X-1,X)(X−1,X) with sum 2X−12X-12X−1.
  • +1X=P+1
    X=P+1
    (1,P)(1,P)(1,P)
    (1,P+1)(1,P+1)(1,P+1)
  • Otherwise, there is no such pair.
  • txorzt\mathbin{\mathrm{xor}}z
    txorz
    0≤t≤X−P0\le t\le X-P0≤t≤X−P
    t=zt=zt=z
    000
    −
    P=
    1
    z=0,1z=0,1z=0,1
    (1,P+1)(1,P+1)(1,P+1)
    (1,P)(1,P)(1,P)
  • If X−P≥2X-P\ge 2X−P≥2, at least two valid values remain after excluding at most one.
  • (
    1
    ,
    2
    )
    ,
    (
    1
    ,
    3
    )
    ,
    (
    i
    ,
    P
    )

    (P−3,P−1),(P−2,P−1)(P-3,P-1),(P-2,P-1)(P−3,P−1),(P−2,P−1)

    none

    none

    X=P+1X=P+1X=P+1

    (1,2),(1,3),(1,P),(1,P+1),(P−1,P+1),(P,P+1)(1,2),(1,3),(1,P),(1,P+1),(P-1,P+1),(P,P+1)(1,2),(1,3),(1,P),(1,P+1),(P−1,P+1),(P,P+1)

    (P−2,P)(P-2,P)(P−2,P)

    (P−3,P+1)(P-3,P+1)(P−3,P+1)

    (P−4,P)(P-4,P)(P−4,P)

    otherwise

    (1,2),(1,3),(X−2,X),(X−1,X)(1,2),(1,3),(X-2,X),(X-1,X)(1,2),(1,3),(X−2,X),(X−1,X)

    none

    none

    none

    1
    )
    2P−42P-42P−4
    (P−2,P−1)(P-2,P-1)(P−2,P−1)
    2P−32P-32P−3
    222

    When X=P+1X=P+1X=P+1, after day 111, only (P−2,P)(P-2,P)(P−2,P) remains at XOR 2P−22P-22P−2. After removing it, only (P−3,P+1)(P-3,P+1)(P−3,P+1) remains at sum 2P−22P-22P−2. After removing that edge, only (P−4,P)(P-4,P)(P−4,P) remains at XOR 2P−42P-42P−4.

    After all edges listed in the table are removed, every sum or XOR vertex of positive degree has degree at least 222. This follows by checking the constant number of boundary configurations and, elsewhere, constructing another pair with the same sum or another valid value of ttt with the same XOR. The assumption X≥10X\ge 10X≥10 keeps all required values in range. Therefore, no further peeling occurs.

    For X≤9X\le 9X≤9, the slack used above is insufficient and extra cascades may occur. There are at most (92)=36\binom{9}{2}=36(29​)=36 edges, so we directly construct the graph and simulate the peeling rounds.

    Finding PPP takes O(log⁡X)O(\log X)O(logX) time per test case. The small simulation has constant size. The total time complexity is O(Tlog⁡X)O(T\log X)O(TlogX) and the extra space complexity is O(1)O(1)O(1).

    Solution written by GPT5.6