#abc296e. E - Transition Game

E - Transition Game

Score : 500500 points

问题描述

给定一个包含 NN 个数的序列:A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N)。其中,每个 AiA_i1iN1\leq i\leq N)满足 1AiN1\leq A_i \leq N

高桥和青木将进行 NN 轮游戏。对于每一轮 i=1,2,,Ni=1,2,\ldots,N,第 ii 轮游戏按以下方式进行:

  1. 青木指定一个正整数 KiK_i
  2. 在得知青木指定的 KiK_i 后,高桥从 11NN(包括两端点)选择一个整数 SiS_i,并将其写在黑板上。
  3. 重复以下操作 KiK_i 次:
    • 将黑板上所写的整数 xx 替换为 AxA_x

若经过 KiK_i 次迭代后,黑板上出现数字 ii,则高桥赢得第 ii 轮;否则,青木赢得第 ii 轮。

这里,对于每一轮 i=1,2,,Ni=1,2,\ldots,NKiK_iSiS_i 可以独立选择。

假设两位玩家都采取最优策略以求获胜,请找出高桥赢得的轮数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a sequence of NN numbers: A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N). Here, each AiA_i (1iN)(1\leq i\leq N) satisfies 1AiN1\leq A_i \leq N.

Takahashi and Aoki will play NN rounds of a game. For each i=1,2,,Ni=1,2,\ldots,N, the ii-th game will be played as follows.

  1. Aoki specifies a positive integer KiK_i.

  2. After knowing KiK_i Aoki has specified, Takahashi chooses an integer SiS_i between 11 and NN, inclusive, and writes it on a blackboard.

  3. Repeat the following KiK_i times.

    • Replace the integer xx written on the blackboard with AxA_x.

If ii is written on the blackboard after the KiK_i iterations, Takahashi wins the ii-th round; otherwise, Aoki wins.
Here, KiK_i and SiS_i can be chosen independently for each i=1,2,,Ni=1,2,\ldots,N.

Find the number of rounds Takahashi wins if both players play optimally to win.

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 1AiN1\leq A_i\leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Find the number of rounds Takahashi wins if both players play optimally to win.

Sample Input 1

3
2 2 3

Sample Output 1

2

In the first round, if Aoki specifies K1=2K_1=2, Takahashi cannot win whichever option he chooses for S1S_1: 11, 22, or 33.

For example, if Takahashi writes S1=1S_1=1 on the initial blackboard, the two operations will change this number as follows: 12(=A1)1\to 2(=A_1), 22(=A2)2\to 2(=A_2). The final number on the blackboard will be 2(1)2(\neq 1), so Aoki wins.

On the other hand, in the second and third rounds, Takahashi can win by writing 22 and 33, respectively, on the initial blackboard, whatever value Aoki specifies as KiK_i.

Therefore, if both players play optimally to win, Takashi wins two rounds: the second and the third. Thus, you should print 22.

Sample Input 2

2
2 1

Sample Output 2

2

In the first round, Takahashi can win by writing 22 on the initial blackboard if K1K_1 specified by Aoki is odd, and 11 if it is even.

Similarly, there is a way for Takahashi to win the second round. Thus, Takahashi can win both rounds: the answer is 22.

update @ 2024/3/10 12:18:32