CF542C.Idempotent functions

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1, 2, ..., n} is such function , that for any the formula g(g(x)) = g(x) holds.

Let's denote as f(k)(x) the function f applied k times to the value x. More formally, f(1)(x) = f(x), f(k)(x) = f(f(k - 1)(x)) for each k > 1.

You are given some function . Your task is to find minimum positive integer k such that function f(k)(x) is idempotent.

不久之前,列昂尼德了解到了幂等函数。定义在集合 {1, 2, ..., n}\{1, 2, ..., n\} 上的幂等函数 gg 是指:对任意 x∈{1, 2, ..., n}x \in \{1, 2, ..., n\},均满足 g(g(x)) = g(x)g(g(x)) = g(x) 的函数。

我们用 f(k)(x)f^{(k)}(x) 表示将函数 ff 对值 xx 连续应用 kk 次的结果。更准确地,定义为:f(1)(x) = f(x)f^{(1)}(x) = f(x),且对每个 k > 1k > 1,有 f(k)(x) = f(f(k−1)(x))f^{(k)}(x) = f(f^{(k-1)}(x))。

现给出某个函数 ff。你的任务是找出最小的正整数 kk,使得函数 f(k)(x)f^{(k)}(x) 是幂等的。

输入格式

In the first line of the input there is a single integer n (1 ≤ n ≤ 200) — the size of function f domain.

In the second line follow f(1), f(2), ..., f(n) (1 ≤ f(i) ≤ n for each 1 ≤ i ≤ n), the values of a function.

输入的第一行包含一个整数 nn(1≤n≤2001 \leq n \leq 200)——即函数 ff 定义域的大小。

第二行包含 f(1), f(2), …, f(n)f(1),\,f(2),\,\dots,\,f(n)(对每个 1≤i≤n1 \leq i \leq n,满足 1≤f(i)≤n1 \leq f(i) \leq n),即该函数的函数值。

输出格式

Output minimum k such that function f(k)(x) is idempotent.

输出使函数 f(k)(x)f^{(k)}(x) 成为幂等函数的最小 kk。

输入输出样例

  • 输入#1

    4
    1 2 2 4

    输出#1

    1
  • 输入#2

    3
    2 3 3

    输出#2

    2
  • 输入#3

    3
    2 3 1

    输出#3

    3

说明/提示

In the first sample test function f(x) = f(1)(x) is already idempotent since f(f(1)) = f(1) = 1, f(f(2)) = f(2) = 2, f(f(3)) = f(3) = 2, f(f(4)) = f(4) = 4.

In the second sample test:

  • function f(x) = f(1)(x) isn't idempotent because f(f(1)) = 3 but f(1) = 2;
  • function f(x) = f(2)(x) is idempotent since for any x it is true that f(2)(x) = 3, so it is also true that f(2)(f(2)(x)) = 3.

In the third sample test:

  • function f(x) = f(1)(x) isn't idempotent because f(f(1)) = 3 but f(1) = 2;
  • function f(f(x)) = f(2)(x) isn't idempotent because f(2)(f(2)(1)) = 2 but f(2)(1) = 3;
  • function f(f(f(x))) = f(3)(x) is idempotent since it is identity function: f(3)(x) = x for any meaning that the formula f(3)(f(3)(x)) = f(3)(x) also holds.

在第一个样例测试中,函数 f(x)=f(1)(x)f(x) = f^{(1)}(x) 已经是幂等的,因为 f(f(1))=f(1)=1f(f(1)) = f(1) = 1,f(f(2))=f(2)=2f(f(2)) = f(2) = 2,f(f(3))=f(3)=2f(f(3)) = f(3) = 2,f(f(4))=f(4)=4f(f(4)) = f(4) = 4。

在第二个样例测试中:

  • 函数 f(x)=f(1)(x)f(x) = f^{(1)}(x) 不是幂等的,因为 f(f(1))=3f(f(1)) = 3,但 f(1)=2f(1) = 2;
  • 函数 f(x)=f(2)(x)f(x) = f^{(2)}(x) 是幂等的,因为对任意 xx,均有 f(2)(x)=3f^{(2)}(x) = 3,因此 f(2)(f(2)(x))=3f^{(2)}(f^{(2)}(x)) = 3 也成立。

在第三个样例测试中:

  • 函数 f(x)=f(1)(x)f(x) = f^{(1)}(x) 不是幂等的,因为 f(f(1))=3f(f(1)) = 3,但 f(1)=2f(1) = 2;
  • 函数 f(f(x))=f(2)(x)f(f(x)) = f^{(2)}(x) 不是幂等的,因为 f(2)(f(2)(1))=2f^{(2)}(f^{(2)}(1)) = 2,但 f(2)(1)=3f^{(2)}(1) = 3;
  • 函数 f(f(f(x)))=f(3)(x)f(f(f(x))) = f^{(3)}(x) 是幂等的,因为它是恒等函数:对任意 ,均有 f(3)(x)=xf^{(3)}(x) = x,因此公式 f(3)(f(3)(x))=f(3)(x)f^{(3)}(f^{(3)}(x)) = f^{(3)}(x) 也成立。

输入解题思路,AI测评打分。不知道怎么写?

首页