QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 1024 MB 總分: 100

#4937. 排列变换

统计

$1 \dots N$ 的一个排列是一个整数数组 $P[1 \dots N]$,使得 $1$ 到 $N$ 中的每个整数在 $P[1 \dots N]$ 中恰好出现一次。对 $P[1 \dots N]$ 的一次变换定义为将 $P[1 \dots N]$ 变为另一个排列 $P'[1 \dots N]$,其中对于所有 $1 \le i \le N$,满足 $P'[i] = P[P[i]]$。

给定一个排列 $P[1 \dots N]$。你的任务是计算通过对给定的排列进行零次或多次变换所能得到的不同排列的数量。

例如,设 $P[1 \dots N] = [3, 5, 1, 2, 4]$。 进行一次变换,将 $P$ 变为 $[1, 4, 3, 5, 2]$。 再进行一次变换,将 $P$ 变为 $[1, 5, 3, 2, 4]$。 * 再进行一次变换,将 $P$ 再次变为 $[1, 4, 3, 5, 2]$。

因此,通过进行零次或多次变换,可以得到 $3$ 种不同的排列: 1. $[3, 5, 1, 2, 4]$ 2. $[1, 4, 3, 5, 2]$ 3. $[1, 5, 3, 2, 4]$

输入格式

输入的第一行包含一个整数 $N$ ($1 \le N \le 100\,000$),表示给定排列中的元素个数。下一行包含 $N$ 个整数:$P[i]$ ($1 \le P[i] \le N$),表示该排列。保证 $P[1 \dots N]$ 中的元素是唯一的。

输出格式

输出一行一个整数,表示通过对给定的排列进行零次或多次变换所能得到的不同排列的数量,结果对 $998\,244\,353$ 取模。

样例

样例输入 1

5
3 5 1 2 4

样例输出 1

3

说明 1

这是题目描述中的示例。

样例输入 2

8
7 5 1 6 8 2 3 4

样例输出 2

4

说明 2

通过对给定的排列进行零次或多次变换,可以得到 $4$ 种不同的排列: 1. $[7, 5, 1, 6, 8, 2, 3, 4]$ 2. $[3, 8, 7, 2, 4, 5, 1, 6]$ 3. $[7, 6, 1, 8, 2, 4, 3, 5]$ 4. $[3, 4, 7, 5, 6, 8, 1, 2]$

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.