#B4210. Determining the Earliest Meeting Round in a 128-Player Knockout Tournament

    ID: 11867 Type: Default 1000ms 256MiB

Determining the Earliest Meeting Round in a 128-Player Knockout Tournament

Determining the Earliest Meeting Round in a 128-Player Knockout Tournament

In the "We Love Science" event, 128 players compete in a knockout tournament. The tournament bracket is arranged in specific fashion using seeding:

  • The top 32 players (called seeded players) are assigned positions according to the following procedure:
    1. Round 0: Sort the seeded players by their numbers. Seed 1 is placed at position \(1\) and seed 2 is placed at position \(128\).
    2. Round 1: Divide the bracket into two halves: positions \(1\) to \(64\) (upper half) and \(65\) to \(128\) (lower half). Then the last position of the upper half (\(64\)) and the first of the lower half (\(65\)) are selected. The next two seeded players (seeds 3 and 4) are drawn randomly into these two positions.
    3. Round 2: Further divide each half into two quarters. In the upper half, split \(1\mbox{--}64\) into \(1\mbox{--}32\) and \(33\mbox{--}64\); in the lower half, split \(65\mbox{--}128\) into \(65\mbox{--}96\) and \(97\mbox{--}128\). Then select the last position of the first subgroup and the first position of the second subgroup from each half (i.e. positions \(32,33,96,97\)). The next four seeded players (seeds 5 to 8) are randomly drawn into these positions.
    4. Round 3: Each quarter (of size 32) is divided into two groups of 16. From each quarter, select the last position of the first group and the first position of the second group (i.e. positions \(16,17,48,49,80,81,112,113\)). The next eight seeded players (seeds 9 to 16) are randomly drawn into these positions.
    5. Round 4: Each group of 16 is divided into two groups of 8. For a segment \([L,R]\) of 16 positions, the two boundary positions are \(L+8-1\) and \(L+8\). Accordingly, the next 16 seeded players (seeds 17 to 32) fill the positions: \(8,9,24,25,40,41,56,57,72,73,88,89,104,105,120,121\).
  • The remaining 96 players (non‐seeded, numbered 33 through 128) are drawn randomly into the 96 positions that have not been occupied by seeded players.

All players are extremely strong. In fact, the two players, X and Y, have AIs that defeat every opponent except each other. Thus, if they both win all their matches until they meet, the round in which they meet is determined solely by their positions in the bracket.

For any two bracket positions \(p\) and \(q\), the round in which they would meet is the smallest integer \(r\) (with \(1 \le r \le 7\)) satisfying:

[ \left\lfloor \frac{p-1}{2^r} \right\rfloor = \left\lfloor \frac{q-1}{2^r} \right\rfloor. ]

Because the drawing of non‐seeded players and the random assignment within each seeding group can be chosen arbitrarily (subject to the restrictions above), we want to determine the earliest possible round in which X and Y could meet if both win all their games.

You are given the numbers of players X and Y (each between 1 and 128). If a player’s number is between 1 and 32, they are a seeded player with a fixed candidate set of positions:

  • If the number is 1, the candidate set is \(\{1\}\).
  • If the number is 2, the candidate set is \(\{128\}\).
  • If the number is 3 or 4, the candidate set is \(\{64,65\}\).
  • If the number is between 5 and 8, the candidate set is \(\{32,33,96,97\}\).
  • If the number is between 9 and 16, the candidate set is \(\{16,17,48,49,80,81,112,113\}\).
  • If the number is between 17 and 32, the candidate set is \(\{8,9,24,25,40,41,56,57,72,73,88,89,104,105,120,121\}\).

If a player’s number is greater than 32 (non‐seeded), their candidate set is the set of all positions that are not occupied by seeded players (i.e. the free positions). Using the optimal draw for non‐seeded players, they can be assigned any position from that set.

Your task is to compute the earliest possible round in which the two given players could meet. You may assume that in an optimal assignment:

  • If both players are non–seeded, they can be placed in two positions of a first round match (thus meeting in round 1).
  • If one player is seeded and one is not, you can choose a free position for the non–seeded player that minimizes the meeting round with the seeded player’s fixed position.
  • If both are seeded, the drawing within their candidate sets can be assumed to be chosen to minimize their meeting round.
  • </p>

    Input Format: Two integers representing the numbers of players X and Y.

    inputFormat

    The input consists of a single line with two space‐separated integers \(X\) and \(Y\) (\(1 \le X, Y \le 128\)).

    outputFormat

    Output a single integer: the earliest round (from 1 to 7) in which players X and Y can meet if both win all their matches.

    sample

    1 2
    7