QOJ.ac

QOJ

时间限制: 1 s 内存限制: 2048 MB 总分: 100

#6256. 卡组随机化

统计

CC-BY-SA 4.0 由 Alexey Musulev 提供,来自 wikimedia.org

Alice 和 Bob 热衷于玩“Don’tminion”,这通常涉及大量不同规模牌组的洗牌。由于他们玩得非常频繁,他们不仅洗牌速度极快,而且非常稳定。每次 Alice 洗牌时,她的牌总是以相同的方式排列;同样,Bob 每次洗牌时,他的牌也总是以相同的方式排列。这对玩游戏来说不是什么好事,但却引出了一个有趣的问题。

他们知道,如果他们轮流洗牌,那么在某个时刻,牌组最终会恢复到开始时的顺序。Alice 先洗一次,然后 Bob 洗一次,接着 Alice 再洗一次,以此类推。他们从一副有序的牌组开始。然而,他们不知道在牌组恢复有序之前需要洗多少次牌。

你能帮他们计算需要洗多少次牌吗?由于 Alice 和 Bob 在有限的时间内最多只能洗 $10^{12}$ 次牌,任何严格大于此数的次数都应输出为 huge

输入格式

  • 第一行包含一个整数 $1 \le n \le 10^5$,表示牌组中牌的数量。
  • 第二行包含 $n$ 个不同的整数 $1 \le a_1, a_2, \dots, a_n \le n$,其中 $a_i$ 表示 Alice 洗牌后,原先位于位置 $i$ 的牌所到达的新位置。
  • 第三行包含 $n$ 个不同的整数 $1 \le b_1, b_2, \dots, b_n \le n$,其中 $b_i$ 表示 Bob 洗牌后,原先位于位置 $i$ 的牌所到达的新位置。

输出格式

  • 输出一个正整数 $m > 0$,表示使牌组恢复有序所需的最少洗牌次数;如果该次数严格大于 $10^{12}$,则输出 huge

样例

输入格式 1

3
2 3 1
3 1 2

输出格式 1

2

输入格式 2

6
5 1 6 3 2 4
4 6 5 1 3 2

输出格式 2

5

输入格式 3

8
1 4 2 6 7 8 5 3
3 6 8 4 7 1 5 2

输出格式 3

10

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.