luogu#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 cows () 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 , so the first cow in the lineup will be in position 1, the next in position 2, and so on, up to position .
A shuffle is described with numbers, , where a cow in position moves to position during the shuffle (and so, each is in the range ). Every cow moves to its new location during the shuffle. Unfortunately, all the '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 , the number of cows. The next line contains the integers .
输出格式
Please output the number of positions that will always contain cows, no matter how many shuffles take place.
题目大意
背景
FJ 坚信快乐的牛可以产出更多的牛奶,因此 FJ 在牛棚里安装了一个巨大的迪斯科球并且打算让奶牛们学会跳舞。
FJ在许多出名的奶牛舞中选择了一种叫做 Bovine Shuffle
的舞蹈。这种舞蹈由 FJ 的 头奶牛组成。 头奶牛以一种顺序排成一行,接着表演数次 shuffle
。每次的 shuffle
会将奶牛重新排列。FJ 为了让奶牛们更加快乐,让奶牛们更容易找到重新排列后的位置,他标记了 头奶牛的位置。在最开始,所有奶牛排成一排,第一头奶牛会在位置 上,第二只在 上,以此类推。
我们用 个正整数 来描述每次的 shuffle
。 说明了在位置 上的奶牛在经过这回合的 shuffle
之后,会跑到位置 上。令 FJ 倍感非洲的是,即使 与 不同, 也可能会等于 !所以可能在一次 shuffle
后,有多头奶牛会跑到同一位置上,在这之后这群奶牛也会一同行动。
作为一名资深的养牛大户&坑牛专家的 FJ 猛然发现,无论经过多少次的 shuffle
,一直都有 个位置上有奶牛。FJ现在要你在 秒内帮他得出 的值,否则就赏你 大板!
输入格式
第一行包含一个整数,
第二行包含 个整数,描述题目中的
输出格式
一个整数,代表
4
3 2 1 3
3