#P4089. [USACO17DEC] The Bovine Shuffle S

[USACO17DEC] The Bovine Shuffle S

题目描述

Convinced that happy cows generate more milk, Farmer John has installed a giant disco ball in his barn and plans to teach his cows to dance!

Looking up popular cow dances, Farmer John decides to teach his cows the "Bovine Shuffle". The Bovine Shuffle consists of his NN cows (1N100,0001 \leq N \leq 100,000) lining up in a row in some order, then performing successive "shuffles", each of which potentially re-orders the cows. To make it easier for his cows to locate themselves, Farmer John marks the locations for his line of cows with positions 1N1 \ldots N, so the first cow in the lineup will be in position 1, the next in position 2, and so on, up to position NN.

A shuffle is described with NN numbers, a1aNa_1 \ldots a_N, where a cow in position ii moves to position aia_i during the shuffle (and so, each aia_i is in the range 1N1 \ldots N). Every cow moves to its new location during the shuffle. Unfortunately, all the aia_i's are not necessarily distinct, so multiple cows might try to move to the same position during a shuffle, after which they will move together for all remaining shuffles.

Farmer John notices that some positions in his lineup contain cows in them no matter how many shuffles take place. Please help him count the number of such positions.

输入格式

The first line of input contains NN, the number of cows. The next line contains the NN integers a1aNa_1 \ldots a_N.

输出格式

Please output the number of positions that will always contain cows, no matter how many shuffles take place.

题目大意

背景

FJ 坚信快乐的牛可以产出更多的牛奶,因此 FJ 在牛棚里安装了一个巨大的迪斯科球并且打算让奶牛们学会跳舞。

FJ在许多出名的奶牛舞中选择了一种叫做 Bovine Shuffle 的舞蹈。这种舞蹈由 FJ 的 NN 头奶牛组成。NN 头奶牛以一种顺序排成一行,接着表演数次 shuffle。每次的 shuffle 会将奶牛重新排列。FJ 为了让奶牛们更加快乐,让奶牛们更容易找到重新排列后的位置,他标记了 NN 头奶牛的位置。在最开始,所有奶牛排成一排,第一头奶牛会在位置 11 上,第二只在 22 上,以此类推。

我们用 NN 个正整数 a1,a2ana_1,a_2\dots a_n 来描述每次的 shuffleaia_i 说明了在位置 ii 上的奶牛在经过这回合的 shuffle 之后,会跑到位置 aia_i 上。令 FJ 倍感非洲的是,即使 iijj 不同,aia_i 也可能会等于 aja_j !所以可能在一次 shuffle 后,有多头奶牛会跑到同一位置上,在这之后这群奶牛也会一同行动。

作为一名资深的养牛大户&坑牛专家的 FJ 猛然发现,无论经过多少次的 shuffle,一直都有 kk 个位置上有奶牛。FJ现在要你在 11 秒内帮他得出 kk 的值,否则就赏你 1018mod1010^{18} \bmod10 大板!

输入格式

第一行包含一个整数,NN

第二行包含 NN 个整数,描述题目中的a1,a2ana_1,a_2\dots a_n

输出格式

一个整数,代表 kk

4
3 2 1 3
3