B. World Cup

Codeforces
IDCF931B
Time1000ms
Memory256MB
Difficulty
constructive algorithmsimplementation
English · Original
Chinese · Translation
Formal · Original
The last stage of Football World Cup is played using the play-off system. There are _n_ teams left in this stage, they are enumerated from 1 to _n_. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over. Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids _a_ and _b_ can meet. ## Input The only line contains three integers _n_, _a_ and _b_ (2 ≤ _n_ ≤ 256, 1 ≤ _a_, _b_ ≤ _n_) — the total number of teams, and the ids of the teams that Arkady is interested in. It is guaranteed that _n_ is such that in each round an even number of team advance, and that _a_ and _b_ are not equal. ## Output In the only line print "_Final!_" (without quotes), if teams _a_ and _b_ can meet in the Final. Otherwise, print a single integer — the number of the round in which teams _a_ and _b_ can meet. The round are enumerated from 1. [samples] ## Note In the first example teams 1 and 2 meet in the first round. In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds. In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
足球世界杯的最后阶段采用淘汰赛制。 目前剩下 #cf_span[n] 支球队,编号从 #cf_span[1] 到 #cf_span[n]。比赛进行若干轮,每轮中剩余球队按其编号排序,然后第 1 名与第 2 名比赛,第 3 名与第 4 名比赛,第 5 名与第 6 名比赛,依此类推。保证每轮球队数量均为偶数。每场比赛的胜者晋级下一轮,败者被淘汰,没有平局。最后一轮仅剩两支球队进行一场比赛:这一轮称为决赛,胜者称为冠军,赛事结束。 阿尔卡迪希望他最喜欢的两支球队能在决赛中相遇。不幸的是,球队编号已经确定,可能由于它们在更早阶段就不得不相遇(如果它们都足够强大),导致无法在决赛中相遇。请确定编号为 #cf_span[a] 和 #cf_span[b] 的两支球队可能在第几轮相遇。 仅一行包含三个整数 #cf_span[n], #cf_span[a] 和 #cf_span[b](#cf_span[2 ≤ n ≤ 256], #cf_span[1 ≤ a, b ≤ n])——球队总数,以及阿尔卡迪感兴趣的两支球队的编号。 保证 #cf_span[n] 的取值使得每轮晋级的球队数均为偶数,且 #cf_span[a] 和 #cf_span[b] 不相等。 若两支球队 #cf_span[a] 和 #cf_span[b] 可能在决赛中相遇,请在一行中输出 "_Final!_"(不含引号)。 否则,输出一个整数——两支球队 #cf_span[a] 和 #cf_span[b] 可能相遇的轮次编号。轮次从 #cf_span[1] 开始编号。 在第一个例子中,球队 #cf_span[1] 和 #cf_span[2] 在第一轮相遇。 在第二个例子中,球队 #cf_span[2] 和 #cf_span[6] 只有在赢得此前所有比赛的情况下,才可能在第三轮(即决赛)相遇。 在第三个例子中,编号为 #cf_span[7] 和 #cf_span[5] 的球队若在第一轮都获胜,则可能在第二轮相遇。 ## Input 仅一行包含三个整数 #cf_span[n], #cf_span[a] 和 #cf_span[b](#cf_span[2 ≤ n ≤ 256], #cf_span[1 ≤ a, b ≤ n])——球队总数,以及阿尔卡迪感兴趣的两支球队的编号。保证 #cf_span[n] 的取值使得每轮晋级的球队数均为偶数,且 #cf_span[a] 和 #cf_span[b] 不相等。 ## Output 若两支球队 #cf_span[a] 和 #cf_span[b] 可能在决赛中相遇,请在一行中输出 "_Final!_"(不含引号)。否则,输出一个整数——两支球队 #cf_span[a] 和 #cf_span[b] 可能相遇的轮次编号。轮次从 #cf_span[1] 开始编号。 [samples] ## Note 在第一个例子中,球队 #cf_span[1] 和 #cf_span[2] 在第一轮相遇。 在第二个例子中,球队 #cf_span[2] 和 #cf_span[6] 只有在赢得此前所有比赛的情况下,才可能在第三轮(即决赛)相遇。 在第三个例子中,编号为 #cf_span[7] 和 #cf_span[5] 的球队若在第一轮都获胜,则可能在第二轮相遇。
Let $ n $ be the number of teams, and $ a, b $ be the IDs of the two teams ($ a \neq b $). The tournament is a single-elimination bracket with $ \log_2 n $ rounds, indexed from $ 1 $ to $ \log_2 n $, where round $ \log_2 n $ is the Final. In round $ r $, the $ n / 2^{r-1} $ remaining teams are paired as $ (1,2), (3,4), \dots $, based on their current indices (sorted by original ID). Two teams $ a $ and $ b $ can meet in round $ r $ if and only if they are in the same bracket of size $ 2^r $, but in different brackets of size $ 2^{r-1} $. Define the **bracket index** of team $ x $ in round $ r $ as: $$ \left\lfloor \frac{x - 1}{2^{r-1}} \right\rfloor $$ Teams $ a $ and $ b $ meet in round $ r $ if: $$ \left\lfloor \frac{a - 1}{2^{r-1}} \right\rfloor = \left\lfloor \frac{b - 1}{2^{r-1}} \right\rfloor \quad \text{and} \quad \left\lfloor \frac{a - 1}{2^r} \right\rfloor \neq \left\lfloor \frac{b - 1}{2^r} \right\rfloor $$ Let $ r_{\text{meet}} $ be the smallest $ r \in \{1, 2, \dots, \log_2 n\} $ satisfying the above. If $ r_{\text{meet}} = \log_2 n $, output "Final!". Otherwise, output $ r_{\text{meet}} $.
Samples
Input #1
4 1 2
Output #1
1
Input #2
8 2 6
Output #2
Final!
Input #3
8 7 5
Output #3
2
API Response (JSON)
{
  "problem": {
    "name": "B. World Cup",
    "description": {
      "content": "The last stage of Football World Cup is played using the play-off system. There are _n_ teams left in this stage, they are enumerated from 1 to _n_. Several rounds are held, in each round the remaini",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF931B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "The last stage of Football World Cup is played using the play-off system.\n\nThere are _n_ teams left in this stage, they are enumerated from 1 to _n_. Several rounds are held, in each round the remaini...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "足球世界杯的最后阶段采用淘汰赛制。\n\n目前剩下 #cf_span[n] 支球队,编号从 #cf_span[1] 到 #cf_span[n]。比赛进行若干轮,每轮中剩余球队按其编号排序,然后第 1 名与第 2 名比赛,第 3 名与第 4 名比赛,第 5 名与第 6 名比赛,依此类推。保证每轮球队数量均为偶数。每场比赛的胜者晋级下一轮,败者被淘汰,没有平局。最后一轮仅剩两支球队进行一场比赛:这一轮称为...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the number of teams, and $ a, b $ be the IDs of the two teams ($ a \\neq b $).\n\nThe tournament is a single-elimination bracket with $ \\log_2 n $ rounds, indexed from $ 1 $ to $ \\log_2 n $,...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments